[Qt-qml] Getting a QML component to reload itself

Martin Jones martin.jones at nokia.com
Fri Sep 3 02:30:28 CEST 2010


On Fri, 3 Sep 2010 07:37:17 am ext Alex wrote:
> Hi all,
> 
> I have several QML screens managed by a C++ application.  In some
> cases, I need to reload a QML screen, in response to a signal emitted
> from that screen.  On the C++ side I have a slot that does this:
> 
> currentView->setSource(currentView->source());
> 
> If this slot is invoked by a signal emitted on the C++ side,
> everything works as expected.  However, if it is the QML screen itself
> that emits the signal, I get a crash.

The first thing setSource() does is clear the previously created object tree.  
If some object in that tree is calling the setSource(), then you've seen what 
happens...

> What is the correct way to do this?

You could create a different slot for the QML call that calls the actual 
reload slot asynchronously:

void MyClass::reload()
{
	QMetaObject::invokeMethod(this, "doReload", Qt::QueuedConnection);
}

void MyClass::doReload()
{
	currentView->setSource(currentView->source());
}


I'll investigate using deleteLater() instead so that this isn't a problem in 
future.

-- 
Martin



More information about the Qt-qml mailing list