Search code examples
c++qtqpushbuttonqradiobutton

Get the list of all QPushButton in Qt


I would like to get the list of all QPushButton in my MainWindow. Actually, I have a QRadioButton, and when I uncheck it, I would like to disable all the QPushButton of my window.

How can I do that ?


Solution

  • Here is a minimal example:

    #include <QApplication>
    #include <QWidget>
    #include <QPushButton>
    #include <QLabel>
    #include <QDebug>
    
    int main( int argn, char **argc)
    {
        QApplication app(argn, argc);
    
        // Creating some content
        QWidget window;
        QPushButton ba(&window); ba.setObjectName("but1");
        QPushButton bb(&window);bb.setObjectName("but2");
        QLabel l(&window); l.setObjectName("label");
        QPushButton bc(&l);bc.setObjectName("but3");
    
    
        // Getting all buttons
        QList<QPushButton *> butts = window.findChildren<QPushButton *>();
        qDebug() << butts.size();
    
        for (const auto *but: butts) qDebug() << "   " << but->objectName();
    
       return 0;
    }