Results 1 to 11 of 11

Thread: QMenu: remain visible while mouseOver

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QMenu: remain visible while mouseOver

    I have a standard pushButton with a Menu and a few Actions. The default behavior: when an action is clicked, the menu closes. Is there a way to change the behavior such that the Menu remains open/visible while so long as the mouse is over it? (i.e. doesn't close when an action is clicked)

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMenu: remain visible while mouseOver

    one way you could try is to catch the aboutToHide () signal, and in the slot call show().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu: remain visible while mouseOver

    thanks hf, that's a start... that never ends.

    Qt Code:
    1. connect(myMenu, SIGNAL(aboutToHide()), this SLOT(menuSL()));
    2.  
    3. void Window::menuSL()
    4. {
    5. show();
    6.  
    7. // is it possible to do something here like:
    8. // if(myMenu->QWidget::leaveEvent) hide();
    9. // else show();
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    Above won't allow the menu to close, since show() is called before every attempt. Obviously, I need to test whether the mouse is over the menu or not, like in the comments.

    How to do this? I already have an eventFilter that checks if the mouse is over this widget for other reasons. Perhaps I should simply amend this eventFilter to check for buttonPresses: if pressed over my menu, do show(), and if menu leave, then hide(). Seems like overkill, though.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMenu: remain visible while mouseOver

    Obviously, I need to test whether the mouse is over the menu or not, like in the comments.
    I thought it was abovious too.
    You didn't expect us to deliver you every little detail did you?

    If I understand correctly, you want to have the menu stay visible as long as your cursor is above it, even if you made your selection - is that correct?
    So testing to see if the mouse button is clicked wont bring much.

    But you could try the following:
    Qt Code:
    1. void Window::menuSL()
    2. {
    3. if(myMenu->isUnderMouse())
    4. myMenu->show();
    5. }
    To copy to clipboard, switch view to plain text mode 

    and in your event filter, look for QEvent::Leave to hide the menu once the mouse has left it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. The following user says thank you to high_flyer for this useful post:

    vonCZ (24th October 2007)

  6. #5
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu: remain visible while mouseOver

    Quote Originally Posted by high_flyer View Post
    You didn't expect us to deliver you every little detail did you?
    ...yeah, eventually.

    Thanks a mill: underMouse() did tha' trick.

  7. #6
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu: remain visible while mouseOver

    ...i spoke too soon: more difficulties. The pushButton isn't staying checked while the menu is up.

    I've setCheckable(false) on my pushButton. Normal behavior: the button is pressed/checked while the menu is up, then as soon as a menu selection is made, the menu disappears and the button goes back to checked(false).

    I've modified the menu to stay up while moused Over per hf's advice, above.

    I would like the button to stay checked while the menu is up, then unchecked when it disappears (basically what you'd expect). I'm getting the following behavior, however: when I select an Action on the menu, the button changes to checked(false)... even though the menu is still up. If I make more selections, the button changes to checked(true) and stays that way. I've done this:

    Qt Code:
    1. void Window::menuSL()
    2. {
    3. if(myMenu->isUnderMouse())
    4. {
    5. myButton->setChecked(true);
    6. myMenu->show();
    7. }
    8. }
    9.  
    10. //I also added a new SLOT and connected it to the Actions:
    11.  
    12. void Window::actionSL()
    13. {
    14. qDebug("actionSL"); // this works
    15. myButton->setChecked(true);
    16. }
    To copy to clipboard, switch view to plain text mode 

    Neither of these have any effect: the button changes to checked(false) as soon as an action is selected (even though the menu is still up). Advice?

  8. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMenu: remain visible while mouseOver

    The pushButton isn't staying checked while the menu is up.
    I was not aware you were popping a menu as a reaction to a toggle button.
    the button changes to checked(false)... even though the menu is still up.
    That is because the QAction object is still doing its work, and the button is connected to it.
    They both don't know you are "cheating" with the menu. (making it behave in a way not consistent with what the QAction scheme is design to do)

    In your case I would reconsider using QAction, and just make your own signal slot connections.
    QAction and other such mini frameworks are designed to make life easier for common coding tasks, but if you want something different, then you might be better of doing things your own way.

    Any how, how about the following:
    Qt Code:
    1. //somewhere:
    2. connect(myButton,SIGNAL(toggled(bool)),this,SLOT(checkMenuState(bool)));
    3.  
    4. void Window::checkMenuState(bool checked)
    5. {
    6. if(myMenu->isUnderMouse() && !checked)
    7. {
    8. myButton->setChecked(true);
    9. {
    10. }
    To copy to clipboard, switch view to plain text mode 

    It could be that you will have also have to take care of the un-checking your self when the menu closes.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #8
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu: remain visible while mouseOver

    Quote Originally Posted by high_flyer View Post
    In your case I would reconsider using QAction, and just make your own signal slot connections.
    Yeah, I thought I might have to drop QAction; if I do so, don't I also have to drop QMenu?

    A menu consists of a list of action items... There are three kinds of action items: separators, actions that show a submenu, and actions that perform an action.
    If so: what to use instead? A drop-down menu that closes when the mouse has left this menu would seem like a pretty basic feature. Surely there's a relatively simple way to do it...

    I tried your checkMenuState idea. Doesn't work either.

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

    Default Re: QMenu: remain visible while mouseOver

    I'd use a custom widget with QToolButtons (so that you can still use actions) in a vertical layout and monitor for enter and leave events. And probably make that widget a popup as well by using appropriate window flags.

  11. #10
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu: remain visible while mouseOver

    Quote Originally Posted by wysota View Post
    I'd use a custom widget with QToolButtons (so that you can still use actions) in a vertical layout and monitor for enter and leave events. And probably make that widget a popup as well by using appropriate window flags.
    I don't follow. You mean a custom widget (like a QFrame?) that has the 2 actions in a vertical layout... which pops-up when the QToolButton is pressed?

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

    Default Re: QMenu: remain visible while mouseOver

    Yes, something like that. Just not a QFrame but QWidget. There is no reason for using a frame here.

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.