QTextEdit - 根据 QCursor 位置有条件地拖放

QTextEdit - conditional drag and drop according to QCursor position

我有一个带有文本的 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);

这应该可以修复第一个编译错误。不幸的是,似乎没有任何明显的方法可以使 QPoint 与 QTextCursor 相关联(尽管可能有一种方法可以通过 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();
}

此处仅当鼠标指针不在禁止区域时才接受建议的放置操作。否则用户将看到(通过指针字形或其他方式)掉落将不被接受。