Hi all,
I'm completely new to Qt. I've been building small apps, trying to get familiar with how everything works, and I feel like some of the things I'm doing (or want to do) can surely be done better.
The question for this post concerns shortcut keys. I know that QShortcut can be used to attach shortcuts to the class that displays (for example) my window widget.
(void) new QShortcut(Qt
::Key_Up,
this,
SLOT(on_movePositiveButton_pressed
()));
(void) new QShortcut(Qt::Key_Up, this, SLOT(on_movePositiveButton_pressed()));
To copy to clipboard, switch view to plain text mode
And that works nicely. I can also use events, by defining an event filter and then using
movePositiveButton->installEventFilter(this)
movePositiveButton->installEventFilter(this)
To copy to clipboard, switch view to plain text mode
to make that filter operate on events that occur through that object. That works as expected.
However, there are a few problems with either approach.
1. The QShortcut approach doesn't let you assign an action for when you let go of a key. So if I want the Left arrow key to run the movePlayerLeft() function, I can't run the stopPlayerMotion() after the user has let go of the key. Right? Is there a good way to detect when a key has been let go using this method?
2. On the other hand, I like the QShortcut method because it seems to notice the key press event before it even gets to any given widget. For example, let's say you have several buttons on your interface. By default, the arrow keys are set to move from button to button. If you define a QShortcut for the Up arrow, the Up arrow will no longer select another button. Nice and clean.
3. The Event Filter method is pretty much the ultimate solution for these problems. Build a filter that can handle all the keypresses, and install the filter on all your widgets. But MAN, that's a pain! I don't want to run around, trying to make sure I have the filter installed on every new widget! I don't want to have to keep track of which widgets should have the filter, and which need to keep the default behavior! If I install the filter ONLY in the base class (say, the window widget), then arrow keys (for example) still get caught by the button widgets and never make it to the filter. Which makes arrow keys select widgets instead of firing an event. Annoying.
So how do I design this application? How do you design shortcut events without it becoming a bookkeeping nightmare?
Thanks for the help!
Bookmarks