5

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 ?

today
  • 32,602
  • 8
  • 95
  • 115
iAmoric
  • 1,787
  • 3
  • 31
  • 64

1 Answers1

10

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;
}
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85