Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Rectangle width is '0' if i try to access in "Component.OnCompleted"

Rectangle width is '0' if i try to access in "Component.OnCompleted"

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqtquick2qt5.9.1rect
8 Posts 3 Posters 2.7k Views
  • 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.
  • P Offline
    P Offline
    pra7
    wrote on 10 Aug 2017, 11:29 last edited by
    #1

    Hello all i want to get the width of a rectangle in "Component.OnCompleted" .Following is my code:

    main.qml

    import QtQuick 2.6
    import QtQuick.Controls 2.2
    import QtQuick.Window 2.3
    
    ApplicationWindow {
        id: appWindow
        visible: true
        width: 600
        height: 400
        title: qsTr("test")
        flags:  Qt.Window | Qt.FramelessWindowHint
        
        MyRect{
            id:rect
        }
    }
    

    MyRect.qml

    import QtQuick 2.6
    import QtQuick.Controls 2.2
    
    Rectangle{
            id:rectParent
            anchors.fill:parent
            Rectangle{
                id:rectChild
                width:parent.width * 0.75
                height: parent.height * 0.70
                Component.onCompleted: {
                    console.log("Width=",width) //prints "0" .
                    //code to create component follows  and i need the width.
                }
            }
        }
    

    What is the approach that i should use to create a component on completion? i can't use timers.

    E 1 Reply Last reply 10 Aug 2017, 11:44
    0
    • O ODБOï
      10 Aug 2017, 13:39

      Replace 'parent' by parents id

      /** MyRec.qml **/
      import QtQuick 2.6

      Rectangle{
      id:rectParent

          color: "grey"
          property alias _recChildW : rectChild
      
      Rectangle{
              id:rectChild
              width:rectParent.width * 0.75  // parent id
              height: rectParent.height * 0.70 //   parent id
      
          }
      
      
      }
      

      /Main/
      import QtQuick 2.7
      import QtQuick.Controls 2.0
      import QtQuick.Layouts 1.3

      ApplicationWindow {
      id:root
      visible: true
      width: 640
      height: 480
      title: qsTr("Hello World")

          MyRec{
                    id: customRec
                  height:root.height // here : parent id
                  width:root.width   // here  : parent id
      
                  Component.onCompleted:  console.log("width " + _recChildW.width) 
      
              }
      

      }

      P Offline
      P Offline
      pra7
      wrote on 10 Aug 2017, 17:19 last edited by
      #8

      @LeLev Thanks , I will try the same .

      1 Reply Last reply
      1
      • P pra7
        10 Aug 2017, 11:29

        Hello all i want to get the width of a rectangle in "Component.OnCompleted" .Following is my code:

        main.qml

        import QtQuick 2.6
        import QtQuick.Controls 2.2
        import QtQuick.Window 2.3
        
        ApplicationWindow {
            id: appWindow
            visible: true
            width: 600
            height: 400
            title: qsTr("test")
            flags:  Qt.Window | Qt.FramelessWindowHint
            
            MyRect{
                id:rect
            }
        }
        

        MyRect.qml

        import QtQuick 2.6
        import QtQuick.Controls 2.2
        
        Rectangle{
                id:rectParent
                anchors.fill:parent
                Rectangle{
                    id:rectChild
                    width:parent.width * 0.75
                    height: parent.height * 0.70
                    Component.onCompleted: {
                        console.log("Width=",width) //prints "0" .
                        //code to create component follows  and i need the width.
                    }
                }
            }
        

        What is the approach that i should use to create a component on completion? i can't use timers.

        E Offline
        E Offline
        Eeli K
        wrote on 10 Aug 2017, 11:44 last edited by
        #2

        @pra7 Have you tried moving the code to rectParent's Component.onCompleted?

        P 1 Reply Last reply 10 Aug 2017, 13:06
        0
        • O Offline
          O Offline
          ODБOï
          wrote on 10 Aug 2017, 12:47 last edited by ODБOï 8 Oct 2017, 12:48
          #3

          Hi,
          There is an concatenation error in 'MyRect.qml' :

          Component.onCompleted: {
                   
                                   console.log("Width=",width) //prints "0" .  //  <<< Error here
                     
                  }
          

          Should be :

          Component.onCompleted: {

                                   console.log("Width=" + width) //prints "0" .  //  '+'  and not   ' , '               
                  }
          

          LA

          P 1 Reply Last reply 10 Aug 2017, 13:05
          0
          • O ODБOï
            10 Aug 2017, 12:47

            Hi,
            There is an concatenation error in 'MyRect.qml' :

            Component.onCompleted: {
                     
                                     console.log("Width=",width) //prints "0" .  //  <<< Error here
                       
                    }
            

            Should be :

            Component.onCompleted: {

                                     console.log("Width=" + width) //prints "0" .  //  '+'  and not   ' , '               
                    }
            

            LA

            P Offline
            P Offline
            pra7
            wrote on 10 Aug 2017, 13:05 last edited by
            #4

            @LeLev That doesn't make any difference

            1 Reply Last reply
            0
            • E Eeli K
              10 Aug 2017, 11:44

              @pra7 Have you tried moving the code to rectParent's Component.onCompleted?

              P Offline
              P Offline
              pra7
              wrote on 10 Aug 2017, 13:06 last edited by
              #5

              @Eeli-K Even there it's the same .. I am getting width has '0'

              1 Reply Last reply
              0
              • O Offline
                O Offline
                ODБOï
                wrote on 10 Aug 2017, 13:09 last edited by
                #6

                Oh sorry, i just tested console.log("Width=",width) and it works. My bad.

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  ODБOï
                  wrote on 10 Aug 2017, 13:39 last edited by ODБOï 8 Oct 2017, 13:41
                  #7

                  Replace 'parent' by parents id

                  /** MyRec.qml **/
                  import QtQuick 2.6

                  Rectangle{
                  id:rectParent

                      color: "grey"
                      property alias _recChildW : rectChild
                  
                  Rectangle{
                          id:rectChild
                          width:rectParent.width * 0.75  // parent id
                          height: rectParent.height * 0.70 //   parent id
                  
                      }
                  
                  
                  }
                  

                  /Main/
                  import QtQuick 2.7
                  import QtQuick.Controls 2.0
                  import QtQuick.Layouts 1.3

                  ApplicationWindow {
                  id:root
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Hello World")

                      MyRec{
                                id: customRec
                              height:root.height // here : parent id
                              width:root.width   // here  : parent id
                  
                              Component.onCompleted:  console.log("width " + _recChildW.width) 
                  
                          }
                  

                  }

                  P 1 Reply Last reply 10 Aug 2017, 17:19
                  1
                  • O ODБOï
                    10 Aug 2017, 13:39

                    Replace 'parent' by parents id

                    /** MyRec.qml **/
                    import QtQuick 2.6

                    Rectangle{
                    id:rectParent

                        color: "grey"
                        property alias _recChildW : rectChild
                    
                    Rectangle{
                            id:rectChild
                            width:rectParent.width * 0.75  // parent id
                            height: rectParent.height * 0.70 //   parent id
                    
                        }
                    
                    
                    }
                    

                    /Main/
                    import QtQuick 2.7
                    import QtQuick.Controls 2.0
                    import QtQuick.Layouts 1.3

                    ApplicationWindow {
                    id:root
                    visible: true
                    width: 640
                    height: 480
                    title: qsTr("Hello World")

                        MyRec{
                                  id: customRec
                                height:root.height // here : parent id
                                width:root.width   // here  : parent id
                    
                                Component.onCompleted:  console.log("width " + _recChildW.width) 
                    
                            }
                    

                    }

                    P Offline
                    P Offline
                    pra7
                    wrote on 10 Aug 2017, 17:19 last edited by
                    #8

                    @LeLev Thanks , I will try the same .

                    1 Reply Last reply
                    1

                    7/8

                    10 Aug 2017, 13:39

                    • Login

                    • Login or register to search.
                    7 out of 8
                    • First post
                      7/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved