I'm trying to make my own animated GUI with the help of Qt 4.8 on Windows. At the beginning i used the widgets and the Qt Designer to place a horizontal/grid/etc layout and put inside the normal widgets but I found that making them move with the animations framework is not really easy. So I switched to QGraphicsScene/QGraphicsView that seems much simpler to use with animations. I would make a simple menu where there are the main items that when hovered makes visible the submenu items. Something simple, thing about a dropdown horizontal menu. I placed the QGraphicsTextItem items but I still don't understand how could I connect the hover event of the main items to display the submenu items. Any idea? At the moment I have a QMainWindow with a QGraphicsView widget that takes all the area.
Asked
Active
Viewed 1,518 times
1 Answers
1
You could display the submenu (or do whatever action you want) on hover by creating your own specialized subclass of QGraphicsTextItem
and re-implementing the hoverEnterEvent
and hoverLeaveEvent
methods.
Make sure you've read the setAcceptHoverEvents
documentation so you understand when, and for which items, these events are generated.

Mat
- 202,337
- 40
- 393
- 406
-
I read the docs and I've done my own class that supports hover events. It works but i still don't understand how can I "link" actions of the items of the menu like I would do with a real qt menu and how I can display some subitems when their main menu item is hovered – Stefano May 27 '12 at 08:09
-
Well, you just write the code that does that. If you need to display something while your text item is hovered, `show()` that item on hover enter, and `hide()` it on leave. Or create the items on hover and remove them on leave. There's no magic, you need to code the actions you want to happen. – Mat May 27 '12 at 08:12
-
would be better to use QToolButton to link QActions like I would do normally? – Stefano May 27 '12 at 08:30
-
Define "better". Use whatever works best for you. Experiment. Play with the widget proxy class. There is no universal "best" method to do anything. – Mat May 27 '12 at 08:35
-
I would simply know the best way to have a menu with submenu items that appear only if needed (hover event of their parent menu item) and that are linked to a QAction that allow me to execute commands – Stefano May 27 '12 at 08:42
-
The best way is the one that works best for you in this specific circumstance. If you're not using a plain menu but building that menu yourself, you get to (and have to) choose what to do. Trigger the QAction you want when the user does whatever it is your UI needs. – Mat May 27 '12 at 08:46