I was using Qt to build a basic GUI for an application and I have a few questions..
So I created the GUI, it works fine and I thought I would check something..
for(int i=0; i < 100000; i++)
{
menu = new QMenu(this);
act = new QAction("About", menu);
menu->addAction(act);
connect(act, SIGNAL(triggered()), this, SLOT(slotHelpAbout()));
menuBar()->addMenu(menu)->setText("Help");
}
menuBar()->clear();
I use the QMenuBar of the QMainWindow class and fill it up with QMenu which are also filled up with QAction for wich I connect the triggered signal to a few slots.. This works fine but when I call clear it should delete the menu/action items contained by QMenuBar.. I am checking in task manager and the memory usage is still huge..
Even after:
QList<QAction*> lst = menuBar()->actions();
for(int i=0;i < lst.length(); i++)
{
delete lst.at(i);
}
Shouldn’t all memory used by the QMenus and QActions be freed ?
QMenuBar::clearwill do nothing for you since, as pointed out by @Dave Mateer, it only removes actions from theQMenuBarand doesn’t delete them.Also, deleting the list of actions from the
QMenuBarwill not cause eachQMenuitself to be deleted.Each
QMenuthat you have is parented tothiswhich is presumably yourQMainWindow. They will only be deleted when you delete theQMainWindowand not it’s menu bar. You can change your code so that you parent eachQMenuto theQMenuBarso that deleting the menu bar deletes the menus (and their actions). Alternatively you can keep hold of pointers to each individual menu and delete them manually.