Jump to content
  • Advertisement
Sign in to follow this  

QAction vs. Keyboard accelerator

This topic is 2799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know this doesn't belong here but i've asked on the QT forum and have recieved little input. If anyone has experience please provide input to my question smile.png!

Could anyone please clearly state the difference between a keyboard QAccel and a QAction?

Also in the menu bar at the top of the main window....is a QAction an item in one of the menus? for example

File -> Exit <--- is Exit a QAction?

Share this post

Link to post
Share on other sites
The Qt documentation is very very very good.

I haven't used QActions too much, but my understanding of it is: A QAction is a class representing an 'action' triggered by some user input. That action can be triggered by a number of different widgets or events.

If I have an application that lets someone save their work, I might have a single QAction called 'saveAction'.
I might have a QMenu called 'File', that has 'saveAction' in it.
I might use that same 'saveAction' in a QMenu that appears when I right-click on the document itself (a content menu).
I might also have a QToolButton on my toolbar that has 'saveAction' as an action.
I might also have CTRL+S also trigger that same 'saveAction'.

In that scenario, there are four different ways of triggering the same QAction. 2 of them were QMenus, one was a QToolButton, and one was by keyboard shortcuts.

Here's the thing: The single QAction contains the text, icon, tooltip, and hotkey command that the other 3 widgets (the two menus and the QToolButton) use.

The icon of the QToolButton, the icon next to the menu item in the two menus: QAction's icon.
The text under the QToolButton (if you set QToolButton to that style) and the text in the two menus: QAction's text.
The tooltips for all three: QAction's tooltip.
The hotkey for all three: QAction's hotkey.

The QAction provides a uniform representation of the action itself (text, tooltip, icon, hotkeys, etc...), that the menus and buttons and the application itself uses, when providing an interface for triggering the QAction. The actual result of using the QAction is not handled by QAction, QAction will just emit a signal that you can catch and handle where you want. However, instead of having to handle four different signals and making them do the same thing (Filemenu->signal, contextMenu->signal, toolbutton->signal, hotkeyTriggered->signal), you just connect to a signal signal (saveAction->triggered() signal).

I may have gotten some of the details wrong, but I think that's the gist of it.

File -> Exit <--- is Exit a QAction?


To slightly confuse things, yes, 'Exit' is a QAction... but only because having a class called QMenuItem that does nothing except pretend to be a QAction would be pointless. QMenu has a list of actions, and handles the drawing of those actions.
QAction doesn't draw itself or describe how to be drawn. QMenu just wants a list of text, icons, tooltips, and signals to emit. QMenu does all the drawing itself. QToolButton does all the drawing itself, it just wants some text, a icon, a tooltip, and a signal to emit.

You could say QToolButton 'contains' a generic QAction internally, just as QMenu contains a list of QActions, if it helps you think about it. A QAction has nothing to do with the details of the visuals, only the details of describing the action to the user (text, tooltip, icon) and the signal to emit.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!