【问题标题】:Clearing WebView Cache from withing QML从 QML 中清除 WebView 缓存
【发布时间】:2012-12-21 14:44:19
【问题描述】:

我使用 QDeclarativeView 打开网站并使用 JavaScript 在同一视图中加载下一页。

每个网站加载后,我的程序会多占用 20mb 内存。新网站加载后如何清理缓存或释放内存?

我试过了:

decView->engine()->rootContext()->setContextProperty("myEngine", decView->engine());

然后在qml中

myEngine.clearComponentCache()

但我明白了

TypeError:表达式“myEngine.clearComponentCache”[undefined] 的结果不是函数。

我该怎么办?

编辑:这是我到目前为止得到的:
aws.cpp

void Aws::openQMLWindowSlot(){
   QDeclarativeView *decView= new QDeclarativeView();
   decView->engine()->rootContext()->setContextProperty("myAws",this);
   decView->setSource(QUrl("qrc:/inc/firstqml.qml"));
   decView->show();
}

void Aws::clearCacheQMLSlot(){

//HERE I GOT PROBLEM
}

firstqml.qml

import QtQuick 1.1
import QtWebKit 1.0
WebView {

    id: webView
    objectName: "myWebView"
    url:"http://example.com"
    onLoadFinished: {myAws.clearCacheQMLSlot();}
}

【问题讨论】:

  • 这里有我们gc() javascript中的方法,你试过了吗?
  • 是的,我试过 webView.evaluateJavaScript("gc();");和 gc(),都没有结果。

标签: c++ qt qml


【解决方案1】:

您的代码无法按预期工作的原因有两个。首先,为了能够访问QObject后代的插槽和可调用方法,您必须register它们:

qmlRegisterType<QDeclarativeEngine>("MyApp", 1, 0, "QDeclarativeEngine");

其次,QDeclarativeEngine::clearComponentCache 既不是插槽也不是invokable method,所以它仍然无法工作。从 QML 调用普通的 C++ 方法是根本不可能的。

您实际上需要做的是实现一个自己的基于QObject 的类,将对QDeclarativeEngine::clearComponentCache 的调用包装在一个槽中,像上面一样注册类,将该类的实例设置为上下文属性,就像您所做的那样声明式引擎,最后从 QML 调用插槽。

【讨论】:

  • 感谢您的回答。我在我创建 decView 的同一位置使用decView-&gt;engine()-&gt;rootContext()-&gt;setContextProperty("myAws",this); 设置“this”,因此所有公共插槽都暴露给 qml 文件,但我如何调用 QDeclarativeEngine::clearComponentCachein那个插槽?
  • 只需将指向声明性视图或指向类中引擎的指针存储为成员变量,然后在其上调用方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 2016-10-10
相关资源
最近更新 更多