Regarding how to find the children



  • Hi,

    i have a VBox and HBox layout, widgets are added to Hbox, later align using VBox,
    how can i find children.

    Code below:

    m_hbox->addWidget(addKeys(tr("Q"),characters));
       m_hbox->addWidget(addKeys(tr("W"),characters));
       m_hbox->addWidget(addKeys(tr("E"),characters));
       m_hbox->addWidget(addKeys(tr("R"),characters));
       m_hbox->addWidget(addKeys(tr("T"),characters));
       m_hbox->addWidget(addKeys(tr("Y"),characters));
       m_hbox->addWidget(addKeys(tr("U"),characters));
       m_hbox->addWidget(addKeys(tr("I"),characters));
       m_hbox->addWidget(addKeys(tr("O"),characters));
       m_hbox->addWidget(addKeys(tr("P"),characters));
       m_hbox->addWidget(addKeys("backspaceicon",backspace_icon));
    
       m_vbox->insertLayout(0,m_hbox);
    
    
       m_hbox1->addWidget(addKeys(tr("A"),characters));
       m_hbox1->addWidget(addKeys(tr("S"),characters));
       m_hbox1->addWidget(addKeys(tr("D"),characters));
       m_hbox1->addWidget(addKeys(tr("F"),characters));
       m_hbox1->addWidget(addKeys(tr("G"),characters));
       m_hbox1->addWidget(addKeys(tr("H"),characters));
       m_hbox1->addWidget(addKeys(tr("J"),characters));
       m_hbox1->addWidget(addKeys(tr("K"),characters));
       m_hbox1->addWidget(addKeys(tr("L"),characters));
       m_hbox1->addWidget(addKeys("entericon",enter_icon));
    
       m_vbox->insertLayout(1,m_hbox1);
    
    
       m_hbox2->addWidget(addKeys("capsicon",caps_icon));
       m_hbox2->addWidget(addKeys(tr("Z"),characters));
       m_hbox2->addWidget(addKeys(tr("X"),characters));
       m_hbox2->addWidget(addKeys(tr("C"),characters));
       m_hbox2->addWidget(addKeys(tr("V"),characters));
       m_hbox2->addWidget(addKeys(tr("B"),characters));
       m_hbox2->addWidget(addKeys(tr("N"),characters));
       m_hbox2->addWidget(addKeys(tr("M"),characters));
       m_hbox2->addWidget(addKeys(tr(","),characters));
       m_hbox2->addWidget(addKeys(tr("."),characters));
       m_hbox2->addWidget(addKeys("capsicon",caps_icon));
    
    
       m_vbox->insertLayout(2,m_hbox2);
    
    
       m_hbox3->addWidget(addKeys("&123",numbers));
       m_hbox3->addWidget(addKeys("multilingual",multilingual_icon));
       m_hbox3->addWidget(addKeys("spacebar",spacebar));
       m_hbox3->addWidget(addKeys(",",characters));
       m_hbox3->addWidget(addKeys(":-)",characters));
       m_hbox3->addWidget(addKeys("close",close_icon));
    
       m_vbox->insertLayout(3,m_hbox3);
    
    
       this->setLayout(m_vbox);
    

    how to find children ?.

    If i try to use below code, getting zero as list size.

    QVBoxLayout * layout = m_vbox->findChild<QVBoxLayout *> ();
      QList<QWidget *> list = layout->findChildren<QWidget *> ();
    
      qDebug() <<  "list size of children present :>" << list.size() << endl;
    

    Thanks,



  • QLayout is a QObject, not a QWidget. the items you are searching for are not children of QLayout but of m_vbox directly

    QList<QWidget *> list = m_vbox->findChildren<QWidget *> ();



  • used
    QList<QWidget *> list = m_vbox->findChildren<QWidget *> ();

    getting zero as list size.:(

    Thanks,



  • @Pradeep-Kumar
    Hi,

    If you want to get all the widgets inside the layout try this,

    QList<QWidget *> list;
    for (int i = 0; i < lyt->count(); ++i)
    {
    QWidget *widget = lyt->itemAt(i)->widget();
    if (widget != NULL)
    {
    list.append(widget);
    }
    }



  • Just want to know all the children present in Vbox.

    Thanks,



  • @Pradeep-Kumar said in Regarding how to find the children:

    Just want to know all the children present in Vbox.

    what do you mean?



  • meaning i want to find children present in vbox. as i provided code above.

    Thanks,



  • @Pradeep-Kumar ,

    for(int i = 0; i < m_VBox->count(); i++) {
        qDebug() << Q_FUNC_INFO << "No of child widgets: " << m_VBox->itemAt(i)->layout()->count() << endl;
        int count = m_VBox->itemAt(i)->layout()->count();
        for(int j = 0; j < count; j++) {
            qDebug() << Q_FUNC_INFO << "Widget at:" << m_VBox->itemAt(i)->layout()->itemAt(j)->widget()->objectName() << endl;
        }
    }


  • Hi,

    thanks all, it worked and i am getting the child present in the vbox layout.

    Thanks,


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.