Results 1 to 3 of 3

Thread: Accessing QAction from within Slot

  1. #1
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Accessing QAction from within Slot

    I'm dynamically creating menu items (using QMenu.AddAction) and connecting each QAction.triggered() to the one slot. Inside the slot function I need to know which QAction triggered the slot and ideally be able to get the data (QAction.data()) from that QAction.

    Is there a way to get hold of the QAction or is there another/better way to achieve this?

    Many thanks
    Stefan

  2. #2
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing QAction from within Slot

    Found the answer:

    When inserting action items you usually specify a receiver and a slot. The receiver will be notifed whenever the item is triggered(). In addition, QMenu provides two signals, activated() and highlighted(), which signal the QAction that was triggered from the menu.
    From QMenu Class Reference

  3. #3
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing QAction from within Slot

    It keeps on getting better:

    void QMenu::triggered ( QAction * action ) [signal]
    This signal is emitted when an action in this menu is triggered.

    action is the action that caused the signal to be emitted.

    Normally, you connect each menu action's triggered() signal to its own custom slot, but sometimes you will want to connect several actions to a single slot, for example, when you have a group of closely related actions, such as "left justify", "center", "right justify".

    Note: This signal is emitted for the main parent menu in a hierarchy. Hence, only the parent menu needs to be connected to a slot; sub-menus need not be connected.

    See also hovered() and QAction::triggered().
    From QMenuClass Reference

Similar Threads

  1. getting QAction text from the slot
    By roxton in forum Qt Programming
    Replies: 3
    Last Post: 18th April 2008, 13:03
  2. Replies: 3
    Last Post: 19th November 2007, 15:31
  3. deleting qaction
    By hyling in forum Qt Programming
    Replies: 1
    Last Post: 8th December 2006, 21:48
  4. QAction
    By mickey in forum Qt Programming
    Replies: 7
    Last Post: 17th July 2006, 09:42
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.