我使用的是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();
}
我运行程序,出现一个窗口,我可以看到yahoo
的内容。然后我将结果保存到test.png
,但这是一张白色图片。我是否无法将结果保存到image
,或者我无法将image
的结果保存到文件test.png
以及如何修复它?
答案 0 :(得分:1)
您需要等待页面完成加载。
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);
});