Results 1 to 3 of 3

Thread: "anonymous" QActions

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default "anonymous" QActions

    I want to have a variable amount of INTs in a menu, and if the user clicks on one of the items, the QAction triggers() and i process the integer that belongs to the menu item.

    The problem is that the number of INTs to be displayed in the menu varies, and that i actually always call the same function for processing the INT number. A static amount of QActions and a slot for each actions is therefore inefficient.

    It's not a perfect solution, but the window owning the menu could keep a vector of actions. If the context menu event fires, the vector is emptied and refilled with the desired amount of QActions.
    This would manage the lifetime of the QActions but does not address the core problem: connecting to the triggered() event of an QAction, there is no way of knowing WHICH Action was fired, and thus which number to process.

    Is there any good way to work around this?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: "anonymous" QActions

    Would QSignalMapper help?

  3. #3
    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: "anonymous" QActions

    Another option is to set the the reference value (in your case the INT), as the action's data and then retrieve the action in the slot using QObject::sender().

    However, I would also suggest QSignalMapper since it was specifically designed to map from a parameter-less signal to a single parameter slot (in your case an int parameter).

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 7th April 2010, 22:46
  2. Replies: 3
    Last Post: 15th February 2010, 18:27
  3. Replies: 3
    Last Post: 8th July 2008, 20:37
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 16:58

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.