我正在将网页加载到QWebEngineView
. 用户创建不同类型的表格(报告),然后需要将这些表格作为网页保存到本地计算机。这是我尝试过的:
这里我使用了一个
QWebEnginePage::save()
方法,但是什么也没发生:connect(saveButton, &QPushButton::clicked, this, [this]() { engineWebView->page()->save("save.html"); });
然后我尝试了一个 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();
}