From 10d55ca78f2f3e4873a0d287091288bddeead856 Mon Sep 17 00:00:00 2001 From: Bart Janssens Date: Wed, 1 May 2024 23:19:12 +0200 Subject: [PATCH] Add QML file watching and reloading Issue Exposing `QQmlEngine::clearComponentCache` for live reloading #195 --- src/QML.jl | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/QML.jl b/src/QML.jl index 6aa43e2..4d66cec 100644 --- a/src/QML.jl +++ b/src/QML.jl @@ -1,7 +1,7 @@ module QML export QVariant, QString, QUrl -export QQmlContext, root_context, loadqml, qt_prefix_path, set_source, engine, QByteArray, QQmlComponent, set_data, create, QQuickItem, content_item, QTimer, context_property, emit, JuliaDisplay, JuliaCanvas, qmlcontext, init_qmlapplicationengine, init_qmlengine, init_qquickview, exec, exec_async, QVariantMap +export QQmlContext, root_context, loadqml, watchqml, qt_prefix_path, set_source, engine, QByteArray, QQmlComponent, set_data, create, QQuickItem, content_item, QTimer, context_property, emit, JuliaDisplay, JuliaCanvas, qmlcontext, init_qmlapplicationengine, init_qmlengine, init_qquickview, exec, exec_async, QVariantMap export JuliaPropertyMap export QStringList, QVariantList export JuliaItemModel, addrole!, roles, roleindex, setgetter!, setsetter!, setheadergetter!, setheadersetter! @@ -102,6 +102,32 @@ function loadqml(qmlfilename; kwargs...) end end +function watchqml(engine::CxxPtr{QQmlApplicationEngine}, qmlfile) + function clearcache(path) + rootobject = first(QML.rootObjects(engine)) + QML.deleteLater(rootobject) + QML.clearComponentCache(engine) + QML.load_into_engine(engine, path) + end + + watcher = QML.QFileSystemWatcher(engine) + QML.addPath(watcher, qmlfile) + QML.connect_file_changed_signal(watcher, clearcache) +end + +function watchqml(qview::CxxPtr{QQuickView}, qmlfile) + engine = QML.engine(qview) + + function clearcache(path) + QML.clearComponentCache(engine) + set_source(qview, QUrlFromLocalFile(path)) + end + + watcher = QML.QFileSystemWatcher(engine) + QML.addPath(watcher, qmlfile) + QML.connect_file_changed_signal(watcher, clearcache) +end + const _loaded_qml_modules = Module[] function loadqmljll(m::Module)