Selective keyPressEvent, filter for dedicated sender QLineEdit



  • I've created a QWidget, which contains three QLineEdits. Then I added a overwrote the keyPressEvent so that this lineEdit_3 reacts on key press. Working good.

    void MySuperWidget::keyPressEvent(QKeyEvent* keyEv) {
    switch (keyEv->key()) {
    case Qt::Key_Up:
    //.. stuff
    break;

    case Qt::Key_Down: {
        //.. stuff
        }
        break;
      default:
        break;
    }
    

    }

    BUT the first and second QLineEdit also react on keypress :(

    I need somethng like this:

    if (sender() != ui->lineEdit_3 ) {
    keyEv->ignore();
    }



  • You need to accept the event so the parent knows that the event has been handled. Otherwise the event will go all children until someone accepts it or if none do, it will be ignored.
    so keyEv->accept() should do the trick.


Log in to reply
 

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