Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved IP Editor Plugin key_tab issues

    General and Desktop
    keypressevent keytab ip editor plugin line edit
    2
    3
    1330
    Loading More Posts
    • 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.
    • Sh1gs
      Sh1gs last edited by

      Hello all,

      I am creating an IP Editor plugin consisting of 4 LineEdits. The methodologies that I've tweaked to make my own are very similar to this topic.

      My Ip Editor looks kind of like this:

      ___.___.___.___

      I am having issues with the Key_Tab function within the eventfilter. This code snippet is taken from the link that I've provided:

      bool IPCtrl::eventFilter(QObject *obj, QEvent *event)
      {
          bool bRes = QFrame::eventFilter(obj, event);
      
          if ( event->type() == QEvent::KeyPress )
          {
              QKeyEvent* pEvent = dynamic_cast<QKeyEvent*>( event );
              if ( pEvent )
              {
                  for ( unsigned int i = 0; i != QTUTL_IP_SIZE; ++i )
                  {
                      QLineEdit* pEdit = m_pLineEdit[i];
                      if ( pEdit == obj )
                      {
                          switch ( pEvent->key() )
                          {
                          case Qt::Key_Left:
                              if ( pEdit->cursorPosition() == 0 )
                              {
                                  // user wants to move to previous item
                                  MovePrevLineEdit(i);
                              }
                              break;
      
                          case Qt::Key_Right:
                              if ( pEdit->text().isEmpty() || (pEdit->text().size() == pEdit->cursorPosition()) )
                              {
                                  // user wants to move to next item
                                  MoveNextLineEdit(i);
                              }
                              break;
      
                          case Qt::Key_0:
                              if ( pEdit->text().isEmpty() || pEdit->text() == "0" )
                              {
                                  pEdit->setText("0");
                                  // user wants to move to next item
                                  MoveNextLineEdit(i);
                              }
                              emit signalTextChanged( pEdit );
                              break;
      
                          case Qt::Key_Backspace:
                              if ( pEdit->text().isEmpty() || pEdit->cursorPosition() == 0)
                              {
                                  // user wants to move to previous item
                                  MovePrevLineEdit(i);
                              }
                              break;
      
                          case Qt::Key_Comma:
                          case Qt::Key_Period:
                              MoveNextLineEdit(i);
                              break;
      
                          default:
                              emit signalTextChanged( pEdit );
                              break;
      
                          }
                      }
                  }
              }
          }
      
          return bRes;
      }
      

      In my function, I am adding

       case Qt::Key_Tab:
      

      in which, I'd like it to have the same functionality as

      case Qt::Key_Right:
      

      Unfortunately, instead of moving to the next LineEdit, the cursor instead moves to the period in between the LineEdits. Does anyone have an idea of why this is?

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @Sh1gs last edited by jsulm

        @Sh1gs You need to return true in your eventFilter for all keys you want to handle by yourself (at least for tab key), else the event will be handled further and default is to move the cursor to the next widget. See documentation (http://doc.qt.io/qt-5/qobject.html#eventFilter):
        "In your reimplementation of this function, if you want to filter the event out, i.e. stop it being handled further, return true; otherwise return false."
        And you should not call QFrame::eventFilter(obj, event) in case the key is one of the keys you're handling by yourself.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        Sh1gs 1 Reply Last reply Reply Quote 1
        • Sh1gs
          Sh1gs @jsulm last edited by

          Thank you @jsulm, I thought I read the documentation through, but apparently not. Also, I'm not calling QFrame. I saw the link I posted and used it as more of a guideline to create an IP Editor for my needs. Thank you for your help :)

          1 Reply Last reply Reply Quote 0
          • First post
            Last post