Use eventFilter with Shift-Tab
-
Hi there!
I'm having a widget "editWidget" that has in its layout several "editFieldWidget" which have each 2 QLineEdit. My goal is to make Tab and Shift-Tab working for focus change. This means, if I press Tab in the first QLineEdit, focus should jump to the second one (and vv. for Shift-Tab). If Tab is pressed while the focus is in the second QLineEdit, the first QLineEdit in the next editFieldWidget should get focus (and analog for Shift-Tab in the first QLineEdit).
To achieve this, I override eventFilter in editFieldWidget. Pressing Tab will check if the left or the right QLineEdit was triggered and if it's the right one, a signal "pleaseFocusNextFw" is emitted. It causes the parent (editWidget) to give focus to the next editFieldWidget. It works fine.For Shift-Tab, everything works out-of-the-box (no event filter) on Linux. However on Windows, Shift-Tab fails: It writes a normal Tab into the active box. I am unable to get it working with my eventFilter approach.
The following is my code from editFieldWidget:
bool ListEditFieldWidget::eventFilter(QObject *obj, QEvent *event) { if (obj==leftTextEdit_m || obj==rightTextEdit_m) { if (event->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); if(keyEvent->key() == Qt::Key_Tab){ if(keyEvent->modifiers().testFlag(Qt::ShiftModifier)){ if(obj==rightTextEdit_m) focusPreviousChild(); else if(obj==leftTextEdit_m) emit pleaseFocusPrevFw(); qDebug()<<"miiiiau"; }else{ if(obj==leftTextEdit_m) focusNextChild(); else if(obj==rightTextEdit_m) emit pleaseFocusNextFw(); qDebug()<<"wuuufff"; } return true; }else{ return false; } } else { return false; } } else { // pass the event on to the parent class return QWidget::eventFilter(obj, event); } }
Result: wuuufff is printed, but not miiiau.
Why does this not work? And would there be a simpler way to achieve my goal (I feel like I'm doing a little overkill)...
Thanks a lot in advance for any tipps!
Cheers,
Kalsan -
hi.
um... why dont you use "Sendevent"?if tab or backtab key insert, Sendevent to ui->centralwidget, it just operate as tab or backtab.
in eventfileter,
if(evt->key()==Qt::Key_Tab)
{
Qt::KeyboardModifiers km;
QKeyEvent *ra=new QKeyEvent(QEvent::KeyPress,Qt::Key_Tab,km);
QApplication::sendEvent(ui->centralWidget,ra);
return true;
}->like this...
- it tested just before, in simply case that the textedit. i insert layout(with lineedit1, lineedit2) to textedit. and use above code in eventfilter. the focus move (press tab key in textedit or lineedit) as textedit->lineedit1->lineedit2. in case of backtab, lineedit2->lineedit1->textedit.....(Linux)
++ if you want to use shift tab, use Qt::Key_Backtab. and i use this linux, i cant test it in window os.
-
Hi,
thanks for your reply. I have been unable to test it for time reasons and this horror will still be going on for months, keeping me from working at this project. I think it's best to bookmark this page and eventually return to it when times are better.
Have a nice week-end,
Kalsan