Results 1 to 3 of 3

Thread: Life time of a QMenu?

  1. #1
    Join Date
    Sep 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Life time of a QMenu?

    Hello,

    I have a question about the life time of QMenu. I use it as a custom context menu. It is declared on MainWindow class level (this->contextMenu) and is intended to be cleared and reused with different QActions depending on the column that is right-clicked in a QTableView. Here is my code:

    Qt Code:
    1. void MainWindow::on_tableView_customContextMenuRequested(const QPoint &pos)
    2. {
    3. this->contextMenu->clear();
    4. //...
    5. //Some code to add QActions depending on the column under pos
    6. //...
    7. //In a first approach the context menu is placed 10px right of the mouse click position
    8. QPoint kPos = ui->tableView->viewport()->mapToGlobal(pos) + QPoint(10, 0);
    9. if (this->contextMenu->isVisible()) {
    10. this->contextMenu->move(kPos);
    11. } else {
    12. this->contextMenu->popup(kPos);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    This code sort of works. If I right-click, the context menu opens. If I select a QAction from the menu, the menu is closed and the QAction is executed. However, if I don't select a QAction, but right-click on another cell, I want the context menu to be moved to the new position with a new set of QActions. My approach is to clear the QMenu, build it up from scratch and move it to the new position.
    The problem is: Sometimes it works and sometimes it doesn't (I feel it depends on if I move the mouse between button down and button up). If it doesn't work, the menu is hidden instead. If I close the main window in this situation (hidden context menu), the main window won't close. The first close attempt seems to close the invisible context menu, the second attempt finally closes the main window.

    • Is the intended feature maybe to close the QMenu if popup() is called and it is already open?
    • What is the intended life time of a QMenu:
    • If I select a QAction, is it intended to be destroyed? Or hidden?
    • What is the best practice approach to reuse a QMenu?


    What am I doing wrong?

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Life time of a QMenu?

    Qt Code:
    1. void MainWindow::on_tableView_customContextMenuRequested(const QPoint &pos)
    2. {
    3. QMenu l_menu;
    4. //.....get the data depend on cell & create actions add them to l_menu
    5. // l_menu.addAction(action);
    6.  
    7. QPoint kPos = ui->tableView->viewport()->mapToGlobal(pos) + QPoint(10, 0); //check if your position is correct or not
    8. menu.exec(kPos); // or else menu.exec(QCursor::pos());
    9. }
    To copy to clipboard, switch view to plain text mode 

    Now menu is local & will be cleaned automatically, No memory leak.
    No need to check if menu is visible or not.
    Thanks :-)

  3. #3
    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: Life time of a QMenu?

    Instead of recreating the menu(s) all the time, have you considered just hiding the action that are currently not applicable?

    Cheers,
    _

Similar Threads

  1. Prefixed for life?
    By qtoptus in forum Qt Programming
    Replies: 2
    Last Post: 16th April 2014, 18:32
  2. Conway's life visualization
    By qutron in forum Newbie
    Replies: 2
    Last Post: 30th December 2010, 16:13

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.