2016-06-14 8 views
0

私はテキストの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(); 
} 

答えて

1

あなたが現在持っている最終的なコード...

QTextCursor t(textCursor()); //create a cursor for QTextEdit 
t.setPos(&p); 

あなたが使用する必要があり、提案ドロップ位置に関連したQTextCursorをしたい場合は...

QTextCursor t = cursorForPosition(p); 

これは最初のコンパイルエラーを修正するはずです。残念ながら、QTextCursorに関連付けられたQPointを取得する明白な方法はありません(QTextDocumentとQTextBlockを経由する方法があるかもしれませんが、私はチェックしていません)。その場合は、あなたは自分を落とす実行する必要があります...

if (t.position() < startPos) 
    t.movePosition(QTextCursor::End); 
setTextCursor(t); 
insertPlainText(event->mimeData()->text()); 

しかし、私はあなたが何しようとしていることは、ユーザーにとって非常に紛らわしい証明するかもしれないことを示唆していることができます。テキストがドロップされた場合にどうなるかについての視覚的なインジケータがあります。禁止された領域にテキストをドロップすると、現在のテキストの末尾に追加されます(大きなドキュメントでは表示されないことがあります)。マウスポインタが禁止領域内にない場合は、より良いアプローチはdragMoveEventを上書きするかもしれないことを念頭に置いて

...

void BasicOutput::dragMoveEvent (QDragMoveEvent *event) 
{ 
    QTextCursor t = cursorForPosition(p); 
    if (t.position() >= startPos) 
    event->acceptProposedAction(); 
} 

は、ここで提案されているドロップアクションのみが受け入れられています。さもなければ、ユーザは(ポインタのグリフなどを介して)ドロップが受け入れられないことを見るでしょう。

+0

あなたは天才です、G.M.あなたのアプローチは非常にシンプルで良いです。私はコードを更新しました。ありがとうございました。 – Junior

関連する問題