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

2024-03-21

我正在将网页加载到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();
    });

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

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


Edit: minimal reproducible code:
#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 https://developer.mozilla.org/es/docs/Web/API/Element/outerHTML文档的使用runJavaScript() https://doc.qt.io/qt-5/qwebenginepage.html#runJavaScript-2的方法QWebEnginePage https://doc.qt.io/qt-5/qwebenginepage.html:

#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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在QWebEngineView中保存动态生成的网页? 的相关文章

  • 结构化绑定中缺少类型信息

    我刚刚了解了 C 中的结构化绑定 但有一件事我不喜欢 auto x y some func is that auto正在隐藏类型x and y 我得抬头看看some func的声明来了解类型x and y 或者 我可以写 T1 x T2 y
  • 在模板类中声明模板友元类时出现编译器错误

    我一直在尝试实现我自己的链表类以用于教学目的 我在迭代器声明中指定了 List 类作为友元 但它似乎无法编译 这些是我使用过的 3 个类的接口 Node h define null Node
  • C# 异步等待澄清?

    我读了here http blog stephencleary com 2012 02 async and await html that 等待检查等待的看看它是否有already完全的 如果 可等待已经完成 那么该方法将继续 运行 同步
  • 根据属性的类型使用文本框或复选框

    如果我有这样的结构 public class Parent public string Name get set public List
  • 通过引用传递 [C++]、[Qt]

    我写了这样的东西 class Storage public Storage QString key const int value const void add item QString int private QMap
  • 如何在 Cassandra 中存储无符号整数?

    我通过 Datastax 驱动程序在 Cassandra 中存储一些数据 并且需要存储无符号 16 位和 32 位整数 对于无符号 16 位整数 我可以轻松地将它们存储为有符号 32 位整数 并根据需要进行转换 然而 对于无符号 64 位整
  • std::list 线程push_back、front、pop_front

    std list 线程安全吗 我假设不是这样 所以我添加了自己的同步机制 我认为我有正确的术语 但我仍然遇到问题 每个函数都由单独的线程调用 Thread1 不能等待 它必须尽可能快 std list
  • 如何从 Visual Studio 将视图导航到其控制器?

    问题是解决方案资源管理器上有 29 个项目 而且项目同时具有 ASP NET MVC 和 ASP NET Web 表单结构 在MVC部分中 Controller文件夹中有大约100个子文件夹 每个文件夹至少有3 4个控制器 视图完全位于不同
  • std::vector 与 std::stack

    有什么区别std vector and std stack 显然 向量可以删除集合中的项目 尽管比列表慢得多 而堆栈被构建为仅后进先出的集合 然而 堆栈对于最终物品操作是否更快 它是链表还是动态重新分配的数组 我找不到关于堆栈的太多信息 但
  • 为什么 GCC 不允许我创建“内联静态 std::stringstream”?

    我将直接前往 MCVE include
  • 如何从本机 C(++) DLL 调用 .NET (C#) 代码?

    我有一个 C app exe 和一个 C my dll my dll NET 项目链接到本机 C DLL mynat dll 外部 C DLL 接口 并且从 C 调用 C DLL 可以正常工作 通过使用 DllImport mynat dl
  • 用于 FTP 的文件系统观察器

    我怎样才能实现FileSystemWatcherFTP 位置 在 C 中 这个想法是 每当 FTP 位置添加任何内容时 我都希望将其复制到我的本地计算机 任何想法都会有所帮助 这是我之前问题的后续使用 NET 进行选择性 FTP 下载 ht
  • WPF 数据绑定到复合类模式?

    我是第一次尝试 WPF 并且正在努力解决如何将控件绑定到使用其他对象的组合构建的类 例如 如果我有一个由两个单独的类组成的类 Comp 为了清楚起见 请注意省略的各种元素 class One int first int second cla
  • 人脸 API DetectAsync 错误

    我想创建一个简单的程序来使用 Microsoft Azure Face API 和 Visual Studio 2015 检测人脸 遵循 https social technet microsoft com wiki contents ar
  • C# 列表通用扩展方法与非通用扩展方法

    这是一个简单的问题 我希望 集合类中有通用和非通用方法 例如List
  • WcfSvcHost 的跨域异常

    对于另一个跨域问题 我深表歉意 我一整天都在与这个问题作斗争 现在已经到了沸腾的地步 我有一个 Silverlight 应用程序项目 SLApp1 一个用于托管 Silverlight SLApp1 Web 的 Web 项目和 WCF 项目
  • LINQ:使用 INNER JOIN、Group 和 SUM

    我正在尝试使用 LINQ 执行以下 SQL 最接近的是执行交叉联接和总和计算 我知道必须有更好的方法来编写它 所以我向堆栈团队寻求帮助 SELECT T1 Column1 T1 Column2 SUM T3 Column1 AS Amoun
  • C++ 中的 include 和 using 命名空间

    用于使用cout 我需要指定两者 include
  • DotNetZip:如何提取文件,但忽略zip文件中的路径?

    尝试将文件提取到给定文件夹 忽略 zip 文件中的路径 但似乎没有办法 考虑到其中实现的所有其他好东西 这似乎是一个相当基本的要求 我缺少什么 代码是 using Ionic Zip ZipFile zf Ionic Zip ZipFile
  • 从 mvc 控制器使用 Web api 控制器操作

    我有两个控制器 一个mvc控制器和一个api控制器 它们都在同一个项目中 HomeController Controller DataController ApiController 如果我想从 HomeController 中使用 Dat

随机推荐