Qt Property initialization, inconsistent behaviour object and literal

  • I am using 2 Text components and intializing the text property on both. The first one is
    initialized using qsTr("Text 1") and the other one using "Text 2". The output of this is

    Output (The text i can see on the screen):

    Text 1 changed

    Text 2

    I was expecting that either no onChanged would be called or both. Is there anyone here
    that can explain to me why this is happening?

    import QtQuick 1.1

    Rectangle {
    width: 360
    height: 360

    Text {
        id: lblText1;
        text: qsTr("Text 1")
        onTextChanged: {
            lblText1.text = "Text 1 changed";
        id: lblText2;
        text: "Text 2"
        x: 0
        y: 50
        onTextChanged: {
            lblText2.text = "Text 2 changed";
    MouseArea {
        anchors.fill: parent
        onClicked: {




  • lblText1 is binding to a function, the result of which cannot be known until the runtime starts processing proper. This happens much later, i.e. when the event loop is entered.

    lblText2 is binding to a constant expression. The QML runtime has no need to re-evaluate this at a later time since it is known to be constant.

    To make lblText2 behave like lblText1 you could bind the text property to a javascript function that just returns the string.

Log in to reply

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