folibis
25th November 2011, 00:49
I have TreeView with context menu (QMenu) in my program. But it have to be dynamic one, I mean than menu items created in runtime. Each item have his own set of actions.
My problem is how to avoid memory leaks.
I will illustrate it by example:
//menu initialization:
treeMenu = new QMenu(ui->objectTree);
connect(ui->objectTree,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(treePopupShow(const QPoint&)));
// slot
void MyProgram::treePopupShow(const QPoint & point) {
QModelIndex index = ui->objectTree->indexAt(point);
if(index.isValid())
{
TreeItem * item = static_cast<TreeItem *>(index.internalPointer());
if(item)
{
treeMenu->clear();
switch(item->getType())
{
case NodeType1:
QMenu * submenu1 = treeMenu->addMenu(tr("submenu1")); // memory leak
foreach(QString action,actionList1)
{
QAction * action = submenu1->addAction(action);
action->setData(action);
}
break;
case NodeType2:
QMenu * submenu2 = treeMenu->addMenu(tr("submenu2")); // memory leak
foreach(QString action,actionList2)
{
QAction * action = submenu2->addAction(action);
action->setData(action);
}
break;
}
}
}
It will be tens of such submenus like submenu1 and submenu2. and tens of top items. treeMenu->clear() clears only actions, not submenus. May be I have to store all of them to delete every time when menu is shown?
My problem is how to avoid memory leaks.
I will illustrate it by example:
//menu initialization:
treeMenu = new QMenu(ui->objectTree);
connect(ui->objectTree,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(treePopupShow(const QPoint&)));
// slot
void MyProgram::treePopupShow(const QPoint & point) {
QModelIndex index = ui->objectTree->indexAt(point);
if(index.isValid())
{
TreeItem * item = static_cast<TreeItem *>(index.internalPointer());
if(item)
{
treeMenu->clear();
switch(item->getType())
{
case NodeType1:
QMenu * submenu1 = treeMenu->addMenu(tr("submenu1")); // memory leak
foreach(QString action,actionList1)
{
QAction * action = submenu1->addAction(action);
action->setData(action);
}
break;
case NodeType2:
QMenu * submenu2 = treeMenu->addMenu(tr("submenu2")); // memory leak
foreach(QString action,actionList2)
{
QAction * action = submenu2->addAction(action);
action->setData(action);
}
break;
}
}
}
It will be tens of such submenus like submenu1 and submenu2. and tens of top items. treeMenu->clear() clears only actions, not submenus. May be I have to store all of them to delete every time when menu is shown?