Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Window.width in a property



  • 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 ^^



  • @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.



  • 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)
    
    }
    


  • @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.



  • @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 
     }
    
    }
    
    


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


Log in to reply