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. how to find children length of a window
Forum Updated to NodeBB v4.3 + New Features

how to find children length of a window

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 2.7k Views 1 Watching
  • 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.
  • ashajgA Offline
    ashajgA Offline
    ashajg
    wrote on last edited by ashajg
    #1

    Hi guys

    I want to find length of children in a window

    In my main.qml I am writing a function

    function getGroupRef()
      {
          var length=parent.children.length
          console.log("MaInWiNdOw children length---->"+length)
      }
    

    Main.qml

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick 2.9
    import "qrc:/MasterController.js" as Controll
    
    Window {
        id:mainWindow
        visible: true
    
        width: 800
        height: 600
        title: qsTr("Hello World")
    
    
            Component.onCompleted:
            {
                console.log ("triggered")
                Controll.createGroup();
               
            }
    
    
    
    
    
        function getGroupRef()
        {
            var length=mainWindow.children.length
            console.log("MaInWiNdOw children length---->"+length)
        }
    
        MouseArea
        {
            anchors.fill:parent
            onClicked:getGroupRef()
        }
    
    }
    

    MasterController.js

    var sprite
    var groupComponent;
    var groupComponentTwo;
    var ventGroupContainer;
    var barButtonContainer;
    var myList=[];
    var myCompNames=["ventbutmod","qkeyWidget1","qkeyWidget2","qkeyWidget3","qkeyWidget4","moreset","buttonBar0","buttonBar1","buttonBar2"];
    
    
    
    
    function createGroup()
    {
        console.log("inside Create Group")
    
        groupComponent = Qt.createComponent("qrc:/VentGroup.qml");
        groupComponentTwo=Qt.createComponent("qrc:/ButtonBarGroup.qml");
        if(groupComponent.status === Component.Ready && groupComponentTwo.status === Component.Ready){
            finishGroupCreation();}
    
        else
        {
            groupComponent.statusChanged.connect(finishGroupCreation);
            groupComponentTwo.statusChanged.connect(finishGroupCreation)
        }
    
    }
    
    function finishGroupCreation()
    {
        console.log("inside finishgroupcreation")
          barButtonContainer=groupComponentTwo.createObject(mainWindow)
        ventGroupContainer=groupComponent.createObject(mainWindow)
    
    }
    
    ERROR:
    
    qrc:/main.qml:47: TypeError: Cannot read property 'length' of undefined
    

    In this program I am creating two components using .js . when I am trying to fetch children length of mainWindow which should be 2(ventgroup and buttonBargroup). I am getting error .
    How can I solve this?

    raven-worxR 1 Reply Last reply
    0
    • ashajgA ashajg

      Hi guys

      I want to find length of children in a window

      In my main.qml I am writing a function

      function getGroupRef()
        {
            var length=parent.children.length
            console.log("MaInWiNdOw children length---->"+length)
        }
      

      Main.qml

      import QtQuick 2.9
      import QtQuick.Window 2.2
      import QtQuick 2.9
      import "qrc:/MasterController.js" as Controll
      
      Window {
          id:mainWindow
          visible: true
      
          width: 800
          height: 600
          title: qsTr("Hello World")
      
      
              Component.onCompleted:
              {
                  console.log ("triggered")
                  Controll.createGroup();
                 
              }
      
      
      
      
      
          function getGroupRef()
          {
              var length=mainWindow.children.length
              console.log("MaInWiNdOw children length---->"+length)
          }
      
          MouseArea
          {
              anchors.fill:parent
              onClicked:getGroupRef()
          }
      
      }
      

      MasterController.js

      var sprite
      var groupComponent;
      var groupComponentTwo;
      var ventGroupContainer;
      var barButtonContainer;
      var myList=[];
      var myCompNames=["ventbutmod","qkeyWidget1","qkeyWidget2","qkeyWidget3","qkeyWidget4","moreset","buttonBar0","buttonBar1","buttonBar2"];
      
      
      
      
      function createGroup()
      {
          console.log("inside Create Group")
      
          groupComponent = Qt.createComponent("qrc:/VentGroup.qml");
          groupComponentTwo=Qt.createComponent("qrc:/ButtonBarGroup.qml");
          if(groupComponent.status === Component.Ready && groupComponentTwo.status === Component.Ready){
              finishGroupCreation();}
      
          else
          {
              groupComponent.statusChanged.connect(finishGroupCreation);
              groupComponentTwo.statusChanged.connect(finishGroupCreation)
          }
      
      }
      
      function finishGroupCreation()
      {
          console.log("inside finishgroupcreation")
            barButtonContainer=groupComponentTwo.createObject(mainWindow)
          ventGroupContainer=groupComponent.createObject(mainWindow)
      
      }
      
      ERROR:
      
      qrc:/main.qml:47: TypeError: Cannot read property 'length' of undefined
      

      In this program I am creating two components using .js . when I am trying to fetch children length of mainWindow which should be 2(ventgroup and buttonBargroup). I am getting error .
      How can I solve this?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @ashajg
      Window is not a visual QML item neither does it have a children property thus the error.

      I haven't tested the following but i think it's closer to what you try to achieve:

      var length=mainWindow.contentItem.children.length
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • ashajgA Offline
        ashajgA Offline
        ashajg
        wrote on last edited by
        #3

        @raven-worx said in how to find children length of a window:

        var length=mainWindow.contentItem.children.length

        hi

        this line var length=mainWindow.contentItem.children.length is giving me answer as 3.
        What is contentItem ?

        Actually I am trying to fetch Objectnames of Ventgroup.qml and ButtonBargroup.qml

        something like this

        function getReference(objectName)
        {
            var length = mainWindow.children.length
            console.log("length of children is "+length)
            for(var i=0;i<length;i++)
            {
                if(objectName===mainWindow.children[i].objectName)
                {
                    console.log("FOUND");
                    return mainWindow.children[i];
        
                }
            }
        }
        

        basically I am trying to fetch mainWindow.children[i]; so that I can manuplate properties of this group from my .js
        any idea?

        raven-worxR 1 Reply Last reply
        1
        • ashajgA ashajg

          @raven-worx said in how to find children length of a window:

          var length=mainWindow.contentItem.children.length

          hi

          this line var length=mainWindow.contentItem.children.length is giving me answer as 3.
          What is contentItem ?

          Actually I am trying to fetch Objectnames of Ventgroup.qml and ButtonBargroup.qml

          something like this

          function getReference(objectName)
          {
              var length = mainWindow.children.length
              console.log("length of children is "+length)
              for(var i=0;i<length;i++)
              {
                  if(objectName===mainWindow.children[i].objectName)
                  {
                      console.log("FOUND");
                      return mainWindow.children[i];
          
                  }
              }
          }
          

          basically I am trying to fetch mainWindow.children[i]; so that I can manuplate properties of this group from my .js
          any idea?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @ashajg said in how to find children length of a window:

          What is contentItem ?

          the contentItem is the visual item holding the child items in the window

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • ashajgA Offline
            ashajgA Offline
            ashajg
            wrote on last edited by
            #5

            ok

            I was able to get it like this

            ```
            
            function getGroupRef(groupName)
                {
                    var length=mainWindow.contentItem.children.length
            
                    console.log("MaInWiNdOw children length---->"+length)
                    for(var i=0;i<length;i++)
                    {
                        if(mainWindow.contentItem.children[i].objectName===groupName)
                        {
                        console.log("found at :"+i)
                            return mainWindow.contentItem.children[i];
                        }
                    }
                   // console.log("MaInWiNdOw children name---->"+mainWindow.contentItem.children[2].objectName)
                }
            
            J.HilkJ 1 Reply Last reply
            0
            • ashajgA ashajg

              ok

              I was able to get it like this

              ```
              
              function getGroupRef(groupName)
                  {
                      var length=mainWindow.contentItem.children.length
              
                      console.log("MaInWiNdOw children length---->"+length)
                      for(var i=0;i<length;i++)
                      {
                          if(mainWindow.contentItem.children[i].objectName===groupName)
                          {
                          console.log("found at :"+i)
                              return mainWindow.contentItem.children[i];
                          }
                      }
                     // console.log("MaInWiNdOw children name---->"+mainWindow.contentItem.children[2].objectName)
                  }
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @ashajg
              great, that it's now working for you.

              Don't forget to set the topic to solved, via topic tools.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • ashajgA Offline
                ashajgA Offline
                ashajg
                wrote on last edited by
                #7

                yes
                Thank you!!!

                1 Reply Last reply
                0

                • Login

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