0

I'm trying to render a HTML to Image without show on screen because this app will run in a linux console, but application always write e empty image. I'm using Qt 6.4.

webviewrender.cpp

#include "webviewrender.h"

WebViewRender::WebViewRender()
{
    this->view = new QWebEngineView;
}
WebViewRender::WebViewRender(QString html)
{
    this->scene = new QGraphicsScene;
    this->screen = new QGraphicsView(this->scene);
    this->view = new QWebEngineView;
    this->scene->addWidget(this->view);
    this->view->setHtml(html);

    this->view->show();
    connect(this->view,SIGNAL(loadFinished(bool)),this,SLOT(saveImage()));

}
QByteArray WebViewRender::convertToArray()
{
    QImage image(this->view->size(),QImage::Format_ARGB32);
    QPainter painter(&image);
    this->view->render(&painter);
    painter.end();
    image.save("D:\\teste.jpeg");
    return this->convertFromQImage(image);
}

QByteArray WebViewRender::convertFromQImage(QImage &image)
{
    QByteArray arr;
    QBuffer buffer(&arr);
    buffer.open(QIODevice::WriteOnly);
    image.save(&buffer,"jpeg");
    return arr;
}

bool WebViewRender::saveImage()
{
    QByteArray arr = this->convertToArray();
    return true;
}

webviewrender.h

#ifndef WEBVIEWRENDER_H
#define WEBVIEWRENDER_H
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QWebEngineView>
#include <QPainter>
#include <QBuffer>

class WebViewRender: public QObject
{
    Q_OBJECT
private:
    QGraphicsScene *scene;
    QGraphicsView *screen;
    QWebEngineView *view;
    QString html;
    QByteArray convertFromQImage(QImage &image);
public:
    WebViewRender();
    WebViewRender(QString html);
    bool setHtml(QString html);
    QByteArray convertToArray();
private slots:
    bool saveImage();

};

#endif // WEBVIEWRENDER_H

main.cpp

#include <QApplication>
#include "webviewrender.h"
#include <QFile>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString s;
    QFile file("D:\\html.txt");
    file.open(QIODevice::ReadOnly);
    QTextStream s1(&file);
    s.append((s1.readAll()));


    WebViewRender render(s);
    return a.exec();
}

I tried using QGraphicsView, and without using it either. I'm using now Qt on windows, and compile with MSVC 2019.

  • Could you try by adding `this->view->setAttribute(Qt::WA_DontShowOnScreen);` – Pamputt Mar 24 '23 at 17:05
  • Does it work if you do make the widget visible? Also, it uses the Chromium engine, not just Qt, so that can be a source of issues with things. – hyde Mar 24 '23 at 17:20
  • i have tried with this this->view->setAttribute(Qt::WA_DontShowOnScreen);, and with visible widget too, without success :( – João Paulo Masiero Mar 24 '23 at 18:59

0 Answers0