PDA

View Full Version : Get name of QAction from Menu when dynamically added



yapatel
9th April 2014, 01:57
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:


QAction* MainWindow::createModuleMenuItem(const QString &text, const QObject *receiver, const char *member) {
return moduleMenu->addAction(text, receiver, member);
}

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).


QAction *load = new QAction(tr("&Load Workspace"), this);
load->setShortcuts(QKeySequence::Open);
load->setStatusTip(tr("Load a saved workspace"));
connect(load, SIGNAL(triggered()), this, SLOT(loadSettings()));
fileMenu->addAction(load);
fileMenu->removeAction(load)

Any advice/help/guidance is appreciated. Thanks.

wysota
9th April 2014, 09:16
addAction returns a pointer that you can later pass to removeAction. You can store that pointer and retrieve it later when you need it.