Results 1 to 5 of 5

Thread: Identifying which QAction was toogled in a simple way

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Identifying which QAction was toogled in a simple way

    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/5774...a-QTableWidget

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Identifying which QAction was toogled in a simple way

    You are looking for QSignalMapper

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

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    Momergil (28th January 2014)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Identifying which QAction was toogled in a simple way

    Did you mean QTabBar or QToolBar? QButtonGroup is designed for grouping buttons in this way.

  5. #4
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Identifying which QAction was toogled in a simple way

    Quote Originally Posted by ChrisW67 View Post
    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...

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Identifying which QAction was toogled in a simple way

    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.

  7. The following user says thank you to ChrisW67 for this useful post:

    Momergil (30th January 2014)

Similar Threads

  1. identifying the position of a circle in image
    By qtlearner123 in forum Newbie
    Replies: 2
    Last Post: 26th April 2012, 06:14
  2. Identifying signal emitter
    By alan lenton in forum Qt Programming
    Replies: 4
    Last Post: 7th October 2009, 17:16
  3. Identifying QThreads
    By TheGrimace in forum Newbie
    Replies: 2
    Last Post: 18th March 2008, 16:39
  4. Identifying file when filename is not enough
    By drhex in forum Qt Programming
    Replies: 4
    Last Post: 9th November 2006, 20:44
  5. QAction
    By mickey in forum Qt Programming
    Replies: 7
    Last Post: 17th July 2006, 09:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.