PDA

View Full Version : ownership of QMenu after calling addMenu()



cic
10th July 2013, 09:13
Hello all,

from Qt's documentation, the ownership of a QMenu after calling QMenu::addMenu(...) and QMenu::addMenu(QMenu*) differs.
I am confusing who should be responsible for deleting the created QMenu after some test codes.




this->menuBar()->addMenu("test1");
QList<QMenu*> menuList = this->menuBar()->findChildren<QMenu*>();

if (!menuList.isEmpty()) {
QMenu* menu = menuList.front();
delete menu;

QList<QMenu*> list = this->menuBar()->findChildren<QMenu*>();
}

QMenu* menu = new QMenu("test2");
this->menuBar()->addMenu(menu);
menuList = this->menuBar()->findChildren<QMenu*>();

if (!menuList.isEmpty()) {
QMenu* menu = menuList.front();
delete menu;

QList<QMenu*> list = this->menuBar()->findChildren<QMenu*>();
}



from the debugger I see, the Menu test1 could be found when findChildren was called.
while Menu test2 couldnt be found.

now 2 questions stand:
1. even from the doc, the menubar should have the ownership of Menu test 1. But after I call delete in the first loop, the list in the next code line becomes 0.
That means I have delete the menu out from menubar. Who has the ownership of the Menu test 1 indeed?

2. in the second loop, since the Menu test 2 is pre-defined Menu out of menubar, it couldnt be found in the menubar when findChildren was called according to the documentation.
The question is, in this case, how can I find the menu item in the menubar?

Ok, the question above led to another thought that I wanna also test QAction's behavior.
I used to think they are similar functioned according to http://stackoverflow.com/questions/8750878/ownership-of-qaction

However... they worked unexpectedly....


QAction* action = new QAction("action1", menu);
menu->addAction(action);
menu->addAction("action2");
QList<QAction*> actionList = menu->findChildren<QAction*>();
QListIterator<QAction*> iter(actionList);

while (iter.hasNext()) {
QAction* action = iter.next();
delete action;

QList<QAction*> list = menu->findChildren<QAction*>();
}


In this time, the actionList contains 3 elements, I think one of the element is from the QMenu::menuAction() as default.
What's more, i can delete 3 elements then the list in the loop becomes 0. The question is:

1. Who has the ownership when addAction(QAction*) is called?

anda_skoa
10th July 2013, 13:04
You can always delete a QObject even if it is part of a parent/child tree, because it will notify its parent about the deletion who will then remove it from the list of its children.

Why you would ever want to use findChildren on the menu bar is beyond me though.

Cheers,
_

cic
10th July 2013, 14:03
@anda_skoa, thank you.

It solves part of my puzzules.
I wanted to find a certain QMenu in the menu bar, that's why I called this function since from QMenuBar I didnt find such a function.

Is there any way to find it other than findChildren() ?

anda_skoa
20th July 2013, 17:27
Well, if you want to access a menu after creating it, keep its pointer, e.g. in a class instance variable.

Cheers,
_