Results 1 to 5 of 5

Thread: Shortcut key events best practices

  1. #1
    Join Date
    Nov 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Shortcut key events best practices

    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.

    Qt Code:
    1. (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
    Qt Code:
    1. 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!

  2. #2
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: Shortcut key events best practices

    If you want monitor things like how long the user keeps the key in you need to use event filters. QShortcuts won't work for that scenario. I don't exactly understand what your app should do, but you can install an event filter on the QApplication object which will allow you to catch all key events, no matter which widget has focus.

    Hope it helps,
    Cheers
    Jaco

  3. #3
    Join Date
    Nov 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut key events best practices

    Ah! That's a good idea. I'm having trouble making the event filter installer actually install it into the QApplication object (it requires QObject?!); can you give me a hint?

  4. #4
    Join Date
    Nov 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut key events best practices

    I found a decent solution. I just disabled the focus for the forms that got focus, and installed the eventFilter on the class that showed the window. That works well. It just means that I can't have forms in the window that require focus (like a text input form thing). I can probably live with that.

  5. #5
    Join Date
    Jan 2010
    Location
    China Xian
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shortcut key events best practices

    Maybe you can use QxtGlobalShortcut ,It's part of libqxt.
    http://code.google.com/p/h-qt-exercise/downloads/list

Similar Threads

  1. programming practices with connect
    By jajdoo in forum Qt Programming
    Replies: 6
    Last Post: 5th September 2010, 13:19
  2. Best practices concerning QStandardItemModel
    By tim47 in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2010, 05:29
  3. query about best practices
    By Raajesh in forum Qt Programming
    Replies: 3
    Last Post: 13th June 2008, 18:47
  4. Signals/slots best practices?
    By gfunk in forum Qt Programming
    Replies: 5
    Last Post: 18th August 2007, 18:50
  5. Qt <-> Java - Best Practices?
    By mentat in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2006, 02:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.