Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QToolButton Menu Popup style/behavior problem
primem0ver last edited by
I have a plugin based program that uses a toolbar rather than a regular application menu because the menu displayed is supplied by the plugin AND several plugins are active at once. In some ways it is similar in appearance to the Qt IDE except that the toolbar is meant to host both menus and tool buttons.
I want the menus that popup to behave like they would on a normal menu. This means I don't want to have to hit the button a second time for the menu to disappear. If the mouse leaves the menu's client area then the menu should disappear automatically. Is there a setting that allows this to happen? Or will I need to handle a "mouse leave" type event and send a message to the button (menu?) to hide the menu?
It would also be nice if the menu on the button next to it would appear if I move the mouse over another menu button while a menu is being displayed. I have a feeling this behavior would never be automatic so it is up to me to implement it. However, any pointers to get me started would be helpful (Thanks!).
primem0ver last edited by primem0ver
Ok... I partially solved this myself by subclassing the menu class and overriding the leaveEvent method. However it only works if my mouse actually goes down over the menu. Otherwise the leaveEvent doesn't even fire. I cannot for the life of me figure out any way to change the menu displayed (or hide the current menu) if I go to another button or control without "mouseovering" the menu. I mentioned my desire to do this in the OP.
The bottom line seems to be that all mouse events (at least ones having to do with movement) are disabled when the menu is shown and DO NOT reactivate until the mouse enters the menu's client area (rectangle) or the button is clicked again. QMainWindow events don't respond either. Is this a bug? Is there a way to change this?
I want the menus that popup to behave like they would on a normal menu. This means I don't want to have to hit the button a second time for the menu to disappear.
I'm not sure where you got the impression that this is "normal". I haven't seen such behavior in any app I know (I'm on Windows btw.). It sounds like an incredibly frustrating behavior. Especially if you don't have a steady hand. People often "fly around" with their cursor before they choose a menu option. Closing the menu while they make a decision sounds really anti-user experience.
Think really hard about changing that. The thing is that what might sound like a good idea to you is not always best for your users.
If you insist on that anyway you might try to subclass QWidgetAction and detect the leave there. Since it's a full blown widget it might be possible (I haven't tried myself).
It would also be nice if the menu on the button next to it would appear if I move the mouse over another menu button while a menu is being displayed
If you want a menu behavior why not using QMenu instead of buttons?
primem0ver last edited by primem0ver
Umm... hmm. Ok... I am so used to menu behavior that perhaps I don't think about it anymore. In a sense you are right. The menu does not go away automatically. However, normally I can click anywhere else in the application and the menu will disappear. This was not the case with the menus being spawned by pressing a QToolButton. Before I made the changes I had to go back up to the button that spawned the menu and click it again to get it (the menu) to go away. Now for some reason it works.
The second behavior I described DOES happen. Anytime you click a menu on a windows menu bar and brush across the menu bar, the menu under the cursor is brought up and the original one goes away. I can't seem to make this happen on the toolbar.
The second behavior I described DOES happen
Yes. It's a standard behavior for menus (menu bar is also a menu). You don't want mouse clicks needed for navigating through menus after all.
Toolbars are a different story. They are collection of buttons. Buttons, as you noticed yourself, don't have this behavior and that is good too. I wouldn't want menus to popup spontaneously when I hover over a button. That would be very annoying.
QToolBar and QMenuBar conform to the generally accepted way of how buttons and menus should work. If you want some custom behavior then it's doable but you will have to jump through some hoops to get it. Ui design has a long long history and the way things are is not because one or two people thought it should be that way but because of years and years of study and adjustments. Tread carefully when breaking these guidelines. You will surprise your users. That's never a good thing in ui design.