Qt Forum

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

    Solved Window.width in a property

    QML and Qt Quick
    qml window
    2
    5
    235
    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.
    • M
      Moisi last edited by

      Hi,

      I have a QtObject. This object is use like a model to stock informations used by my application. Among this informations, I want to have the width of the Window item.

      But I'm not able to correctly stock the Window width in a property. When I try to read the property, I got the wrong value.

      Here is my code:

      Window
      {
      	visible: true
      	width: 1300
      	height: 800
      	title: qsTr("Tests")
      
      	Label { text: item.windowWidth; z: 5 }
      
      	property var model: QtObject
      	{
      		id: item
      		property int windowWidth: Window.width // 0
      	}
      }
      
      

      Expected value from the windowWidth property: 1300
      Current value: 0

      I can't understant why I can't have the right value in my property... Did I miss some information about the Window item ?

      Thank you ^^

      ODБOï 1 Reply Last reply Reply Quote 0
      • M
        Moisi last edited by

        @lelev said in Window.width in a property:

        u need to give an id to your Window item and then you can reach it by id

        Wow, it works perfectly, thank you a lot ^^

        Can I ask you why the id is mantatory ? Reading the Window Qml Type information, using Window.width should have work.

        ODБOï 1 Reply Last reply Reply Quote 0
        • ODБOï
          ODБOï @Moisi last edited by

          hi
          @moisi said in Window.width in a property:

          property int windowWidth: Window.width // 0

          you need to give an id to your Window item and then you can reach it by id

          Window {
              id:win
          
              visible: true
              width: 1300
              height: 480
          
              property var model: QtObject
                  {
                      id: item
                      property int windowWidth: win.width // 0
                  }
          
          
              Component.onCompleted: console.log(item.windowWidth)
          
          }
          
          1 Reply Last reply Reply Quote 1
          • M
            Moisi last edited by

            @lelev said in Window.width in a property:

            u need to give an id to your Window item and then you can reach it by id

            Wow, it works perfectly, thank you a lot ^^

            Can I ask you why the id is mantatory ? Reading the Window Qml Type information, using Window.width should have work.

            ODБOï 1 Reply Last reply Reply Quote 0
            • ODБOï
              ODБOï @Moisi last edited by ODБOï

              @moisi
              Window is the class name not he object itself,, you want to access a instance of it

              Window {
                Rectangle{
                  color : "red"
                }
                Rectangle{
                  color : "blue"
               }
              
               Component.onCompleted : {
                // how to change  first rectangle color  ?  Rectangle.color ? but which rectangle ?
               // solution is to give id to you rectangle so you can cible it 
               }
              
              }
              
              
              1 Reply Last reply Reply Quote 1
              • M
                Moisi last edited by

                Oh, yep, with this explaination is more clear. Thank you a lot ^^

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