[Qt-qml] Component loading in background thread?
Cornelius Hald
hald at icandy.de
Mon Nov 22 10:44:01 CET 2010
On Mon, 2010-11-22 at 01:15 +0100, warwick.allison at nokia.com wrote:
> > I'm exposing a complex QGraphicsWidget to QML and the problem is, that
> > whenever I load that component my application freezes for a second (on
> > desktop, on mobile it's much longer).
> >
> > I'd like to display a lightweight 'loading' animation and load that
> > component in the background without freezing the interface. Then
> > whenever it's ready it should replace the animation with the real
> > content.
>
> There is no all-encompassing answer to this. Threading is not directly provided in QML object instantiation (it would make simple constructions way to slow).
Ok.
> Firstly, do you REALLY need to do all the construction you are doing? One second on the desktop is an inordinately huge amount of time. Perhaps a different approach is needed. For example, if you loaded an entire company addressbook at start-up, that might be better done per-record as needed.
I probably should have provided more information about my use case. The
problem is, I'm not in control of that widget. I'm subclassing the
QGraphicsGeoMap from Qt Mobility to add some functionality, however the
long initialization comes from that widget. Currently I've only added a
couple of convenience methods.
Also maybe 1 second was a bit exaggerated. Still it's enough to mess
with the animation that's happening in parallel. I'm sliding in a new
view while loading the map.
> Can you thread the internals of your class? For example, if you did need to load a huge addressbook, you could do that in a thread and have a "progress" property and "onLoaded" signal emitted as it progresses. This keeps the threading in the C++ side (QThread is your friend), while using QML's inherent asynchronicity on the QML side. The Image elements effectively work this way (though the actual threading is in QDeclarativePixmap).
I maybe could add threading functionality to my subclass of
QGraphicsGeoMap so I could show a brief loading-animation until the
initialization has completed. I'll give that a try.
My current workaround is to use a timer to postpone the loading by one
second so it does not mess with the animation. Still if the user happens
to click something during the initialization he won't get direct
feedback.
Thanks for you answer, I'll have a look at QThread and see what I can
do.
Cheers,
Conny
More information about the Qt-qml
mailing list