QML.jl
QML.jl copied to clipboard
Exposing `QQmlEngine::clearComponentCache` for live reloading
While developing UI features in QML for my application, I got frustrated with the current workflow because it requires restarting the QML application to see changes. This is exacerbated by my window manager, who decides to place the created application randomly between different screens.
A solution to this would be implementing live reloading. I have been following the approach outlined in https://qml.guide/live-reloading-hot-reloading-qml. The technique primarily involves using Loader extensively to reload the components dynamically.
However, a major obstacle with this approach is related to how QML manages caching. QML caches loaded objects based on their filenames, meaning reloading the same file doesn’t reflect any changes made to it. To update the cache and see the changes, the component cache needs to be cleared first. Unfortunately, the necessary function QQmlEngine::clearComponentCache() is not currently exposed.
I also tried to make a workaround by adding a url parameter to the cache like:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Loader {
id: _loader
function reload() {
var time = new Date().getTime();
source = "";
source = "Circle.qml?nocache=" + time;
}
anchors.centerIn: parent
source: "Circle.qml"
}
MouseArea {
anchors.fill: parent
onClicked: {
_loader.reload();
}
}
}
which worked but did not reload components on which the circle itself depended; thus, it’s a dead end.
If QQmlEngine::clearComponentCache
would be exposed by QML, I could implement a qmlfunction Julia.clearCache()
, which would allow me to integrate live reloading in my workflow. Tracking QML source files and updating the view would be something I could also explore if the clearComponentCache
would be exposed.
The clearComponentCache
approach is also used in qhot
which seems to be activelly. maintained as can be seen here.
Thanks, I share your frustration and didn't know about this solution. Exposing clearComponentCache
should be easy, I'll take a look.
Thanks for suggesting this, it is now sort of working, to be demoed tomorrow at JuliaCon ;)