Get the list of all QPushButton in Qt
Asked Answered
S

1

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 ?

Subtlety answered 16/2, 2017 at 10:31 Comment(2)
This is included in the documentation.Ephrayim
Oh thanks! I'm sorry for this stupid question...Subtlety
A
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;
}
Aramen answered 16/2, 2017 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.