首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存QWebEngineHistory并将其加载到QWebEnginePage

保存QWebEngineHistory并将其加载到QWebEnginePage
EN

Stack Overflow用户
提问于 2019-12-03 10:51:01
回答 1查看 193关注 0票数 1

我需要保存QWebEnginePage的历史记录并重新加载它。因此,我想将历史记录从A页存储在某种结构中,并将其设置为B页。

在文档中,我发现了以下方法:

代码语言:javascript
复制
// Saves the web engine history history into stream.
QDataStream &operator<<(QDataStream &stream, const QWebEngineHistory &history)

// Loads the web engine history from stream into history.
QDataStream &operator>>(QDataStream &stream, QWebEngineHistory &history)

但老实说我不知道怎么跟他们合作。我尝试了以下几点:

代码语言:javascript
复制
QWebEnginePage *m_history;
...
...
void setHistory(QWebEngineHistory *history){
   QDataStream data;
   data << history; //Hoping that the content of data is persistent after deleting of the QWebEnginePage where the history is coming from
   data >> m_history;
}

稍后,我想将它加载回页面:

代码语言:javascript
复制
m_history >> m_webEnginePage.history(); // Pseudo-Code

我知道QWebEngineHistory of a QWebEnginePage是const,但是我想知道为什么上面有这两种方法呢?为什么会有“将web引擎历史载入历史”的功能?

我能想到的唯一选择是将我的历史记录存储在QList中,但是管理它并不好,可能会导致更多的问题(因为整个向前/向后按钮等等)。

非常感谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-03 11:13:08

无法保存任何对象,所保存的是与对象关联的信息,因此不应创建QWebEngineHistory,而应保存和/或加载信息。

在下面的示例中,当应用程序关闭并加载启动时,信息将保存在文件中。

代码语言:javascript
复制
#include <QtWebEngineWidgets>

int main(int argc, char *argv[]) {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc,argv);

    const QString filename = "history.bin";

    QWebEngineView view;
    view.load(QUrl("https://stackoverflow.com"));

    {// load
        QFile file(filename);
        if(file.open(QFile::ReadOnly)){
            qDebug() << "load";
            QDataStream ds(&file);
            ds >> *(view.page()->history());
        }
    }  

    view.resize(640, 480);
    view.show();

    int ret = app.exec();

    {// save
        QFile file(filename);
        if(file.open(QFile::WriteOnly)){
            qDebug() << "save";
            QDataStream ds(&file);
            ds << *(view.page()->history());
        }
    }  

    return ret;
}

同样,您可以通过QSettings保存它:

代码语言:javascript
复制
#include <QtWebEngineWidgets>

int main(int argc, char *argv[]) {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication app(argc,argv);

    QWebEngineView view;
    view.load(QUrl("https://stackoverflow.com"));

    {// load
        QSettings settings;
        QByteArray ba = settings.value("page/history").toByteArray();
        QDataStream ds(&ba, QIODevice::ReadOnly);
        ds >> *(view.page()->history());
    }  

    view.resize(640, 480);
    view.show();

    int ret = app.exec();

    {// save
        QSettings settings;
        QByteArray ba;
        QDataStream ds(&ba, QIODevice::WriteOnly);
        ds << *(view.page()->history());
        settings.setValue("page/history", ba);
    } 

    return ret;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59155399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档