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*>();
}
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*>();
}
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*>();
}
To copy to clipboard, switch view to plain text mode
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/8...hip-of-qaction
However... they worked unexpectedly....
menu->addAction(action);
menu->addAction("action2");
QList<QAction*> actionList = menu->findChildren<QAction*>();
QListIterator<QAction*> iter(actionList);
while (iter.hasNext()) {
delete action;
QList<QAction*> list = menu->findChildren<QAction*>();
}
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*>();
}
To copy to clipboard, switch view to plain text mode
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?
Bookmarks