我需要保存QWebEnginePage的历史记录并重新加载它。因此,我想将历史记录从A页存储在某种结构中,并将其设置为B页。
在文档中,我发现了以下方法:
// 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)但老实说我不知道怎么跟他们合作。我尝试了以下几点:
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;
}稍后,我想将它加载回页面:
m_history >> m_webEnginePage.history(); // Pseudo-Code我知道QWebEngineHistory of a QWebEnginePage是const,但是我想知道为什么上面有这两种方法呢?为什么会有“将web引擎历史载入历史”的功能?
我能想到的唯一选择是将我的历史记录存储在QList中,但是管理它并不好,可能会导致更多的问题(因为整个向前/向后按钮等等)。
非常感谢你的帮助。
发布于 2019-12-03 11:13:08
无法保存任何对象,所保存的是与对象关联的信息,因此不应创建QWebEngineHistory,而应保存和/或加载信息。
在下面的示例中,当应用程序关闭并加载启动时,信息将保存在文件中。
#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保存它:
#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;
}https://stackoverflow.com/questions/59155399
复制相似问题