【问题标题】:Custom mouse interaction for custom QTextObject in QTextEditQTextEdit中自定义QTextObject的自定义鼠标交互
【发布时间】:2014-03-20 15:50:20
【问题描述】:

我有一个自定义小部件,我想将它添加到 QTextEdit(通过拖放),然后允许用户双击小部件以打开单独的编辑窗口。

现在我将它拖到可以将小部件拖到 QTextEdit 上的位置,并添加了一个图像来表示文档中的小部件。这是通过实现 QTextObjectInterface 的包装类完成的。

现在我需要弄清楚如何处理鼠标事件,以便当用户单击图像时,程序知道会调出自定义编辑 GUI。

我现在拥有的大概是,

class MyWidget : public QWidget
{ ... }

class MyWidgetWrapper : public QObject, QTextObjectInterface
{
    Q_OBJECT
    Q_INTERFACES(QTextObjectInterface)
    ....

    void drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)
    {
        MyWidgetWrapper *tmp = qvariant_cast<MyWidgetWrapper*>(format.property(1));
        painter->drawImage(rect, tmp->mMyWidget.getImage());
    }

    private:
    MyWidget mMyWidget;
}

然后,在我的自定义 QTextEdit 类中,我有

bool MyTextEdit::initialize()
{
    MyWidgetWrapper *tmp = new MyWidgetWrapper();
    document()->documentLayout()->registerHandler(MyWidgetWrapperFormat, tmp);
    return true;
}

void MyTextEdit::insertFromMimeData(const QMimeData *source)
{
    if(source->hasFormat("application/x-MyWidgetWrapper"))
    {
        MyWidgetWrapper *widgetWrapper = new MyWidgetWrapper(this);

    QTextCharFormat charFormat;
    charFormat.setObjectType(MyWidgetWrapperFormat);
    charFormat.setProperty(MyWidgetWrapperData, QVariant::fromValue(widgetWrapper));

    QTextCursor cursor = textCursor();
    cursor.insertText(QString(QChar::ObjectReplacementCharacter), charFormat);
    setTextCursor(cursor);
    }
}

【问题讨论】:

    标签: qt qtextedit


    【解决方案1】:

    好的,在玩了之后,我管理了一些可行的方法。不过,我不确定这是否是最好的解决方案。我想我希望能更直接地给我点击的对象。正如你所想象的那样,当我开始添加许多自定义 QTextObjects 时,这个解决方案变得很烦人,因为我需要一堆 if ... else if ... 语句

    基本上,因为我已经有了自己的 QTextEdit 子类,所以我实现了自己的 mouseDoubleClickEvent 处理程序

    void MyTextEdit::mouseDoubleClickEvent(QMouseEvent *event)
    {
        QPoint eventPos = event->pos();
        QTextCursor cursor = cursorForPosition(eventPos);
    
        // now check to see if we've moved the cursor to the space
        // before or after the actual click location
        QRect rect = cursorRect();
        if(rect.x() < eventPos.x())
            cursor.movePosition(QTextCursor::Right);
    
        // charFormat is for the object BEFORE the 
        // cursor postion
        int type = cursor.charFormat().objectType();
        if(type == MyWidgetWrapperFormat)
        {
            MyWidgetWrapper *ed = qvariant_cast<MyWidgetWrapper*>(cursor.charFormat().property(1));
    
            mFileDialog->setFileMode(QFileDialog::ExistingFile);
            mFileDialog->setNameFilter("Images (*.bmp *.jpg)");
    
            if(mFileDialog->exec())
            {
                QStringList filenames = mFileDialog->selectedFiles();
                QString filename = filenames.at(0);
                QImage image(filename);
                ed->MyWidget()->setImage(image);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 2010-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多