[英]Clearing WebView Cache from withing QML
I open website with QDeclarativeView and use JavaScript to load next pages in same view. 我使用QDeclarativeView打开网站,并使用JavaScript在同一视图中加载下一页。
After each website loaded, my program occupy 20mb more of memory. 加载每个网站后,我的程序将占用20mb以上的内存。 How do i clean the cache or otherwise release the memory after new website is loaded?
加载新网站后,如何清理缓存或释放内存?
I tried: 我试过了:
decView->engine()->rootContext()->setContextProperty("myEngine", decView->engine());
decView-> engine()-> rootContext()-> setContextProperty(“ myEngine”,decView-> engine());
and then in qml 然后在qml中
myEngine.clearComponentCache()
myEngine.clearComponentCache()
but i get 但我明白了
TypeError: Result of expression 'myEngine.clearComponentCache' [undefined] is not a function.
TypeError:表达式'myEngine.clearComponentCache'的结果[未定义]不是函数。
What i should do? 我该做什么?
EDIT: here is what i got sofar: 编辑:这是我得到沙发的东西:
aws.cpp 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 firstqml.qml
import QtQuick 1.1
import QtWebKit 1.0
WebView {
id: webView
objectName: "myWebView"
url:"http://example.com"
onLoadFinished: {myAws.clearCacheQMLSlot();}
}
There two reasons why your code doesn't work as intended. 您的代码无法按预期运行的原因有两个。 First, to be able to access slots and invokable methods of
QObject
descendants, you have to register them: 首先,要能够访问
QObject
后代的插槽和可调用方法,必须注册它们:
qmlRegisterType<QDeclarativeEngine>("MyApp", 1, 0, "QDeclarativeEngine");
And second, QDeclarativeEngine::clearComponentCache
is neither a slot nor an invokable method , so it would still not work. 其次,
QDeclarativeEngine::clearComponentCache
既不是插槽也不是可调用的方法 ,因此它仍然无法正常工作。 It is simply impossible to call normal C++ methods from QML. 从QML调用普通的C ++方法根本是不可能的。
What you actually have to do is to implement an own QObject
based class wrapping the call to QDeclarativeEngine::clearComponentCache
in a slot, registering the class like above, set an instance of that class as an context property like you did with the declarative engine and finally call the slot from QML. 您实际上要做的是实现一个自己的基于
QObject
的类,将对QDeclarativeEngine::clearComponentCache
的调用包装在插槽中,像上面那样注册该类,将该类的实例设置为上下文属性,就像使用声明式引擎所做的那样,并最后从QML调用广告位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.