2016-06-14 5 views
0

텍스트가있는 QTextEdit이 있습니다. 사용자는 변수 startPos에 저장된 QCursor 위치에서 문서 끝까지 텍스트를 변경할 수 있습니다. 텍스트의 시작 부분은 동일하게 유지되어야합니다. 나는 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

당신은이 최종 CODE ...

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