如何在QWebEngineView中保存动态生成的网页?

How to save dynamically generated web page in QWebEngineView?

我正在将网页加载到 QWebEngineView。用户创建了不同类型的表格(报告),然后需要将这些表格作为网页保存到本地计算机。这是我尝试过的:

  1. 这里我用了一个QWebEnginePage::save()方法,但是没有任何反应:

    connect(saveButton, &QPushButton::clicked, this, [this]()
    {
       engineWebView->page()->save("save.html");
    });
    
  2. 然后我尝试了一个QWebEngineProfile::download()方法:

    connect(saveButton, &QPushButton::clicked, this, [this]()
    {
        engineWebView->page()->download(engineWebView->page()->url(), "save");
    });
    connect(engineWebView->page()->profile(), &QWebEngineProfile::downloadRequested, this, [this](QWebEngineDownloadItem *download) 
    {
        download->setPath("save.html");
        download->accept();
    });

在第二种解决方案中,我只能保存第一个加载的网页。没有动态创建的内容。

如何保存动态创建的数据?


编辑:最小可重现代码:

#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QHBoxLayout>
#include <QPushButton>
#include <QWebEngineProfile>
#include <QWebEngineView>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWebEngineView *engine = new QWebEngineView;
    QObject::connect(engine->page()->profile(), &QWebEngineProfile::downloadRequested, [](QWebEngineDownloadItem *download) {
        download->setPath("download.html");
        download->accept();
    });

    QPushButton *saveButton = new QPushButton("Save");
    QObject::connect(saveButton, &QPushButton::clicked, [engine]()
    {
        engine->page()->save("save.html");
    });

    QPushButton *toHtmlButton = new QPushButton("ToHtml");
    QObject::connect(toHtmlButton, &QPushButton::clicked, [engine]()
    {
        engine->page()->toHtml([](QString html){
        QFile file("toHtml.html");
        if (file.open(QFile::WriteOnly | QFile::Text))
        {
            QTextStream stream(&file);
            stream << html;
            file.waitForBytesWritten(-1);
            file.close();
        }
        else
            qDebug() << "Cannot create a file";
        });
    });

    QPushButton *downloadButton = new QPushButton("Download");
    QObject::connect(downloadButton, &QPushButton::clicked, [engine]()
    {
        engine->page()->download(engine->page()->url());
    });

    QHBoxLayout *hLyt = new QHBoxLayout;
    hLyt->addWidget(saveButton);
    hLyt->addWidget(toHtmlButton);
    hLyt->addWidget(downloadButton);

    QVBoxLayout *vLyt = new QVBoxLayout;
    vLyt->addLayout(hLyt);
    vLyt->addWidget(engine);

    QWidget *mainWin = new QWidget;
    mainWin->setLayout(vLyt);
    mainWin->show();

    // The url is an example for react usage. I am generating data using ReactJS that's why I use this example. What I need is to store the exact view of the dynamically generated calculator        
    engine->load(QUrl("https://ahfarmer.github.io/calculator/"));

    return app.exec();
}

如果你想获得动态生成的 html 你可以使用 javascript 来获得 outerHTML of the document using the runJavaScript() method of QWebEnginePage:

#include <QtWebEngineWidgets>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWebEngineView *view = new QWebEngineView;
    QPushButton *button = new QPushButton("Press me");
    button->setEnabled(false);
    QObject::connect(button, &QPushButton::clicked, [view]()
    {
        view->page()->runJavaScript("document.documentElement.outerHTML", [](const QVariant &v) { 
            QFile file("outerHTML.html");
            if(!file.open(QFile::WriteOnly | QFile::Text)){
                qDebug() << "Cannot create a file";
                return;
            }
            QTextStream stream(&file);
            stream << v.toString();
            file.close();
        });
    });
    QObject::connect(view, &QWebEngineView::loadFinished, [button](bool ok){
        button->setEnabled(ok);
    });
    view->load(QUrl("https://ahfarmer.github.io/calculator/"));
    QWidget w;
    QVBoxLayout *lay = new QVBoxLayout(&w);
    lay->addWidget(button);
    lay->addWidget(view);
    w.resize(640, 480);
    w.show();
    return app.exec();
}