Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Use eventFilter with Shift-Tab
Forum Updated to NodeBB v4.3 + New Features

Use eventFilter with Shift-Tab

Scheduled Pinned Locked Moved General and Desktop
eventfiltershift+tabwindows
3 Posts 2 Posters 3.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    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

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Mue22
      wrote on last edited by Mue22
      #2

      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.

      i can&#x27;t use english well...

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved