1
votes

I am using editingFinished() signals of QLineEdit to perform an Operation. The documentation says that this signal will be emitted when return or enter key is pressed or when it will lose focus.

It works well with the enter key on the numlock (Windows keyboard), and also when it loses focus, but when i press "return key" on the keyboard, the signal is not emitted. i tried to use the returnPressed() signal, it behaves the same way.

Am i missing something ?

Thank you

1

1 Answers

3
votes

Subclass QLineEdit

Reimplement keyPressEvent()

Catch Qt::Key_Enter pressing and do your job or emit signal yourself

From documentation:

Qt::Key_Return  0x01000004   
Qt::Key_Enter   0x01000005  Typically located on the keypad.

Something like this:

void LineEdit::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Enter)
    {
        //do something
    }
}

If you do not want subclass, you can installEventFilter to your dialog window, catch your lineEdit and check is Qt::Key_Enter was pressed.

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
        if (obj == ui->lineEdit && event->type() == QEvent::KeyPress)
        {
            QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
            if(Qt::Key_Enter == keyEvent->key() )
            {
                qDebug() << "numpad Enter pressed";
            }
        }
}

Don't forget

protected:
bool eventFilter(QObject *obj, QEvent *event);//in header

and

qApp->installEventFilter(this);//in constructor

For example:

void MainWindow::on_lineEdit_returnPressed()
{
    qDebug() << "numpad Enter pressed";
}