How to map widgets created in QtCreator with an array?
-
Hello, I made a periodic table with QPushbuttons in QtCreator. And I want to map these buttons with an array, say *pb[115]. I used pb[1]=ui->hydrogen; pb[2]=ui->helium; But it takes tons of work and resulted in a long code. How can I do this elegantly?

-
Hello, I made a periodic table with QPushbuttons in QtCreator. And I want to map these buttons with an array, say *pb[115]. I used pb[1]=ui->hydrogen; pb[2]=ui->helium; But it takes tons of work and resulted in a long code. How can I do this elegantly?

Hi,
first, a
QVector< QPushButton * >or even astd::vector < QPushButton * >is always better than an array ofQPushButton.
Then, since your widgets are all buttons, you could use aQButtonGroup, because you created the actual button in Designer and probably only need a container to group them logically.
Maybe you also want to have a look atQHashorQMap, where you could store the chemical element as string and map it to your button. -
Hello, I made a periodic table with QPushbuttons in QtCreator. And I want to map these buttons with an array, say *pb[115]. I used pb[1]=ui->hydrogen; pb[2]=ui->helium; But it takes tons of work and resulted in a long code. How can I do this elegantly?

@MasterBlade
Further to @Pl45m4.This question comes up often. Designer won't put your widgets into some kind of array/list/map. But at runtime (e.g. just after
setupUi()call) you can retrieve all or some of the widgets you have created via
template <typename T> QList<T> QObject::findChildren(QAnyStringView name, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
This is the secret/required to avoid having to write 115 lines of code, and keep it maintainable if things change.// `theParent` is whatever widget encloses all your QPushButtons, // e.g. might be a `QMainWindow` or some `QWidget` QList<QPushButton *> allButtons = theParent->findChildren<QPushButton *>();This gives you a list of all your
QPushButtons. If you keep a widget container for just the element-name buttons area (suggest you do) you can use that astheParent. If not you need to remove, say, the OK and Cancel buttons from this list when you get it back.That's it if you just want a
QListof them (can be indexed by an integer counter just like an array). If you get advanced, and decide you would like to be able to access a particular element's button from, say, the element name/text on the button, convert it over to aQMap<QString, QPushButton *>and then you can do things like:QPushButton *btnHelium = buttonsMap.value("He"); -
@MasterBlade
Further to @Pl45m4.This question comes up often. Designer won't put your widgets into some kind of array/list/map. But at runtime (e.g. just after
setupUi()call) you can retrieve all or some of the widgets you have created via
template <typename T> QList<T> QObject::findChildren(QAnyStringView name, Qt::FindChildOptions options = Qt::FindChildrenRecursively) const
This is the secret/required to avoid having to write 115 lines of code, and keep it maintainable if things change.// `theParent` is whatever widget encloses all your QPushButtons, // e.g. might be a `QMainWindow` or some `QWidget` QList<QPushButton *> allButtons = theParent->findChildren<QPushButton *>();This gives you a list of all your
QPushButtons. If you keep a widget container for just the element-name buttons area (suggest you do) you can use that astheParent. If not you need to remove, say, the OK and Cancel buttons from this list when you get it back.That's it if you just want a
QListof them (can be indexed by an integer counter just like an array). If you get advanced, and decide you would like to be able to access a particular element's button from, say, the element name/text on the button, convert it over to aQMap<QString, QPushButton *>and then you can do things like:QPushButton *btnHelium = buttonsMap.value("He");@JonB said in How to map widgets created in QtCreator with an array?:
This gives you a list of all your QPushButtons. If you keep a widget container for just the element-name buttons area (suggest you do) you can use that as theParent. If not you need to remove, say, the OK and Cancel buttons from this list when you get it back.
Or cleaner:
You rename all periodic table buttons to something like "btn_element_helium", "btn_element_uranium" etc. so that it's different from "other" control buttons which are also part of that form class.
Then match only the element buttons using their object name pattern (yes, you can match object names by regex and wildcards)QRegularExpression re("*element*"); QList<QPushButton *> allElementBtns = yourForm->findChildren<QPushButton *>(re); -
M MasterBlade has marked this topic as solved on