what is the proper way of reloading qml file to QQuickView? I'm using Qt Quick 2.1 and trying to write a simple program that loads a qml file and displays it. Currently I'm doing it by creating a QQuickView and when i want to reload qml file i am deleting the old one and creating a new one. What is the proper way of doing this? calling QQuickView::setSource with new qml file (or changed qml file) didn't worked for me.
2 Answers
You can use the following (assuming you are in a subclass of QQuickView):
QUrl tmp = source();
setSource(QUrl());
engine()->clearComponentCache();
setSource(tmp);
-
it worked. So it turns out that if you use setSource with a file you already use before, Qt uses it's cache mechanism and doesn't care about the changes you made inside qml file. if you want QQuickView to reload the qml file you need to clear the QQmlEngine cache. That's really helpful, Thanks a lot.– ottoJun 28, 2013 at 6:28
-
That engine function appears from the docs to clear all the cache in the entire Qt Quick interface, which could be a problem if all you want is to reload an individual QQuickView item. Does that sound right? May 5, 2014 at 13:51
-
I'm using WebView at QML and wants to destroy all cookies and stored data. But engine()->clearComponentCache(); setSource(source()); doesn't help– RobotexJun 4, 2014 at 21:08
-
2I had to add
setSource(QUrl());before clearing component cache, or the main QML would not get properly reloaded. I edited the answer to do this as well (feel free to roll back if you feel this is too invasive edit).– hydeMay 11, 2015 at 8:44
You can do it his ways :
Create a main.qml (name can be anything) file, inside which, you will be actually loading and unloading other qml files.
Then use the qml loader element to load/unload (refresh if you may) any other file.
-
1Actually i've tried this and it didn't work, Because of the QQmlEngine's cache mechanism it doesn't care about the changes i made inside qml file. To Solve this i need to clear the cache the way described in the previous answer.– ottoJun 28, 2013 at 6:31