텍스트가있는 QTextEdit가 있습니다. 사용자는 startPos
변수에 저장된 QCursor 위치에서 문서 끝까지 만 텍스트를 변경할 수 있습니다. 텍스트의 시작 부분은 동일해야합니다. 나는 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();
}
현재 ...
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();
}
여기서 제안 된 드롭 동작은 마우스 포인터가 금지 된 영역에없는 경우에만 허용됩니다. 그렇지 않으면 사용자는 드롭이 허용되지 않는다는 것을 (포인터 글리프 등을 통해) 보게됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다