简体   繁体   中英

Qt KeyPress Event on QlineEdit

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM