PDA

View Full Version : QMenu clear items



user_mail07
22nd October 2011, 19:17
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.


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

}

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.

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];
}
}


Thanks.

amleto
22nd October 2011, 22:06
I guess the list is empty then.

why do this,
QAction * pAction = new QAction(pMenu);
then this,
pAction = pMenu->addAction(strArg.arg(m_strItemList[item] ));

?
The first pAction is not used for anything...

Do you have some code that shows the menu not clearing? If so, please post (and see my sig).

user_mail07
22nd October 2011, 22:31
I have posted code above. Where I am calling pMenu->clear();


QString strAg = "ITEM %1";
QMenu * pMenu = new QMenu;

if(pMenu)
{
pMenu->clear(); //CLEAR DOES NOT WORK
disconnect(pMenu,0,0,0);

for ( int item = 0; item < m_strItemList.count(); ++item )
{

QAcrion * pAction = pMenu->addAction(strArg.arg(m_strItemList[item] )); //Adding Action to the Menu

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

}

d_stranz
23rd October 2011, 22:17
In line 2 you create a new QMenu. It's EMPTY. Why do you think calling clear() on an empty menu will do anything? Of course the for() loop in QMenu::clear() is never entered. The menu is empty - there is nothing in it to clear!