我正在使用 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。下面是一个独立的例子。有两个值得注意的细节: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/