Results 1 to 3 of 3

Thread: setFocus() , grabKeyboard(), or event filter?

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default setFocus() , grabKeyboard(), or event filter?

    Hi!

    I am developing a research application for motion control and one of the key features of this software is manual control of a moving vehicle using the arrow keys. Now, keyboard handling in Qt is set up in a way that the focused widget receives the keyboard input first, and then I believe it passes it on to its parent if it ignore()s the event. The issue is that standard elements such as sliders, spinners, dropdowns and scroll areas all consume the arrow keys, and so when the focus is on such an element, I cannot steer my vehicle. So I will have to do something in order for the arrow keys to do what I want them to do no matter where the keyboard focus is. In fact, I am aiming at a solution where with the press of a button, say "K", I can switch to a mode where the arrow keys *always* steer the vehicle no matter where the focus is, and when pressing "K" again, I switch back to the usual way of things and can enjoy ticking sliders with the arrow keys.
    I identified three possible solutions. I can either setFocus() on the main widget where my arrow key presses are handled, I can grabKeyboard() in the main widget, or I can install the main widget as an event filter on the QApplication object.

    1. setFocus(): First of all the "K" has to be ignored by all widgets in order to be routed up to the main widget (this seems to be the case), where then in the keyPressEvent() I can simply call setFocus(). Then the arrow keys would be routed into the main widget, into the same keyPressEvent() that handles the arrows keys, too, of course. The drawback is that I take the focus away from where it was before. So either I accept that, or add the complication of remembering where the focus was and passing it back there when "K" is pressed again.

    2. grabKeyboard(): Essentially the same as the setFocus() method, I grabKeyboard() when "K" was pressed and releaseKeyboard() when it was pressed again, and the focus stays where it is. This sounds better than option 1, except that fishy things happen when I switch to a different window while the keyboard is grabbed in my Qt application. Any thoughts?

    3. Installing a global event filter. I would then use the main widget as an event filter for all events. This would make sure that nothing else can use "K", which is good. It also seems relatively straight forward to either take the arrow key press when "K" is on, or just leave it be when it is off. Only the respect of doing such a global thing and not knowing what unforeseen consequences it may have motivates me to ask this question first.

    Any thoughts?

    Cruz

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: setFocus() , grabKeyboard(), or event filter?

    I would go for the global event filter, maybe even as a plain QObject subclass for only the purpose of controlling the car.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: setFocus() , grabKeyboard(), or event filter?

    Works like a charm and it took less time than writing up my first post. I don't know what the hell I'm doing.

Similar Threads

  1. Event Filter doesnt work on Drop Event
    By DNW in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2015, 06:55
  2. filter mouse event
    By sajis997 in forum Qt Quick
    Replies: 10
    Last Post: 16th February 2015, 07:26
  3. Installing event filter for What's This tool tip...
    By mtnbiker66 in forum Qt Programming
    Replies: 0
    Last Post: 18th September 2012, 19:21
  4. Replies: 2
    Last Post: 12th May 2012, 11:30
  5. Event filter question
    By d_stranz in forum Qt Programming
    Replies: 7
    Last Post: 7th July 2011, 23:08

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.