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

时间:2020-04-21 11:14:11

标签: qwebengineview qwebenginepage

感谢您看到:当Dialog隐藏时,QPixmap为空,为什么? 而且这种方式效率太低...帮助我

1 个答案:

答案 0 :(得分:0)

、、、

#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;
}

、、、