如何捕获 QWebEnginePage::view 渲染的图像?

Posted

技术标签:

【中文标题】如何捕获 QWebEnginePage::view 渲染的图像?【英文标题】:How to capture the image rendered by QWebEnginePage::view? 【发布时间】:2020-04-21 11:14:11 【问题描述】:

谢谢大家看到:当Dialog有hide时,QPixmap是空的,为什么??? 而且这种方式效率太低了...帮帮我

【问题讨论】:

【参考方案1】:

、、、

#include "webKitDialog.h"
#include "ui_webKitDialog.h"
#include <QWebEngineView>
#include <QWebEnginePage>
#include "HiWebEnginePage.h"
#include <QTimer>
#include <QThread>
#include <QScreen>
#include <QGuiApplication>
#include <QPixmap>

webKitDialog::webKitDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::webKitDialog)

    ui->setupUi(this);

    m_view = new QWebEngineView(this);
    m_page = new HiWebEnginePage(m_view);
    m_view->setPage(m_page);

    ui->lineEdit->setText("http://www.youku.com");


    ui->verticalLayout->addWidget(m_view, 1);

    on_lineEdit_editingFinished();

    QTimer *time = new QTimer;
    connect(time, &QTimer::timeout, this, &webKitDialog::onTimeOut);
// 
    time->start(1000);


webKitDialog::~webKitDialog()

    delete ui;


void webKitDialog::showEvent(QShowEvent *event)

    m_view->reload();
    m_view->show();
    QDialog::showEvent(event);


void webKitDialog::onTimeOut()

   emit onImageUpdate(createThumbnail(m_view->size()));


void webKitDialog::on_pushButton_close_clicked()

    this->hide();
    m_view->showNormal();


void webKitDialog::on_pushButton_max_clicked()

    qreal factor = m_view->zoomFactor();
    factor += 0.25;
    if(factor > 5)
    
        factor = 5;
    
    m_page->setZoomFactor(factor);


void webKitDialog::on_pushButton_refresh_clicked()

    m_view->reload();


void webKitDialog::on_lineEdit_editingFinished()

    QString strUrl = ui->lineEdit->text();
    m_page->load(QUrl(strUrl));


void webKitDialog::on_pushButton_zoonout_clicked()

    qreal factor = m_view->zoomFactor();
    factor -= 0.25;
    if(factor < 0.25)
    
        factor = 0.25;
    
    m_page->setZoomFactor(factor);


QPixmap webKitDialog::createThumbnail(const QSize &size)

    QPixmap pixMap(size);

    qDebug() << "size" << size << endl;

    QRegion rg(0, 0, size.width(), size.height());

    QPainter painter(&pixMap);

    m_view->page()->view()->render(&painter, QPoint(), rg);

    painter.end();

    return pixMap;

、、、

【讨论】:

以上是关于如何捕获 QWebEnginePage::view 渲染的图像?的主要内容,如果未能解决你的问题,请参考以下文章

如何记录“捕获的”异常?

如何在 Promise 的回调中捕获未捕获的异常

如何让 pytest 不捕获异常

如何在 PHP 类型提示中捕获“可捕获的致命错误”?

JAVA捕获MYSQL主键冲突异常 示例如Exception ex 对应的异常 如何捕获,只要捕获,求满意答案

Android 中如何捕获未捕获的异常