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.

Qt Code:
  1. this->menuBar()->addMenu("test1");
  2. QList<QMenu*> menuList = this->menuBar()->findChildren<QMenu*>();
  3.  
  4. if (!menuList.isEmpty()) {
  5. QMenu* menu = menuList.front();
  6. delete menu;
  7.  
  8. QList<QMenu*> list = this->menuBar()->findChildren<QMenu*>();
  9. }
  10.  
  11. QMenu* menu = new QMenu("test2");
  12. this->menuBar()->addMenu(menu);
  13. menuList = this->menuBar()->findChildren<QMenu*>();
  14.  
  15. if (!menuList.isEmpty()) {
  16. QMenu* menu = menuList.front();
  17. delete menu;
  18.  
  19. QList<QMenu*> list = this->menuBar()->findChildren<QMenu*>();
  20. }
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....
Qt Code:
  1. QAction* action = new QAction("action1", menu);
  2. menu->addAction(action);
  3. menu->addAction("action2");
  4. QList<QAction*> actionList = menu->findChildren<QAction*>();
  5. QListIterator<QAction*> iter(actionList);
  6.  
  7. while (iter.hasNext()) {
  8. QAction* action = iter.next();
  9. delete action;
  10.  
  11. QList<QAction*> list = menu->findChildren<QAction*>();
  12. }
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?