Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 使用QWebFrame将HTML呈现为QImage/QPicture_Qt_Qt5_Qtwebkit_Qwebpage - Fatal编程技术网

Qt 使用QWebFrame将HTML呈现为QImage/QPicture

Qt 使用QWebFrame将HTML呈现为QImage/QPicture,qt,qt5,qtwebkit,qwebpage,Qt,Qt5,Qtwebkit,Qwebpage,我正在尝试在没有QWebView的情况下使用QWebPage/QWebFrame将html数据呈现到qimage或qpicture: #include <QtWebKitWidgets> auto htmlData = R"( <!DOCTYPE html> <html> <body> <p>A quick brown fox jumps over the lazy dog.</p> </body> </

我正在尝试在没有QWebView的情况下使用QWebPage/QWebFrame将html数据呈现到qimage或qpicture:

#include <QtWebKitWidgets>

auto htmlData = R"(
<!DOCTYPE html>
<html>
<body>
<p>A quick brown fox jumps over the lazy dog.</p>
</body>
</html>
)";

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebPage page;
    auto frame = page.mainFrame();
    frame->setContent(htmlData, "text/html");

    QImage img(500, 500, QImage::Format_ARGB32);
    QPainter p(&img);
    frame->render(&p);
    p.end();
    img.save("html.png");

    return 0;
}
#包括
自动htmlData=R“(
一只敏捷的棕色狐狸跳过了那只懒狗

)"; int main(int argc,char*argv[]) { 质量保证申请a(argc、argv); QQ网页; 自动帧=page.mainFrame(); 框架->设置内容(htmlData,“文本/html”); QImage img(500500,QImage::格式_ARGB32); QPainter p&img; 帧->渲染(&p); p、 end(); 保存(“html.png”); 返回0; }
结果图像为空白。QWebFrame::print确实生成了正确的PDF文件


要使html正确呈现,我需要做什么?

确定。我终于明白了。我需要调用
QWebPage::setViewportSize()
。我猜如果页面是QWebView的一部分,它会自动设置