I am not able to clean action items from the QMenu. It is right click context menu. Every item when I right click it will go through following code. I want to delete any existing items on the menu before I add actions again.
I tried to call clear() on menu. It does not work.
if(pMenu)
{
pMenu->clear();
disconnect(pMenu,0,0,0);
for ( int item = 0; item < m_strItemList.count(); ++item )
{
pAction = pMenu->addAction(strArg.arg(m_strItemList[item] ));
mapper->setMapping(pAction, 1)
connect(pAction, SIGNAL(triggered()), mapper, SLOT(map()));
connect(mapper, SIGNAL(mapped(int)), this, SLOT(RunItem(int)));
}
QString strAg = "ITEM %1";
QMenu * pMenu = new QMenu;
if(pMenu)
{
pMenu->clear();
disconnect(pMenu,0,0,0);
for ( int item = 0; item < m_strItemList.count(); ++item )
{
QAction * pAction = new QAction(pMenu);
pAction = pMenu->addAction(strArg.arg(m_strItemList[item] ));
QSignalMapper* mapper = new QSignalMapper(pMenu);
mapper->setMapping(pAction, 1)
connect(pAction, SIGNAL(triggered()), mapper, SLOT(map()));
connect(mapper, SIGNAL(mapped(int)), this, SLOT(RunItem(int)));
}
To copy to clipboard, switch view to plain text mode
Following is Implemnation of Clear() in QMenu.cpp class in Qt Source. I tried to step into it and it never goes through forloop because actions list is empty.
{
QList<QAction*> acts = actions();
for(int i = 0; i < acts.size(); i++) {
#ifdef QT_SOFTKEYS_ENABLED
// Lets not touch to our internal softkey actions
if(acts[i] == d->selectAction || acts[i] == d->cancelAction)
continue;
#endif
removeAction(acts[i]);
if (acts[i]->parent() == this && acts[i]->d_func()->widgets.isEmpty())
delete acts[i];
}
}
void QMenu::clear()
{
QList<QAction*> acts = actions();
for(int i = 0; i < acts.size(); i++) {
#ifdef QT_SOFTKEYS_ENABLED
Q_D(QMenu);
// Lets not touch to our internal softkey actions
if(acts[i] == d->selectAction || acts[i] == d->cancelAction)
continue;
#endif
removeAction(acts[i]);
if (acts[i]->parent() == this && acts[i]->d_func()->widgets.isEmpty())
delete acts[i];
}
}
To copy to clipboard, switch view to plain text mode
Thanks.
Bookmarks