Results 1 to 2 of 2

Thread: Get name of QAction from Menu when dynamically added

  1. #1
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11

    Default Get name of QAction from Menu when dynamically added

    Hi -

    I am adding QActions to my menu dynamically when a dynamic library is loaded as a plugin. Items get added to a menu using the following funciton:

    Qt Code:
    1. QAction* MainWindow::createModuleMenuItem(const QString &text, const QObject *receiver, const char *member) {
    2. return moduleMenu->addAction(text, receiver, member);
    3. }
    To copy to clipboard, switch view to plain text mode 

    This method works fine for inserting items into menus. However I am having trouble removing items from said menus. I know there is a removeAction function for Menus, but it takes a QAction as an argument, and since I am loading my menu items dynamically, I don't have a name to pass as an arguement to the removeAction function.

    In general, I can get items to load into a menu and remove them from a menu when the QAction has a name tied to it (as shown below), but I can't see to figure out how to do it with QActions added to menus without a statically defined name (as shown above).

    Qt Code:
    1. QAction *load = new QAction(tr("&Load Workspace"), this);
    2. load->setShortcuts(QKeySequence::Open);
    3. load->setStatusTip(tr("Load a saved workspace"));
    4. connect(load, SIGNAL(triggered()), this, SLOT(loadSettings()));
    5. fileMenu->addAction(load);
    6. fileMenu->removeAction(load)
    To copy to clipboard, switch view to plain text mode 

    Any advice/help/guidance is appreciated. Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get name of QAction from Menu when dynamically added

    addAction returns a pointer that you can later pass to removeAction. You can store that pointer and retrieve it later when you need it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 10th March 2014, 05:40
  2. Replies: 2
    Last Post: 3rd June 2013, 16:45
  3. Replies: 6
    Last Post: 19th June 2012, 18:54
  4. Context menu for QAction
    By drhex in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2009, 18:01
  5. Connecting dynamically added properties
    By stefkeB in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2008, 15:19

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.