user891908
user891908

Reputation: 261

Clearing WebView Cache from withing QML

I open website with QDeclarativeView and use JavaScript to load next pages in same view.

After each website loaded, my program occupy 20mb more of memory. 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());

and then in qml

myEngine.clearComponentCache()

but i get

TypeError: Result of expression 'myEngine.clearComponentCache' [undefined] is not a function.

What i should do?

EDIT: here is what i got sofar:
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();}
}

Upvotes: 1

Views: 2664

Answers (1)

sebasgo
sebasgo

Reputation: 3851

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:

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

And second, QDeclarativeEngine::clearComponentCache is neither a slot nor an invokable method, so it would still not work. It is simply impossible to call normal C++ methods from QML.

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.

Upvotes: 1

Related Questions