@@ -88,6 +88,9 @@ Load a QML file, creating a [`QML.QQmlApplicationEngine`](@ref), and setting the
8888"""
8989function loadqml (qmlfilename; kwargs... )
9090 qml_engine = init_qmlapplicationengine ()
91+ return loadqml (qml_engine, qmlfilename; kwargs... )
92+ end
93+ function loadqml (qml_engine,qmlfilename; kwargs... )
9194 ctx = root_context (CxxRef (qml_engine))
9295 for (key,value) in kwargs
9396 set_context_property (ctx, String (key), value)
@@ -208,6 +211,7 @@ function Base.iterate(s::QString, i::Integer=1)
208211end
209212Base. convert (:: Type{<:QString} , s:: String ) = QString (s)
210213QString (u:: QUrl ) = toString (u)
214+ @cxxdereference Base. show (io:: IO , u:: QUrl ) = print (io, toString (u))
211215
212216# QByteArray
213217Base. convert (:: Type{QByteArray} , s:: AbstractString ) = QByteArray (s)
@@ -310,6 +314,14 @@ Base.iterate(h::QMap, state::QMapIterator) = _qmap_iteration_tuple(h, iteratorne
310314Base. values (h:: QMap ) = QML. values (h)
311315Base. keys (h:: QMap ) = QML. keys (h)
312316
317+ # QTimer helper
318+ function QTimer (f, interval)
319+ timer = QTimer ()
320+ setInterval (timer, interval)
321+ callOnTimeout (timer, f)
322+ start (timer)
323+ end
324+
313325# Helper to call a julia function
314326function julia_call (f, argptr:: Ptr{Cvoid} )
315327 arglist = CxxRef {QVariantList} (argptr)[]
@@ -409,17 +421,26 @@ JuliaPropertyMap(dict::Dict{<:AbstractString,<:Any}) = JuliaPropertyMap(dict...)
409421
410422@cxxdereference value (:: Type{JuliaPropertyMap} , qvar:: QVariant ) = getpropertymap (qvar)
411423
412- const _queued_properties = []
413-
414424# Functor to update a QML property when an Observable is changed in Julia
415425struct QmlPropertyUpdater
416426 propertymap:: QQmlPropertyMap
417427 key:: String
418428 active:: Bool
419429end
430+
431+ const _queued_properties = Dict {QmlPropertyUpdater,Any} ()
432+ const _queue_lock = ReentrantLock ()
433+ _called_update = false ;
434+
420435function (updater:: QmlPropertyUpdater )(x)
421436 if Base. current_task () != Base. roottask
422- push! (_queued_properties, (updater, x))
437+ @lock _queue_lock begin
438+ _queued_properties[updater] = x
439+ if ! _called_update
440+ queue_process_eventloop_updates ()
441+ global _called_update = true
442+ end
443+ end
423444 return
424445 end
425446 updater. propertymap[updater. key] = x
@@ -445,6 +466,10 @@ macro deferredcall(expr)
445466 $ (esc (expr))
446467 else
447468 put! (_deferred_calls, () -> $ (esc (expr)))
469+ @lock _queue_lock if ! _called_update
470+ queue_process_eventloop_updates ()
471+ global _called_update = true
472+ end
448473 nothing
449474 end
450475 end
@@ -669,6 +694,7 @@ function Base.displayable(d::JuliaDisplay, mime::AbstractString)
669694end
670695
671696include (" itemmodel.jl" )
697+ include (" imageprovider.jl" )
672698
673699function exec ()
674700 # We redirect to the Core stdout/err in case threading is used
@@ -687,7 +713,21 @@ function exec()
687713 return
688714end
689715
716+ # Runs all observable and model updates that need to be done on the main event loop
717+ # Updates are cached here when they were made from a task different from the Julia root task
718+ function process_eventloop_updates ()
719+ @lock _queue_lock begin
720+ for (updater, x) in _queued_properties
721+ updater. propertymap[updater. key] = x
722+ end
723+ empty! (_queued_properties)
724+ run_deferred_calls ()
725+ global _called_update = false
726+ end
727+ end
728+
690729function exec_async ()
730+ global _called_update = true # No need to queue event loop updates to the main thread
691731 lastdisplay = popdisplay ()
692732 if VERSION >= v " 1.12-"
693733 newrepl = @async Base. run_main_repl (true ,true ,:yes ,true )
@@ -701,11 +741,8 @@ function exec_async()
701741 pushdisplay (lastdisplay)
702742
703743 while ! istaskdone (newrepl)
704- for (updater, x) in _queued_properties
705- updater. propertymap[updater. key] = x
706- end
707- empty! (_queued_properties)
708- run_deferred_calls ()
744+ process_eventloop_updates ()
745+ global _called_update = true
709746 process_events ()
710747 sleep (0.015 )
711748 end
0 commit comments