Skip to content

Commit

Permalink
Add QML file watching and reloading
Browse files Browse the repository at this point in the history
Issue Exposing `QQmlEngine::clearComponentCache` for live reloading #195
  • Loading branch information
barche committed Jun 18, 2024
1 parent 8dc510d commit 10d55ca
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/QML.jl
Original file line number Diff line number Diff line change
@@ -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!
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 10d55ca

Please sign in to comment.