Results 1 to 2 of 2

Thread: Dynamic menu

  1. #1
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Dynamic menu

    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:

    Qt Code:
    1. //menu initialization:
    2. treeMenu = new QMenu(ui->objectTree);
    3. connect(ui->objectTree,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(treePopupShow(const QPoint&)));
    4.  
    5. // slot
    6. void MyProgram::treePopupShow(const QPoint & point) {
    7. QModelIndex index = ui->objectTree->indexAt(point);
    8. if(index.isValid())
    9. {
    10. TreeItem * item = static_cast<TreeItem *>(index.internalPointer());
    11. if(item)
    12. {
    13. treeMenu->clear();
    14. switch(item->getType())
    15. {
    16. case NodeType1:
    17. QMenu * submenu1 = treeMenu->addMenu(tr("submenu1")); // memory leak
    18. foreach(QString action,actionList1)
    19. {
    20. QAction * action = submenu1->addAction(action);
    21. action->setData(action);
    22. }
    23. break;
    24. case NodeType2:
    25. QMenu * submenu2 = treeMenu->addMenu(tr("submenu2")); // memory leak
    26. foreach(QString action,actionList2)
    27. {
    28. QAction * action = submenu2->addAction(action);
    29. action->setData(action);
    30. }
    31. break;
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Dynamic menu

    In this case there's no leak.
    When calling clear() actions get removed and deleted (if not used anywhere else), menus don't but:
    They don't leak as they're still parented ( clear() doesn't remove children ) so they will get deleted when the parent eventually gets destroyed.

    You can test it by checking for parent on a menu that was just cleared.

    Also to delete them you don't have to store them, you can get all the children by calling children() and then deleting them in a loop.

    Edit: it does not apply when using QMenu::addMenu ( QMenu * menu ) as this method doesn't take ownership of the added menu.
    Last edited by Spitfire; 25th November 2011 at 11:03.

Similar Threads

  1. Replies: 1
    Last Post: 4th November 2011, 11:25
  2. Dynamic gui.
    By Niamita in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2011, 15:15
  3. Replies: 1
    Last Post: 20th January 2011, 17:17
  4. Dynamic masking...?
    By lar0che in forum Newbie
    Replies: 1
    Last Post: 16th August 2010, 07:23
  5. ListView Dynamic Popup menu
    By raphaelf in forum Newbie
    Replies: 3
    Last Post: 14th October 2006, 19:26

Tags for this Thread

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.