最近项目从Qt5迁移到Qt6,有若干不兼容或者变动的地方,虽谈不上什么血泪史,但是一天内做了几十处的兼容性处理(好在项目不大),也是挺累人的。 本文重点说说QWebEngineView 咋就默认变成无痕处理模式了,如何咱们如何取消无痕模式,自定义设置缓存路径。
Profiles can be used to isolate pages from each other. A typical use case is a dedicated off-the-record profile for a private browsing mode. Using QWebEngineProfile() without defining a storage name constructs a new off-the-record profile that leaves no record on the local machine, and has no persistent data or cache. The isOffTheRecord() method can be used to check whether a profile is off-the-record.
先读懂这段话。QWebEngineProfile对象在构造的时候,如果不指定storage的名称,那么默认构造一个off-the-record 的profile对象,页面的cache和data将保存在缓存中,而不会在页面关闭时持久化到磁盘。 反过来说,只要我们对页面指定一个独一无二的profile,自然能在页面退出时将页面数据持久化。
*QWebEngineProfile::QWebEngineProfile(const QString &storageName, QObject parent = nullptr) Constructs a new profile with the storage name storageName and parent parent. The storage name must be unique. A disk-based QWebEngineProfile should be destroyed on or before application exit, otherwise the cache and persistent data may not be fully flushed to disk. See also storageName().
简单举个使用例子:
#include "widget.h"
#include <QWebEngineView>
#include <QApplication>
#include <QUrl>
#include <QDebug>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QWebEnginePage>
#include <webview.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebEngineView* view = new QWebEngineView;
QWebEngineProfile profile(a.applicationName());
QWebEnginePage page(&profile);
view->setPage(&page);
QString _storageName = profile.storageName();
QString cacheName = profile.cachePath();
qDebug() << __FUNCTION__ << __LINE__ << _storageName << cacheName;
QUrl url("https://www.iqiyi.com/v_1fzx7cxrde0.html?vfrm=pcw_home&vfrmblk=D&vfrmrst=712211_focus_A_image3");
view->setUrl(url);
view->load(url);
view->show();
return a.exec();
}
效果如上,无需每次都登录,可记住登录账号等信息~~
反过来,如果有朋友需要设置默认无痕模式,那么在Qt6的版本上使用,直接用默认的profile则行;Qt5上,与上使用类似
QWebEnginePage page();
就这样,简单记录下
附Qt6 在webEngine的变化点: https://doc-snapshots.qt.io/qt6-dev/qtwebengine-changes-qt6.html
|