如何捕获 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 渲染的图像?的主要内容,如果未能解决你的问题,请参考以下文章