Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Qt Property initialization, inconsistent behaviour object and literal

    General and Desktop
    2
    2
    1763
    Loading More Posts
    • 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.
    • S
      SorkBarn last edited by

      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";
          }
      }
      Text{
          id: lblText2;
          text: "Text 2"
          x: 0
          y: 50
      
          onTextChanged: {
              lblText2.text = "Text 2 changed";
          }
      }
      
      MouseArea {
          anchors.fill: parent
          onClicked: {
              Qt.quit();
          }
      }
      

      }
      @

      BR

      Mattias

      1 Reply Last reply Reply Quote 0
      • Z
        ZapB last edited by

        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.

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply Reply Quote 0
        • First post
          Last post