Volomike
Volomike

Reputation: 24916

In Qt/C++, is there a way to connect a list of button click events to one slot?

In Qt/C++, I have a series of buttons where I want to run them through one single event handler for the click, and know which one was clicked.

Is there a way to connect a list of button click events to one slot? As in, I create a QList<QPushButton *>, and then use a single connect() statement to handle this list's &QPushButton::clicked signals, and map this to one single SLOT(onButtonClicked(QWidget *)).

Keyword there -- single. Sure, I can iterate a list, but I want to know if there's a way to have only one connect() statement that's outside the list iteration.

In other words, I'm wanting to know if there's already an efficient way to do this that I may not realize, rather than me iterating a QList of QPushButton. It's just a matter of being efficient and tidy.

Upvotes: 1

Views: 881

Answers (1)

Sebastian Lange
Sebastian Lange

Reputation: 4029

Best way to handle this would probably to have a QButtonGroup. If you insist using a List of your own or may have different widgets or signals, you still could work with an QSignalMapper.

http://doc.qt.io/qt-5/qbuttongroup.html

http://doc.qt.io/qt-5/qsignalmapper.html

Upvotes: 2

Related Questions