私はテキストのQTextEditを持っています。ユーザは、startPos
変数に格納されているQCカーソルの位置からのみ、文書の最後までテキストを変更することができます。テキストの始まりは同じでなければなりません。 私はQCursorの位置を調整することでそれを行うことができました。QTextEdit - QCursorの位置に応じた条件付きドラッグ&ドロップ
ただし、ユーザーはいつでも禁止された領域にテキストをドラッグアンドドロップできます。 QCursorの位置に応じて条件付きドラッグ&ドロップを行いたいです。だから、もしユーザが禁じられた領域(カーソル位置startPos
の前)に何らかのテキストをドロップすれば、そのテキストをドキュメントの最後に置いて欲しい。カーソル位置startPos
の後にユーザーがテキストをドロップした場合、そのようにすることが許可されます。
class BasicOutput : public QTextEdit, public ViewWidgetIFace
{
Q_OBJECT
public:
BasicOutput();
~BasicOutput();
virtual void dragEnterEvent(QDragEnterEvent *e);
virtual void dropEvent(QDropEvent *event);
private:
int startPos;
};
と簡素化(非機能)残りのコード:
BasicOutput::BasicOutput() : QTextEdit() {
setInputMethodHints(Qt::ImhNoPredictiveText);
setFocusPolicy(Qt::StrongFocus);
setAcceptRichText(false);
setUndoRedoEnabled(false);
}
void BasicOutput::dragEnterEvent(QDragEnterEvent *e){
e->acceptProposedAction();
}
void BasicOutput::dropEvent(QDropEvent *event){
QPoint p = event->pos(); //get position of drop
QTextCursor t(textCursor()); //create a cursor for QTextEdit
t.setPos(&p); //try convert QPoint to QTextCursor to compare with position stored in startPos variable - ERROR
//if dropCursorPosition < startPos then t = endOfDocument
//if dropCursorPosition >= startPos then t remains the same
p = t.pos(); //convert the manipulated cursor position to QPoint - ERROR
QDropEvent drop(p,event->dropAction(), event->mimeData(), event->mouseButtons(), event->keyboardModifiers(), event->type());
QTextEdit::dropEvent(&drop); // Call the parent function w/ the modified event
}
エラーは以下のとおりです。
In member function 'virtual void BasicOutput::dropEvent(QDropEvent*)':
error: 'class QTextCursor' has no member named 'setPos' t.setPos(&p);
error: 'class QTextCursor' has no member named 'pos'p = t.pos();
ユーザードラッグから禁止されたテキスト領域を保護し、ドロップする方法は?
大いに、 フロリン。
void BasicOutput::dragEnterEvent(QDragEnterEvent *e){
if (e->mimeData()->hasFormat("text/plain"))
e->acceptProposedAction();
else
e->ignore();
}
void BasicOutput::dragMoveEvent (QDragMoveEvent *event){
QTextCursor t = cursorForPosition(event->pos());
if (t.position() >= startPos){
event->acceptProposedAction();
QDragMoveEvent move(event->pos(),event->dropAction(), event->mimeData(), event->mouseButtons(), event->keyboardModifiers(), event->type());
QTextEdit::dragMoveEvent(&move); // Call the parent function (show cursor and keep selection)
}else
event->ignore();
}
あなたは天才です、G.M.あなたのアプローチは非常にシンプルで良いです。私はコードを更新しました。ありがとうございました。 – Junior