iAmoric
iAmoric

Reputation: 1975

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 ?

Upvotes: 5

Views: 3458

Answers (1)

Adrian Maire
Adrian Maire

Reputation: 14855

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;
}

Upvotes: 10

Related Questions