Why keyPressEvent doesn't work?
-
Hi everyone.
I wrote a simple eventFilter, and it works well but I don't understand why a keyPressEvent isn't working.
Indeed, the case Key_Tab works perfectly, but the case Key_Backtab doesn't.
I have testing it and the problem resides on keyPressEvent(keyEvent).
My code is associated with a QComboBox, and particularly it lightens rows forwards but not backwards (in this case the new row is not visually updated).@bool eventFilter(QObject *t, QEvent *e)
{
QKeyEvent keyEvent = static_cast<QKeyEvent>(e);
int key = keyEvent->key();if(e->type() == QEvent::KeyPress) { switch(key) { case Qt::Key_Backtab: //case Qt::Key_Up: { int curIndex = this->currentIndex(); int nextIndex; if (this->currentIndex() == 0) nextIndex = this->count() -1; else { nextIndex = curIndex - 1; } this->setCurrentIndex(nextIndex); QComboBox::keyPressEvent(keyEvent); break; } case Qt::Key_Tab: //case Qt::Key_Down: { int curIndex = this->currentIndex(); int nextIndex; if (this->currentIndex() == this->count() -1 ) nextIndex = 0; else { nextIndex = curIndex + 1; } this->setCurrentIndex(nextIndex); QComboBox::keyPressEvent(keyEvent); break; } } }@
-
Hi,
I wonder if you should not rather test for the shift modifier + Key_Tab (haven't tested it)
Hope it helps
-
I'll test it, but actually the "case statement" works, asf I can confirm that setCurrentIndex works.
-
I didn't understand that phrase. Do you mean that your code is currently working ?
-
Yep, but not perfectly. In my opinion only QComboBox::keyPressEvent(keyEvent) in the Backtab section doesn't work.
My code is thought like this.
I have a comboBox connected to an editor which contains a document. Changing the comboBox's index, the document changes. So, pressing Key_Tab to move forward:document_1 -> document_2 -> document_3
hlight_row_1 -> hlight_row_2 -> hlight_row_3This works perfectly.
Instead, if I press Key_Backtab (backward), the behaviour is:document_1 -> document_3 -> document_2 --------//which is right
hlight_row_1 -> hlight_row_1 -> hlight_row_1 -------//stays stillthe code in the Backtab section, then, seems to be executed, because my documents in the editor are changing backwards, but just the highlighting of the respective rows inside the comboBox popup list doesn't.
Deleting the QComboBox::keyPressEvent(keyEvent) line, I obtain the same exact behaviour. So I thought that for some reason keyPressEvent wasn't work. -
are you sure that the Key_Backtab key is not used or controlled by something else?
-
I am not sure, but if I just delete both these lines from the Backtab case:
@ this->setCurrentIndex(nextIndex);
QComboBox::keyPressEvent(keyEvent);
@
nothing happens any more, and neither documents are any more changing backwards in the Editor (instead, the Tab case still works): completely frozen.So I don't think it is used by anything else, also considering that I have focus on the menu list.
Is it possible that Backtab (recognized as modifier + tab) wants keyEvent->accept()?
-
Why do not you try to do the opposite?
that is, to call a function keypress (which will change the editor or combobox)? -
Thank Salvatello for your suggestion. Did you mean to override the default keyPressEvent? Like this:
@void keyPressEvent(QKeyEvent *e)
{
int key = e->key();switch(key) { case Qt::Key_Backtab: //case Qt::Key_Up: { int curIndex = this->currentIndex(); int nextIndex; if (this->currentIndex() == 0) nextIndex = this->count() -1; else { nextIndex = curIndex - 1; } this->setCurrentIndex(nextIndex); break; } case Qt::Key_Tab: //case Qt::Key_Down: { int curIndex = this->currentIndex(); int nextIndex; if (this->currentIndex() == this->count() -1 ) nextIndex = 0; else { nextIndex = curIndex + 1; } this->setCurrentIndex(nextIndex); break; } } } bool eventFilter(QObject *t, QEvent *e) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e); int key = keyEvent->key(); if(e->type() == QEvent::KeyPress) { this->keyPressEvent(keyEvent); }
@
This code still changes documents in the editor, but it doesn't move the highlight in the menu list forwards and backwards any more.
If I add
@ QComboBox::keyPressEvent(e); @
just after the switch function, I obtain the same behaviour as before, so: documents changed in both directions; selection highlights only if forward direction.
Therefore now my question is more specific: should setCurrentIndex() automatically change the highlight in the menu list to the specific row called?
(If I am not wrong: when calling QComboBox::keyPresseEvent() what I do is asking for the default behaviour of the comboBox relative to Key_Tab. Maybe comboBox has not default behaviour for Key_Backtab? How to lighten manually a row then?) -
I suggest:
@void MainWindows::keyPressEvent(QKeyEvent *e){
if (e->type() == QEvent::KeyPress)
{
QKeyEvent keyEvent = static_cast<QKeyEvent>(e);int keyInt = keyEvent->key(); Qt::KeyboardModifiers modifiers = keyEvent->modifiers();
//For combination of button
if(modifiers & Qt::ShiftModifier)
keyInt += Qt::SHIFT;
if(modifiers & Qt::ControlModifier)
keyInt += Qt::CTRL;
if(modifiers & Qt::AltModifier)
keyInt += Qt::ALT;
if(modifiers & Qt::MetaModifier)
keyInt += Qt::META;if(keyInt==Qt::Key_Backtab) on_spacePushButton_Backtab(); if(keyInt==Key_Tab) on_pushButtonTab(); }
}
void MainWindows::on_spacePushButton_Backtab()
{
.......................
........................
}void MainWindows::on_pushButtonTab()
{
.......................
........................
}@
-
In this case Backtab is not recognized any more by my machine. And therefore the if statement doesn't compile.
But any way, I think that I expressed badly my problem. My keyPress already works. My problem is simply that:
when I setCurrentIndex() in my comboBox, in case the event is Backtab, the highlight in the comboBox doesn't move.
It is simply just the highlight, because all the rest works. Even setCurrentIndex() I am sure works. But:
Tab -> highlight (in comboBox) moves forwards
Backtab -> highlight doesn't move