栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Qt6.2.3 QWebEngineView 咋就默认无痕浏览模式了?

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Qt6.2.3 QWebEngineView 咋就默认无痕浏览模式了?

最近项目从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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebEngineView* view = new QWebEngineView;
    QWebEngineProfile profile(a.applicationName()); // here's important

    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(); // 不指定storage名称

就这样,简单记录下

附Qt6 在webEngine的变化点:
https://doc-snapshots.qt.io/qt6-dev/qtwebengine-changes-qt6.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/762985.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号