metaObject()->className() doesn't find QLabel



  • Hello!

    I am using the following code to find all QLayouts within a main layout and then all widgets within those layouts.

    if(!allSubLayouts.empty())
    {
        int size = allSubLayouts.size();
        for(int i = 0; i < size; i ++)  // this loop hides all of the layouts that are currently present. If there are no layouts, nothing happens
        {
            for(int j = 0; j < allSubLayouts.at(i)->count(); ++j)
            {
    
                QWidget *w = allSubLayouts.at(i)->itemAt(j)->widget();
                if(w != NULL)
                {
                    qDebug() << "The className of the object is " ;
                    qDebug() << allSubLayouts.at(i)->itemAt(j)->widget()->metaObject()->className();
    
                    w->setVisible(false);
                    //delete w;
                }
            }
        }
    

    When I run the code, everything except the QLabel classnames are displayed in the qDebug statement.

    Why doesn't widget()->metaObject()->className(); produce QLabels?


  • Qt Champions 2016

    @mar0029
    It seems okay, but have you tried something simpler like QObject::findChildren?

    QList<QWidget *> items = findChildren<QWidget *>();
    foreach (QWidget * item, items)
        qDebug() << item->metaObject()->className();
    

    Kind regards.


Log in to reply
 

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