Qt KeyPress event for QlineEdit

i have searched the net for how to capture the keypress event for a specific QWidget only (QlineEdit) one way to make it inherit from this class and move the virtual keyPress function, but i cant do that as i am using QtDesigner (can i do this with with QtDesigner?)

I also tried to survive the KeyPress event on all windows, but I only need to filter out events when a specific lineEdit function is active and I couldn't find a way to do this (but there must be a way)

over all, what is the best way to pick up this problem? thank:)

+3


source to share


1 answer


No, you cannot do it with Designer

. If you don't want to use inheritance, you should use an 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 must also:



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

      

and

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

      

+4


source







All Articles