PDA

View Full Version : Some troubles with keyPressEvent



code_err
3rd March 2012, 00:46
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.



void PicDevWindow::keyPressEvent(QKeyEvent *event)
{
if(event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
qDebug() << "Ate key press" << keyEvent->key();

}
}


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?

ChrisW67
3rd March 2012, 01:33
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.

code_err
3rd March 2012, 02:28
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 :-)