[Development] Modifying QQmlFileSelector selector at runtime without Loader or Component

Alan Alpert 416365416c at gmail.com
Tue Dec 16 06:41:26 CET 2014


On Fri, Dec 12, 2014 at 8:29 PM, <achartier at fastmail.fm> wrote:
>
> Hello,
>
> I would like to modify the extraSelectors of QQmlFileSelector at runtime
> and have QML load the new QML file from the appropriate selector folder.
> I have gotten this to work by calling setExtraSelectors() from a C++
> object I have exposed to QML and then forcing a re-evaluation of the
> Loader's source property:
>
> Loader {
>      id: myLoader
>      source: Qt.resolvedUrl("MyItem.qml")
> }
>
> Connections {
>      target: myCppObject
>      onSelectorChanged: myLoader.source = Qt.resolvedUrl("MyItem.qml")
> }
>
> Now, for my use case I really need the ability to achieve the same
> behavior without using Loader or Component. I essentially want something
> like this:
>
> MyItem { }
>
> Connections {
>      target: myCppObject
>      onSelectorChanged: // when selector changes, the MyItem instance
>      above is automatically resolved to the MyItem definition in the
>      appropriate selector folder.
>
> How can this be accomplished?

This cannot be accomplished (I actually tried last year). The problem
is that you have an actual instance of MyItem there, and there's no
way to magically map it into another instance. You have to destroy
that instance and bring up a new one - which basically means Loader.

What you probably want is to have certain parts of the instance shift,
but not destroy the whole instance. For that I recommend using QtQuick
States. You can have a base state, and then a list of property changes
to things within the instance in a sub state and use transitions to
animate it. That shifts the parts of the instance that need to,
without destroying the whole thing. Or save state in another object
(such as in a QtObject{} on the Loader) and bind your internal state
to that so that you can use the Loader approach to recreate whole
instances.

PS: Moving to qt-interest since you didn't seem to be offering to
develop this feature as a part of Qt.

--
Alan Alpert



More information about the Development mailing list