Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing QQmlEngine::clearComponentCache for live reloading #195

Open
JanisErdmanis opened this issue Apr 15, 2024 · 3 comments
Open

Exposing QQmlEngine::clearComponentCache for live reloading #195

JanisErdmanis opened this issue Apr 15, 2024 · 3 comments

Comments

@JanisErdmanis
Copy link

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.

@JanisErdmanis
Copy link
Author

The clearComponentCache approach is also used in qhot which seems to be activelly. maintained as can be seen here.

@barche
Copy link
Collaborator

barche commented Apr 17, 2024

Thanks, I share your frustration and didn't know about this solution. Exposing clearComponentCache should be easy, I'll take a look.

barche added a commit to JuliaGraphics/jlqml that referenced this issue Apr 27, 2024
barche added a commit to JuliaGraphics/jlqml that referenced this issue Apr 30, 2024
barche added a commit that referenced this issue May 1, 2024
Issue Exposing `QQmlEngine::clearComponentCache` for live reloading #195
barche added a commit that referenced this issue Jun 18, 2024
Issue Exposing `QQmlEngine::clearComponentCache` for live reloading #195
@barche barche mentioned this issue Jun 18, 2024
@barche
Copy link
Collaborator

barche commented Jul 11, 2024

Thanks for suggesting this, it is now sort of working, to be demoed tomorrow at JuliaCon ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants