PDA

View Full Version : Identifying which QAction was toogled in a simple way



Momergil
27th January 2014, 15:30
Hello!

I want to insert a QTabBar in a QMainWindow and put on it a series of QActions, all this by code. Most of this QActions will be checkable, so that if one of them is checked, immediately any other that may be checked shall be unchecked. The traditional method I have being used to do this is that, once a button is pressed, it will activate a method receiving by parameters the Id of the button pressed, while that method will then uncheck all buttons with the exception of the one that was triggered (identified by the Id).

I know only two ways of implementing this: either I create some class which posses this Id thing and than, when its triggered, it emits a signal sending its id (and connects that signal with the slot that will implement the uncheck logic), or else I have to create a slot for each button created; since each will have its own slot, I know which was triggered. There is also a third option, which is to use something of the natural Qt class (instead of creating another with the Id stuff) to identify which button was triggered; sometimes I use the objectName to do that.

The problem:
* I can't perform the traditional method because I can't set subclasses of QAction in a QTabBar (already tried, no sucess); the addActions(QList...) don't accept.
* I want to avoid the second method because I don't want to create one slot for each new QAction I instantiate. Imagine a QTabBar with 30 QActions: 30 slots only to call the same method!
* Using a QString to identify takes to much resources when identifying the QAction; I want to avoid that as well. And of course, I don't know any other method to identify QActions naturally.


I'ld be glad for you guys telling me how may I do this, giving the problems I wrote about.


Thanks,

Momergil


Note: I hope you guys don't mind answering this another topic as well: http://www.qtcentre.org/threads/57746-How-to-make-a-QTableWidgetItem-occupy-multiple-rows-and-or-columns-in-a-QTableWidget :)

anda_skoa
27th January 2014, 19:25
You are looking for QSignalMapper


Alternatively look the the documentation for QObject::sender()

Cheers,
_

ChrisW67
28th January 2014, 05:00
Did you mean QTabBar or QToolBar? QButtonGroup is designed for grouping buttons in this way.

Momergil
28th January 2014, 23:23
Did you mean QTabBar or QToolBar? QButtonGroup is designed for grouping buttons in this way.

Sorry, it was QToolBar indeed :x And thanks for the indication of QButtonGroup; it would seem that it provides what I need in a much easier way then QSignalMapper with the only problem that, since I'm working with a QToolBar, I suppose it would be correct to use QActions instead of QPushButtons or QToolButtons for this job (specially thanks to the separators). Although, of course, I'm incorrect about this - in which case I would be very curious about why there is even such a QAction class instead of being QToolButtons always...

ChrisW67
28th January 2014, 23:34
QAction is an implementation of part of the Command Pattern, not a GUI widget. When you addAction() to a tool bar it creates a visible button on your behalf. When you addAction() to a QMenuBar/QMenu widget the widget paints a menu entry for you. QActions added to a QWidget can provide a context menu. The same QAction can be attached to many GUI objects.