Qt WebEngineView 渲染空图像
Qt WebEngineView render empty image
我正在使用 qt 5.6.2。我想在网页上截图。这是我使用的代码:
#include <QtWidgets/QApplication>
#include <QtWebEngineWidgets/QWebEngineView>
int main(int argc, char *argv[]){
int left, top, width, height;
left = 0;
top = 0;
width = 600;
height = 800;
QApplication a(argc, argv);
QWebEngineView* w = new QWebEngineView();
QImage image(height, width, QImage::Format_RGB32);
QRegion rg(left, top, width, height);
QPainter painter(&image);
w->page()->load(QUrl("https://www.yahoo.com"));
w->show();
w->page()->view()->render(&painter, QPoint(), rg);
painter.end();
image.save("test.png", "PNG", 80);
return a.exec();
}
我运行这个程序,出现一个window,我可以看到yahoo
的内容。然后我将结果保存到 test.png
但那是一张白色图片。是我无法将结果保存到 image
还是我无法将结果从 image
保存到文件 test.png
以及如何修复它?
您需要等待页面加载完成。
QWebEngineView* w = new QWebEngineView();
w->page()->load(QUrl("https://www.yahoo.com"));
w->show();
QObject::connect(w, &QWebEngineView::loadFinished,
[w](bool bOk)
{
int left, top, width, height;
left = 0;
top = 0;
width = 600;
height = 800;
QImage image(height, width, QImage::Format_RGB32);
QRegion rg(left, top, width, height);
QPainter painter(&image);
w->page()->view()->render(&painter, QPoint(), rg);
painter.end();
image.save("test.png", "PNG", 80);
});
我正在使用 qt 5.6.2。我想在网页上截图。这是我使用的代码:
#include <QtWidgets/QApplication>
#include <QtWebEngineWidgets/QWebEngineView>
int main(int argc, char *argv[]){
int left, top, width, height;
left = 0;
top = 0;
width = 600;
height = 800;
QApplication a(argc, argv);
QWebEngineView* w = new QWebEngineView();
QImage image(height, width, QImage::Format_RGB32);
QRegion rg(left, top, width, height);
QPainter painter(&image);
w->page()->load(QUrl("https://www.yahoo.com"));
w->show();
w->page()->view()->render(&painter, QPoint(), rg);
painter.end();
image.save("test.png", "PNG", 80);
return a.exec();
}
我运行这个程序,出现一个window,我可以看到yahoo
的内容。然后我将结果保存到 test.png
但那是一张白色图片。是我无法将结果保存到 image
还是我无法将结果从 image
保存到文件 test.png
以及如何修复它?
您需要等待页面加载完成。
QWebEngineView* w = new QWebEngineView();
w->page()->load(QUrl("https://www.yahoo.com"));
w->show();
QObject::connect(w, &QWebEngineView::loadFinished,
[w](bool bOk)
{
int left, top, width, height;
left = 0;
top = 0;
width = 600;
height = 800;
QImage image(height, width, QImage::Format_RGB32);
QRegion rg(left, top, width, height);
QPainter painter(&image);
w->page()->view()->render(&painter, QPoint(), rg);
painter.end();
image.save("test.png", "PNG", 80);
});