Results 1 to 4 of 4

Thread: ownership of QMenu after calling addMenu()

  1. #1
    Join Date
    Nov 2011
    Location
    Karlsruhe, Germany
    Posts
    57
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default ownership of QMenu after calling addMenu()

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ownership of QMenu after calling addMenu()

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    cic (10th July 2013)

  4. #3
    Join Date
    Nov 2011
    Location
    Karlsruhe, Germany
    Posts
    57
    Thanks
    10
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: ownership of QMenu after calling addMenu()

    @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() ?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: ownership of QMenu after calling addMenu()

    Well, if you want to access a menu after creating it, keep its pointer, e.g. in a class instance variable.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 14th March 2012, 15:16
  2. QMenu mac
    By migel in forum Newbie
    Replies: 1
    Last Post: 26th September 2011, 19:06
  3. addMenu problem on Mac
    By yycking in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2009, 07:18
  4. addMenu problem
    By nicolas1 in forum Qt Programming
    Replies: 4
    Last Post: 17th March 2009, 09:48
  5. How to hide/remove a addMenu() menu
    By MrGarbage in forum Qt Programming
    Replies: 2
    Last Post: 3rd August 2007, 23:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.