Results 1 to 8 of 8

Thread: [solved] QMenu popup on QTableView does not close after selecting QAction

  1. #1
    Join Date
    Jun 2016
    Posts
    19
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default [solved] QMenu popup on QTableView does not close after selecting QAction

    Right Click created menu on top of QTableView does not go away after clicking on action item.

    After the right click on QTableView the menu pops up.
    - 1st click on menu item (triggers signal/slot), but dont close the menu, 2nd click anywhere does close the menu.
    - 1st click anywhere (outside of menu) does not close the menu, 2nd click anywhere closes the menu.

    How do I force to close the menu after item triggered event ?

    Qt Code:
    1. void myRLCstuff::on_tableView_customContextMenuRequested(const QPoint &pos)
    2. {
    3. QMenu * menu = new QMenu(this);
    4. // connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(testMenu_actionTriggered(QAction*)));
    5. QAction * action = new QAction("&Add Record", menu);
    6. menu->addAction(action);
    7. // connect(action, SIGNAL(triggered()), this, SLOT(on_action_AddRecord_triggered()));
    8. menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
    9. }
    To copy to clipboard, switch view to plain text mode 

    I have been on this for hours,
    - I tried connect(triggered, testMenu_actionTriggered) and issuing menu->hide/close in testMenu_actionTriggered to no avail
    - I tried one shot timer delay 100ms, => (menu, close/hide) to no avail.
    - I tried 'menus' example and it works there as expected, but in my app using QTableView this dont work.
    - If I add "QMessageBox popup before "menu->popup" line, I need two clicks on button "OK" to dismiss the button

    qt version 5.5.1 (qt creator 3.6)

    EDIT: I realized that the custom context event was firing up two times.
    In UI editor, I had contextMenuPolicy=DefaultContextMenu, and in the code I had
    Qt Code:
    1. view->setContextMenuPolicy(Qt::CustomContextMenu);
    2. connect(view, SIGNAL(customContextMenuRequested(QPoint)), this,
    3. SLOT(on_tableView_customContextMenuRequested(QPoint)));
    To copy to clipboard, switch view to plain text mode 
    That was causing on_tableView_customContextMenuRequested to fire twice

    I fixed this by deleting above code and setting UI editor contextMenuPolicy=CustomContextMenu
    I dont understand though how qt runtime connects contextMenuPolicy=CustomContextMenu setting to on_tableView_customContextMenuRequested() member ?
    Last edited by boo9; 12th June 2016 at 16:45.

  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: [solved] QMenu popup on QTableView does not close after selecting QAction

    When you set Qt::CustomContextMenu it makes the widget emit the contextMenuRequested() signal.

    You named a method "on_" + widget object name + "_" + signal name, which triggers the auto-connect feature in code generated from designer.
    Your code then additionally made a manual connect, connecting the signal to the slot twice, getting it executed two times for each signal emit.

    Recommendations:
    1) do not use the name based auto-connect, always use manual connects

    2) for the use case of just having your own actions in the context menu, just add the actions using QWidget::addAction() and set the context menu policy to [noparse]Qt::ActionsMenuPolicy[/norparse].

    Cheers,
    _

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

    boo9 (12th June 2016)

  4. #3
    Join Date
    Jun 2016
    Posts
    19
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [solved] QMenu popup on QTableView does not close after selecting QAction

    > 1) do not use the name based auto-connect, always use manual connects
    Not even for standard file/edit/help menus ?
    I am not sure I understand 'auto-connect', can you please nudge me into right URL about this topic ?

    > Qt::ActionsMenuPolicy
    nothing in google on this enum

  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: [solved] QMenu popup on QTableView does not close after selecting QAction

    Quote Originally Posted by boo9 View Post
    > 1) do not use the name based auto-connect, always use manual connects
    Not even for standard file/edit/help menus ?
    No, always connect explicitly, it is robust against object name changes.

    Quote Originally Posted by boo9 View Post
    I am not sure I understand 'auto-connect', can you please nudge me into right URL about this topic ?
    The code generated from the .ui file will look for slot names that match the "on_" + object name + "_" + signal name pattern and then look if one of the objects it generated code for has such a name and signal and if yes, connect that signal to the slot.

    In my opinion that should never have been added, but I guess, back then they wanted to appease the Delphi/VB crowds who can't be bothered to write a single line of code and need to have all done by the IDE.

    And yes it works as intended, but it has way too many drawbacks, being totally hidden being one of them.

    Quote Originally Posted by boo9 View Post
    > Qt::ActionsMenuPolicy
    nothing in google on this enum
    Right, sorry, typed that out of memory. It is Qt::ActionsContextMenu, see http://doc.qt.io/qt-5/qt.html#ContextMenuPolicy-enum

    Cheers,
    _

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

    boo9 (12th June 2016)

  7. #5
    Join Date
    Jun 2016
    Posts
    19
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [solved] QMenu popup on QTableView does not close after selecting QAction

    Quote Originally Posted by boo9 View Post
    but I guess, back then they wanted to appease the Delphi/VB crowds who can't be bothered to write a single line of code and need to have all done by the IDE.
    that's a good one
    I do second the args for the explicit approach,
    Just for the heck of it, how would one setup auto-connect to popup context menu from header part of QTableView ?

  8. #6
    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: [solved] QMenu popup on QTableView does not close after selecting QAction

    Quote Originally Posted by boo9 View Post
    Just for the heck of it, how would one setup auto-connect to popup context menu from header part of QTableView ?
    The header view of the table view isn't an explicitly generated object, it is created by the table view.
    Only objects for which code is generated are matched against the slot names.
    I.e. all objects that are members of the "ui" class.

    Another draw back :-)

    Cheers,
    _

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

    boo9 (13th June 2016)

  10. #7
    Join Date
    Jun 2016
    Posts
    19
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [solved] QMenu popup on QTableView does not close after selecting QAction

    What is a good scheme to name my slots ?

  11. #8
    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: [solved] QMenu popup on QTableView does not close after selecting QAction

    If you want to match the signal name, the "on" followed by the signal name is still a good option.

    You could even have the "sender" name in there, as long as you don't use underscores to separate the parts.
    The sender name could be anything, e.g the type of widget, the conceptual role it has, etc.
    E.g.
    Qt Code:
    1. onContextMenuRequested()
    2. onTableContextMenuRequested()
    3. onRecordViewContextMenuRequested()
    To copy to clipboard, switch view to plain text mode 

    For slots that are also called as normal C++ methods, it often makes more sense to use conventional method naming criteria, e.g.
    Qt Code:
    1. showSomething()
    To copy to clipboard, switch view to plain text mode 
    even if it is also connected to a button or action as one of its triggers.

    Cheers,
    _

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

    boo9 (13th June 2016)

Similar Threads

  1. Getting QMenu of selected QAction
    By snydesc in forum Qt Programming
    Replies: 1
    Last Post: 7th October 2012, 00:26
  2. Can't disable a QAction in QMenu
    By punkypogo in forum Qt Programming
    Replies: 3
    Last Post: 10th August 2010, 14:07
  3. QMenu popup: how close when clicked outside
    By powermax in forum Qt Programming
    Replies: 5
    Last Post: 4th March 2009, 03:18
  4. QPushButton QMenu QAction
    By hgedek in forum Newbie
    Replies: 2
    Last Post: 1st November 2007, 12:29
  5. how to popup and close a QMenu
    By Placido Currò in forum Qt Programming
    Replies: 15
    Last Post: 14th May 2007, 16:41

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.