Results 1 to 3 of 3

Thread: Some troubles with keyPressEvent

  1. #1
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Some troubles with keyPressEvent

    I have a simple app with some amount of spinComboBoxes, pushButtons and other elements which can have Focus. App is a single window derived from QWidget and i want it to respond to pressing on arrow keys on keyboard. I tried to do it by reimplementing void keyPressEvent (QKeyEvent * event) function in my main window class and first I set focuses on all elements on Qt::NoFocus, or Qt::ClickFocus because they attract focus from arrows.

    Qt Code:
    1. void PicDevWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3. if(event->type() == QEvent::KeyPress)
    4. {
    5. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    6. qDebug() << "Ate key press" << keyEvent->key();
    7.  
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    I don't understand why when no qui element has fucus i can't get QKeyEvent from arrows, i get key events from most keys on keyboard but not arrows. When i set focus on element like QLineEdit by clicking on it I can get key event from up and down arrows, hmmm but still don't understand how it works.
    Shouldn't it give me all key events from my main window when i define above function in its body?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Some troubles with keyPressEvent

    Your post is self contradictory, you say you set Qt::NoFocus on the child widgets, but then say you give them focus using the arrows. A widget with a focusPolicy() of Qt::NoFocus cannot accept focus by any means.

    If all child widgets disallow focus then the parent widget has focus and sees all events: it's also not a very useful UI. If any child widget has focus then it gets the key press event first and can choose to consume it. The combo and spin boxes use the up and down arrow key presses and will not pass them up to the parent. Both line edits and spin boxes consume left and right arrow events for editing. You can install an event filter on each child widget and process the events before the child sees them: it's in the QObject::installEventFilter() docs (where I suspect you copied your code from).

    If you hijack the arrow keys to drive focus between widgets then your combo/spin boxes and editing of the line edits will be stunted and not what the user expects.

  3. The following user says thank you to ChrisW67 for this useful post:

    code_err (3rd March 2012)

  4. #3
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Some troubles with keyPressEvent

    Your post is self contradictory, you say you set Qt::NoFocus on the child widgets, but then say you give them focus using the arrows. A widget with a focusPolicy() of Qt::NoFocus cannot accept focus by any means.
    No, I wrote that some child widgets have Qt::NoFocus and some have Qt::ClickFocus (QLineEdits, QSpinBoxes). QPushButtons can have Qt::NoFocus focus policy and still i can click on them.

    I want redirect Left and Right key events to my function which invoke some other functions.

    The combo and spin boxes use the up and down arrow key presses and will not pass them up to the parent. Both line edits and spin boxes consume left and right arrow events for editing. You can install an event filter on each child widget and process the events before the child sees them:
    Ohh, it looks like that. Now it makes sense and probably it will be a good idea to do it that way. I can still click on my spinBoxes or lineEdits and steer them with keys and when i press Tab focus gets out and can steer app with arrows.

    If you hijack the arrow keys to drive focus between widgets then your combo/spin boxes and editing of the line edits will be stunted andnot what the user expects.
    I think that user will be satisfied because it's his wish :-)

Similar Threads

  1. FFMPEG Troubles
    By khrave in forum Qt Programming
    Replies: 0
    Last Post: 23rd June 2010, 01:15
  2. makefile troubles
    By Walsi in forum Qt Programming
    Replies: 6
    Last Post: 12th April 2007, 16:12
  3. libqjpeg troubles
    By TheRonin in forum Installation and Deployment
    Replies: 6
    Last Post: 25th August 2006, 16:48
  4. lupdate *.pro troubles
    By jeff_s in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2006, 11:07
  5. QTextEdit troubles?!
    By Jojo in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2006, 17:54

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.