IP Editor Plugin key_tab issues



  • 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?


  • Moderators

    @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.



  • 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 :)



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.