Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Variants in external QML file not initialized when on changed handlers called

Variants in external QML file not initialized when on changed handlers called

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 819 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    megahurts
    wrote on last edited by
    #1

    This seems inconsistent behaviour, can someone please tell me why this breaks and how to get around it?

    Starting with a default qtquick 2 application I add the following to the top level Rectangle in main.qml:

    @ Item {
    id: inititem
    property real f0: 20
    }

    Rectangle {
        id: one
        width: parent.width
        height: 20
        color: "red"
    
        property var listt: [1, 2, 3]
        property real f0: inititem.f0
    
        onF0Changed: {
            console.log("one f0 changed")
            console.log(listt)
            console.log(width)
        }
    }
    
    Two {
        id: two
        f0: inititem.f0
    }@
    

    and in the file Two.qml have:

    @import QtQuick 2.0

    Rectangle {
    width: parent.width
    height: 20
    color: "blue"

    property var listt: [1, 2, 3]
    property real f0: 0
    
    onF0Changed: {
        console.log("two f0 changed")
        console.log(listt)
        console.log(width)
    }
    

    }@

    To me the items with id "one" and "two" should be identical, except two is moved to an external file to help organize the code. However, after running the program I get the output:

    bq. two f0 changed
    undefined
    0
    one f0 changed
    [1,2,3]
    360

    Why is the variant list in the external QML file (and it's width) undefined when f0 is changed, while everything works when I keep it in a single QML file? How can I get around this?

    Thanks

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chrisadams
      wrote on last edited by
      #2

      Hi,

      You're printing that out in a signal handler for a property change signal. That means that if two.f0 is initialized before two.listt and two.width then you are seeing expected behaviour.

      Remember, QML is declarative, so the order of property initialization is not defined by the order it appears in the code (that would be imperative, not declarative).

      You should wait until the Component.onCompleted signal is emitted - at that point, all of the property initializations are guaranteed to be complete.

      Cheers,
      Chris.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved