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. IP Editor Plugin key_tab issues
QtWS25 Last Chance

IP Editor Plugin key_tab issues

Scheduled Pinned Locked Moved Solved General and Desktop
keypresseventkeytabip editorpluginline edit
3 Posts 2 Posters 1.7k Views
  • 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.
  • Sh1gsS Offline
    Sh1gsS Offline
    Sh1gs
    wrote on last edited by
    #1

    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?

    jsulmJ 1 Reply Last reply
    0
    • Sh1gsS Sh1gs

      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?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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

      Sh1gsS 1 Reply Last reply
      1
      • jsulmJ 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.

        Sh1gsS Offline
        Sh1gsS Offline
        Sh1gs
        wrote on last edited by
        #3

        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
        0

        • Login

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