The order of property initialization of a QML component


  • Qt Champions 2016

    Hi,

    Anyone knows the rule of the order of property initialization of a QML component? I am writing a GUI application with style engine that allow to change the style of a GUI component.

    For example ,

    main.qml

    @
    WidgetA {
    style.fieldA: 3
    }
    @

    WidgetA.qml
    @
    Rectangle {
    property StyleA style : Style.widgetAStyle ; // Style is a singleton object with default style information.
    }
    @

    StyleA.qml
    @
    QtObject {
    property int fieldA : 0;
    }
    @

    However, sometimes QML will complain : "Cannot set properties on style as it is null" in the main.qml. But sometimes it won't . So I am very confused that why the Widget.style property is not initialized when I use it. Anyone know the rule? I can't find any document talk about that.



  • Sorry, i don't know the rule eather, but there is a QML Analysis tool which you could use to check which objects were created first. You can find it on the left side of your Qt Creator directly below Projects. Just run your QML Application using this tool.


  • Qt Champions 2016

    Hi onek24,

    Thanks for your reply. But how the profiler can tell you the sequence of creation? Check the timeline?

    By the way , I found a dirty hack to fix my problem:

    A widget:
    @
    property TextInputStyle style : Style.theme.textInput
    @

    That is my original code. "Style" is a singleton object shared by all widget to provide the default style information. But it will complains "Cannot set properties on style as it is null” in the main.qml for setting custom style for the instance.

    This piece of code fix the problem:

    @
    property TextInputStyle style : TextInputStyle {
    background: Style.theme.textInput.background
    textStyle: TextStyle {
    textSize: Style.theme.textInput.textStyle.textSize
    textColor: Style.theme.textInput.textStyle.textColor
    }
    textSelectHandle: Style.theme.textInput.textSelectHandle
    }
    @

    I guess one of the factor to determine the order is the "depth" of component. Nested component will go first. And simple variable assignment will go later.



  • Hey benlau,

    bq. Thanks for your reply. But how the profiler can tell you the sequence of creation? Check the timeline?

    Yes, just check the timeline. it describes a lot.

    bq. I guess one of the factor to determine the order is the “depth” of component. Nested component will go first. And simple variable assignment will go later.

    I would say the same, i'll look into it if i find some time today.

    Keep us up to date if you found out something new :)


  • Qt Champions 2016

    [quote author="onek24" date="1417697726"]Hey benlau,
    I would say the same, i'll look into it if i find some time today.

    Keep us up to date if you found out something new :)[/quote]

    well , It is better to let Qt developer to declare an official document about the order. Otherwise, what we did is just a dirty hack .. In fact, I have encountered the problem of initalization sequence for several times already.


Log in to reply
 

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