Results 1 to 16 of 16

Thread: how to popup and close a QMenu

  1. #1
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    8
    Qt products
    Qt3 Qt4

    Default how to popup and close a QMenu

    Hi everybody,

    if you have a QMenuBar and you want a QMenu to popup without worrying about its position, you can write this code, assuming mb as a QMenuBar:

    Qt Code:
    1. mb->setActiveAction(mb->actions()[0]);
    2.  
    3. mb->actions()[0]->menu()->setActiveAction(mb->actions()[0]->menu()->actions()[0]);
    To copy to clipboard, switch view to plain text mode 

    the first statemet makes the QMenu popup and the second one makes the first QAction be selected.

    But if you want to close the opened QMenu, what would you do ?

    Qt Code:
    1. mb->actions()[0]->menu()->setActiveAction(0);
    To copy to clipboard, switch view to plain text mode 
    works properly because it removes the selection on the first QAction of the QMenu

    but

    mb->setActiveAction(0);
    doesn't work because the QMenu remains opened.

    What would you do to make it disappear ?

    Thanks
    Placido.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to popup and close a QMenu

    Call close() on the menu?

  3. #3
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    8
    Qt products
    Qt3 Qt4

    Default Re: how to popup and close a QMenu

    it doesn't work !

    I would like to supply other informations.

    My QMenuBar is not docked on the top of the window (is not the menuBar() of the QMainWindow), but I build it into a position of the QMainWindow and after using it I have to destroy it. This is to describe you the situation.

    If you execute the clear() or the hide() or the close() of the QMenuBar, you make it close, but the active menu doesn't close and it keeps the focus. If you execute the close of the QMenu it doesn't close.
    Last edited by Placido Currò; 14th May 2007 at 11:10.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to popup and close a QMenu

    Ok, anyway why are you doing such a strange thing as opening and closing the menu manually? It was not designer for this...

  5. #5
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    8
    Qt products
    Qt3 Qt4

    Default Re: how to popup and close a QMenu

    If I could, I would not popup the QMenu at run-time. The problem is how to close it at run-time. The only working solution, as I know, is deleting the QMenu. But it creates problems with the remaining code.

    Another consideration.

    The popupped qmenu raises on the windows of the application you are viewing at that time. I don't know how to open it and not prevent it from raising at this way.


    Coming soon, "don't popup that QMenu !"
    Last edited by Placido Currò; 14th May 2007 at 11:18.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to popup and close a QMenu

    But what do you need that functionality for? If you want a menu to be invactive, just disable it.

  7. #7
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    8
    Qt products
    Qt3 Qt4

    Default Re: how to popup and close a QMenu

    I want to make it not visible, this is what I want. They want to reduce the number of the clicks you have to do with the mouse.

    well, there would be a solution, but it doesnt' work perfectly. I can reduce the dimension of the QMenu,
    Qt Code:
    1. resize(0,0)
    To copy to clipboard, switch view to plain text mode 
    but the QMenu keeps the focus and it would be necessary to press ESCAPE to move the focus to elsewhere.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to popup and close a QMenu

    You mean you want to make collapsible menus? Try making the action responsible for the menu invisible (setVisible(false)).

  9. #9
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    8
    Qt products
    Qt3 Qt4

    Default Re: how to popup and close a QMenu

    Thanks for your help.

    I tried it but what I get is a void little rect at the same point where there was the qmenu. And then, I need to click on it to make this little rect to disappear.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to popup and close a QMenu

    Maybe you just need to update() the menu bar or the parent menu?

  11. #11
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    8
    Qt products
    Qt3 Qt4

    Default Re: how to popup and close a QMenu



    It doesn't work. It doesn't update. The rect remains, as if you build a QMenuBar with QMenus and no actions.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to popup and close a QMenu

    Seems to work quite fine for me...
    Attached Files Attached Files

  13. #13
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    8
    Qt products
    Qt3 Qt4

    Default Re: how to popup and close a QMenu

    try these statements in a on_click() of QPushButton

    mb->setActiveAction(mb->actions()[0]);

    mb->actions()[0]->menu()->setActiveAction(mb->actions()[0]->menu()->actions()[0]);

    then try to hide the first menu that has just appeared. I can close the qmenubar but the qmenu is open.

    I do this inside a routine because if you click outside the menu, the qmenu closes regularly because another event has just fired on another object.

    Sorry, if I insist.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to popup and close a QMenu

    But why do you setActiveAction at all? Could you explain what are you trying to achieve? QMenu closes because it is a popup window, not through any kind of magic. In your situation it doesn't close, because it is modal and you don't do anything to break the event loop.

  15. The following user says thank you to wysota for this useful post:

    Placido Currò (14th May 2007)

  16. #15
    Join Date
    Sep 2006
    Posts
    23
    Thanks
    8
    Qt products
    Qt3 Qt4

    Default Re: how to popup and close a QMenu

    I need the setActiveAction because it opens the qmenu I need to show. I don't use the popup() or the exec() because the former opens the menu but not within the qmenubar and then if I click the right or left arrow the QMenuBar doesn't open the near menus and the latter suspends the event loop.

    This is why I used this statement. My application must show a menu opened but close that menu if it receives a command to close the qmenubar, even before the human operator can do it. Imagine these events:

    My application is a simple client whoose task is to draw and build the gui.

    1) my application receives commands to build a qmenubar and show a qmenu
    2) the operator chooses the menu
    3) the operator does something...
    4) the application receives a command to build a qmenubar and show a qmenu

    my application takes a buffer of the events during the server-time and when it's ready to build the qmenubar it can receive the input to close the qmenubar before it has been displayed. In this case the application receives the command to close the qmenubar and obviously his opened children. This is what I can't do, closing the qmenus, if any is open.

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to popup and close a QMenu

    Maybe you should operate on event level then? It might be a general way to handle many things, not only the menu.... I'm sure if you posted proper events to the menu, you'd get the effect you desire.

Similar Threads

  1. Can't close my popup
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2006, 10:10

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.