Property Alias and runtime loaded QML-Items



  • Hello everyone,

    I came across something I'm not quite sure how to handle and hopefully someone can shine some light on this.

    The situation:
    Currently I design a couple differen small objects, each one consists mainly of a StackView for management and basic animation. The StackView than loads the appropriate Display and Input Objects.

    This is a simplyfied structure:

    Item {
        id:root
        property alias obj1Alias: obj1.text
        property alias obj2Alias: obj2.value
        property alias obj3Alias: obj3.value
    
        StackView{
            id: view
            anchors.fill:parent
            focus: true
    
            initialItem: Obj1{id: obj1; onClicked: view.push(obj2)}
    
            Obj2{id:obj2;onClicked: view.push(obj3)}
            Obj3{id:obj3;onClicked: view.push(obj1)}
         }
    }
    

    I do this to propagate the changed values/texts to the parent that than does stuff with the values and ultimately updates a c++ class.

    My Question:

    • I've been told, to increase performance one should use view.push(Qt.resolvedUrl("MyObject.qml")). But I'm not sure how I would make an property alias to a dynamically loaded qml-file.

  • Moderators

    @J.Hilk said in Property Alias and runtime loaded QML-Items:

    I've been told, to increase performance one should use view.push(Qt.resolvedUrl("MyObject.qml")). But I'm not sure how I would make an property alias to a dynamically loaded qml-file.

    Do you experience any performance issues? If no, they you can safely disregard that hint. The performance improvement in question comes from dynamically allocating the QML component when push() is called, instead of constructing it at application startup. But there are other ways to achieve same goal, too (using a Loader, for example, which should allow you to keep an alias).

    If yes, I would first look into optimizing the way you use the stack - I think in Obj3 you should pop() back to obj1 instead of pushing it again. I'm not sure, though.

    Regarding your question: you may be able to get the alias to work using the parameters dictionary described in the docs http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html#push-method I'm not sure if it will work, though.



  • Hi @sierdzio ,thanks for the answer.

    Do you experience any performance issues?

    In this case, not jet. But I thought, If I'm basically learning a "new language" than I better avoid adopting bad habits, if I can.
    It's something that was said during the QTWS17 by Jesper Pedersen, I thought he ought to know, what he's talking about x)

    But there are other ways to achieve the same goal, too (using a Loader, for example, which should allow you to keep an alias).

    thanks I'll look into the Loader a bit more. I know it exists and you can use it to sequentially load your UI to make it accessibility faster, but thats about it.

    I think in Obj3 you should pop() back to obj1 instead of pushing it again

    You're of course correct, it was an abstract example of what I'm trying to do. I would use

    Obj3{id:obj3;onClicked: view.pop(obj1)}
    

    to get back down to the first obj.

    I'm however not sure what you mean with parameters dictionary.


  • Moderators

    @J.Hilk said in Property Alias and runtime loaded QML-Items:

    I'm however not sure what you mean with parameters dictionary.

    Oh sorry, wrong word :-) I've meant this from the link I provided:

    properties: a list of QML properties that should be assigned to the item upon push. These properties will be copied into the item when it is loaded (in case of a component or URL), or when it becomes the current item for the first time (normally upon push).

    Another possibility would be to use some global context property (added in C++) which your whole app can access. Then you don't need to worry about when and how elements are loaded.

    In this case, not jet. But I thought, If I'm basically learning a "new language" than I better avoid adopting bad habits, if I can.

    It is not necessarily a bad habit. It is a trade-off:

    • you load all QML code at application startup - startup is longer (slightly - parsing QML is extremely fast), then all works fast at runtime (or possibly it instantiates components on push(). It definitely does that if you encapsulate ObjX in Component, I'm not sure if it also applies to the way you've done it)
    • or you load by Loader / URL at runtime - startup is faster, but then you will need some time to load the QML file at runtime when user wants to access given stack page

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.