【双开QWebEngine程序,后一次程序加载网页很慢】
双开QWebEngine程序,后一次程序加载网页很慢,提示Unable to create cache
分析过程:
1. 第一次与第二次加载相同网页相比,第一次网页加载只花了1s左右,但第二次加载网页时,进度80%~100%期间内,明显卡顿了10多秒,如下图。


2. 根据提示Unable to create cache
,有时候也会有Failed to delete the database: Database IO error
提示,怀疑是因为使用了同一个缓存路径导致的。
3. 对两次打开程序,分别设置不同的缓存路径,再次测试,发现后一次程序加载网页的速度和第一次基本一致,但仍然会稍微慢点,如下图。
解决方法:
通过函数QWebEngineProfile::setCachePath
,QWebEngineProfile::setPersistentStoragePath
设置不同的缓存路径和QWebEngine数据。例如:
QWebEngineProfile *profile = QWebEngineProfile::defaultProfile();
if (XXX) {
profile->setCachePath("xxx/cache1");
profile->setPersistentStoragePath("xxx/cache1/QtWebEngine");
} else {
profile->setCachePath("xxx/cache2");
profile->setPersistentStoragePath("xxx/cache2/QtWebEngine");
}