【问题标题】:Qt WebEngineView render empty imageQt WebEngineView 渲染空图像
【发布时间】:2018-02-19 06:17:35
【问题描述】:

我使用的是 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 以及如何解决?

【问题讨论】:

    标签: qt qwebengineview


    【解决方案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);
    });
    

    【讨论】:

    • 感谢您的回复,我很挣扎。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2022-12-21
    • 2017-09-17
    • 2016-04-23
    相关资源
    最近更新 更多