I’ve recently been studying Qt, and have the following questions:
- What is the difference between
QActionandQToolButton? - How do I know when to override
QPushButton? For example, should I override in order to be informed when the mouse enters aQPushButton‘s bounds? I don’t need to in order to get thesignalclick().
Question 1:
QActions are used to define tasks performed by an application in a way which can be understood by a number of different user interface objects. Using an example from the Qt docs for context:A
QActionis defined; it is given an icon image, a text description, a keyboard shortcut and a longer tooltip description, as well as being linked to a user defined function.Later in the implementation, the
QActionis added both to a textual menu bar…… and an icon-based tool bar:
The menu bar uses the text description to create an entry in the menu corresponding to the action. Similarly the toolbar uses the icon set on the
QActionto create an icon in the toolbar which corresponds to the action. Selecting the menu item, clicking the toolbar icon or pressing the keyboard shortcut will all result in the same effect: that defined by the linking ofQAction::triggered()tonewFile().So to directly answer your question: a
QActionis an abstract way of defining the various parameters and behaviour of a particular task performed by the application. AQToolbarButtonis a UI object (derived fromQWidget) created by aQToolbarin response toQToolbar::addAction()Question 2:
Yes,
QPushButtonhas aclicked()signalinherited fromQAbstractButton, but it does indeed lack a way to inform when the mouse has entered its bounds. You have a couple of options in order achieve this, but first you need to first set themouseTrackingproperty to be enabled. This will allow you to receive mouse move events on theQPushButtoneven if no mouse buttons are pressed. With that done you need to explore one of the following options:QPushButtonand reimplementmousePressEventin order to respond to the mouse position.eventFilteron theQPushButton, and watch for events of type (QEvent::MouseMove).