在我的 qt 应用程序中,我正在从 QWebFrame(来自 QWebPage 的 mainFrame)打印一些大型 html 代码。html 代码包含一些内联图像,需要一段时间才能打印出来。因此,我尝试将打印调用放入单独的线程中以保持 gui 响应。正如文档中提到的,我认为这应该是可能的(在线程中绘画)。
但我还是得到了著名的“QPixmap:在 GUI 线程之外使用 pixmaps 是不安全的”
我的打印代码如下:
void PrintDialog::paintRequested(QPrinter *printer) {
futureWatcher = new QFutureWatcher<void>();
QEventLoop q;
connect(futureWatcher, SIGNAL(finished()), &q, SLOT(quit()), Qt::UniqueConnection);
futureWatcher->setFuture(QtConcurrent::run(m_webPage->mainFrame(), &QWebFrame::print, printer));
q.exec();
}
为了澄清所有对象都驻留在 gui 线程中,并且之前生成了 html。图像被内联如下:
QImage image
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
QString html = "<img src=\"data:image/png;base64," + QString(ba.toBase64() + "\"/>";
那么我在这里做错了什么?是不是 QWebFrame 内部使用了一些 QPixmaps?有没有在另一个线程中使用 QWebFrame::print 调用的解决方案?