gpt4 book ai didi

qt - QWebView缓存

转载 作者:行者123 更新时间:2023-12-04 19:48:25 26 4
gpt4 key购买 nike

我正在 QT 5.2 中编写一个简单的 WebView 应用程序。我正在使用的网站有点重,因此我需要缓存它。但问题是,当单击页面并访问子页面时,我收到了 QIODevice::read: device not open 错误。最后,我得到了一张空白的白页。我哪里出错了?

代码如下:

WebsiteWidget::WebsiteWidget(QWidget *parent) :
QWidget(parent)
{
m_url = QUrl("http://www.google.com/");
QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
QWebSettings::globalSettings()->setAttribute(QWebSettings::JavascriptEnabled, true);
QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);

m_websiteContentLayout = new QHBoxLayout();
m_webView = new QWebView(this);

QNetworkAccessManager* manager = new QNetworkAccessManager(this);
QNetworkDiskCache* diskCache = new QNetworkDiskCache(this);

QString location = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
diskCache->setCacheDirectory(location);
manager->setCache(diskCache);
m_webView->page()->setNetworkAccessManager(manager);
m_webView->page()->settings()->setMaximumPagesInCache(10);

QNetworkRequest request = QNetworkRequest();
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
request.setUrl(m_url);

connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
manager->get(request);

m_websiteContentLayout->addWidget(m_webView);
m_websiteContentLayout->setContentsMargins(0, 0, 0, 0);
this->setLayout(m_websiteContentLayout);
}

void WebsiteWidget::replyFinished(QNetworkReply* reply)
{
QByteArray data=reply->readAll();
QString str(data);
if(reply->url() == m_url)
{
m_webView->setHtml(str, reply->url());
}
}

最佳答案

在我制作之后,我已经让离线存储工作了:

webView->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);webView->settings()->enablePersistentStorage(QDir::homePath());

其他设置(setOfflineStorageDefaultQuota 等)似乎是可选的。(有人可能想将 homePath 更改为更好的东西,如 tmpPath 等)

关于qt - QWebView缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20616064/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com