【问题标题】:QTextEdit - conditional drag and drop according to QCursor positionQTextEdit - 根据 QCursor 位置有条件的拖放
【发布时间】:2016-06-14 12:36:01
【问题描述】:

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

【问题讨论】:

    标签: c++ qt drag-and-drop qtextedit


    【解决方案1】:

    您目前有...

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

    如果您想要与建议的放置位置关联的 QTextCursor,您应该使用...

    QTextCursor t = cursorForPosition(p);
    

    这应该可以解决第一个编译错误。不幸的是,似乎没有任何明显的方法可以让 QPoint 与 QTextCursor 相关联(尽管可能有一种方法可以通过 QTextDocument 和 QTextBlock,但我没有检查过)。如果是这种情况,那么您将不得不自己执行 drop...

    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();
    }
    

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

    【讨论】:

    • 你是个天才,G.M.你的方法非常简单而且很好。我更新了代码。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-10-09
    • 2011-03-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多