I am using qt 5.6.2. I would like to take a screenshot on a web page. Here is the code I am using:
#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();
}
I run the program, a window shows up, I can see the content of yahoo
. I then save the result to test.png
but that is a white picture. Is it I cannot save the result to image
or I cannot save the result from image
to file test.png
and how to fix it?
You need to wait for the page to finish loading.
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);
});