Qt KeyPress Event on QlineEdit

c++, qt

Solution

No, you can't do this with `Designer`.If you don't want to use inheritance, then you should use event filter. For example:

bool Dialog::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->lineEdit && event->type() == QEvent::KeyPress)
    {
        QKeyEvent *key = static_cast<QKeyEvent *>(event);
        qDebug() << "pressed"<< key->key();
    }
    return QObject::eventFilter(obj, event);
}

To use `eventFilter` you should also:

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

and

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

Problem

i searched over the net on how to capture a key press event only on a specific QWidget ( a QlineEdit ) one way to do is to inherit from that class and over ride the virtual keyPress function , but i cant do that since i'm using QtDesigner ( is it possible to do it with QtDesigner ? ) i also tried to over ride the KeyPress Event on the entire windows but i need to filter only the the events when a specific lineEdit is active which i could not find a way to do so ( but there must be a way ) over all what is the best way to sole this problem ? thanks :)

Original source