QTextBrowser - 如何从鼠标点击位置识别图像

标签 qt qtextdocument qtextbrowser

我正在使用 QTextBrowser显示包含大量图像的富文本,每个图像都用 HTML <img> 指定标记并使用 QTextDocument::addResource() 添加为资源.

我希望能够做的是,在上下文菜单处理程序中(即鼠标点击位置可用),识别点击结束的图像。可以判断点击是否在图像上,因为 cursorForPosition(event->pos()).block().text()返回以 Unicode 0xFFFC 开头的字符串。不幸的是, View 中的每个图像都返回相同的字符串。

可以通过 QTextDocument::allFormats() 获取所有正在使用的格式。 ,确定哪些是图像格式,并获取它们的图像资源名称。不幸的是,似乎无法获得它们的实际显示位置或边界矩形。

最佳答案

来自 documentation :

Inline images are represented by an object replacement character (0xFFFC in Unicode) which has an associated QTextImageFormat. The image format specifies a name with setName() that is used to locate the image.



您可以使用 charFormat().toImageFormat().name()在光标上提取图像的 URL。下面是一个独立的例子。有两个值得注意的细节:
  • 光标有时会指向图像之前的一个字符。因此解决方法; Qt 4.8.5 和 5.1.1 似乎都需要。
  • 弹出菜单应异步显示,以免阻塞应用程序的其余部分。文档中提供的示例代码是糟糕用户体验的根源,应被视为邪恶的可憎之物。所有小部件在关闭时都可以自动删除自己,因此菜单不会泄漏。一个 QPointer仅用于证明这一事实。它跟踪菜单的生命周期并在菜单删除自身时将自身归零。

  • #include <QApplication>
    #include <QTextBrowser>
    #include <QImage>
    #include <QPainter>
    #include <QMenu>
    #include <QContextMenuEvent>
    #include <QTextBlock>
    #include <QPointer>
    #include <QDebug>
    
    class Browser : public QTextBrowser
    {
        QPointer<QMenu> m_menu;
    protected:
        void contextMenuEvent(QContextMenuEvent *ev) {
            Q_ASSERT(m_menu.isNull()); // make sure the menus aren't leaking
            m_menu = createStandardContextMenu();
            QTextCursor cur = cursorForPosition(ev->pos());
            QTextCharFormat fmt = cur.charFormat();
            qDebug() << "position in block" << cur.positionInBlock()
                     << "object type" << cur.charFormat().objectType();
            if (fmt.objectType() == QTextFormat::NoObject) {
                // workaround, sometimes the cursor will point one object to the left of the image
                cur.movePosition(QTextCursor::NextCharacter);
                fmt = cur.charFormat();
            }
            if (fmt.isImageFormat()) {
                QTextImageFormat ifmt = fmt.toImageFormat();
                m_menu->addAction(QString("Image URL: %1").arg(ifmt.name()));
            }
            m_menu->move(ev->globalPos());
            m_menu->setAttribute(Qt::WA_DeleteOnClose); // the menu won't leak
            m_menu->show(); // show the menu asynchronously so as not to block the application
        }
    };
    
    void addImage(QTextDocument * doc, const QString & url) {
        QImage img(100, 100, QImage::Format_ARGB32_Premultiplied);
        img.fill(Qt::white);
        QPainter p(&img);
        p.drawRect(0, 0, 99, 99);
        p.drawText(img.rect(), url);
        doc->addResource(QTextDocument::ImageResource, QUrl(url), img);
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        QTextDocument doc;
        Browser browser;
        doc.setHtml("<img src=\"data://image1\"/><br/><img src=\"data://image2\"/>");
        addImage(&doc, "data://image1");
        addImage(&doc, "data://image2");
        browser.show();
        browser.setDocument(&doc);
        return a.exec();
    }
    

    关于QTextBrowser - 如何从鼠标点击位置识别图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18700945/

    相关文章:

    qt - 在 QT 中关闭窗口之前获取应用程序退出信号

    c++ - Qt写入QTextTable时检测断线

    c++ - 更改 QTextBrowser 中的最后一行

    c++ - 如何使用 PushButton-Qt 连接 ComboBox 和 TextBrowser?

    c++ - Qt4 : Decorating a QLineEdit (painting around it)

    c++ - QT_NO_DEBUG 是否会导致 NDEBUG 的定义?

    java - 开始在 Android 2.3 上编程

    c++ - Qt - 如何检测自动换行后行数的增加?

    c++ - 如何使用QTextDocumentFragment在QTextEdit中设置图像和文本的大小

    python - 将 blob 图像从 sqlite db 插入 QPixMap 到 PyQt5 中的 QTextBrowser