6

This is a bit of a beginners question but I don't find the solution.

I'm using an own object that inherits from QLineEdit and reiceves numbers as input (which works smoothly now).

Now I want to receive an event, when the user presses the Escape-button. This does not happen with the textChanged()-event. According to the documentation there is no special escape-event. So how else can this be done?

Thanks!

Nejat
  • 31,784
  • 12
  • 106
  • 138
Elmi
  • 5,899
  • 15
  • 72
  • 143
  • 1
    Each `QWidget` (and the `QLineEdit` is one) has a method called [`keyPressEvent`](http://doc.qt.io/qt-5/qwidget.html#keyPressEvent). In your own subclass, you can overload it and check if `event->key()` is the Escape key. But don't forget to call the base class `keyPressEvent` afterwards. – Bowdzone Jan 28 '15 at 10:12

2 Answers2

3

I had this same problem. I am solving it by implementing keyPressEvent in my QMainWindow.

void MainWindow::keyPressEvent(QKeyEvent *e)
{
    if (e->key() == Qt::Key_Escape) {
        QLineEdit *focus = qobject_cast<QLineEdit *>(focusWidget());
        if (lineEditKeyEventSet.contains(focus)) {
            focus->clear();
        }
    }
}

And setting up QSet<QLineEdit *> lineEditKeyEventSet to contain the QLineEdits that need this behavior.

void MainWindow::setupLineEditKeyEventList()
{
    lineEditKeyEventSet.insert(ui->lineEdit_1);
    lineEditKeyEventSet.insert(ui->lineEdit_2);
    lineEditKeyEventSet.insert(ui->lineEdit_3);
}
schmitt
  • 338
  • 2
  • 5
2

You can either implement keyPressEvent :

void LineEdit::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Escape)
    {
        ...
    }

    QLineEdit::keyPressEvent(event);
}

Or implement eventFilter :

bool LineEdit::eventFilter(QObject  *obj, QEvent * event)
{

    if((LineEdit *)obj == this && event->type()==QEvent::KeyPress && ((QKeyEvent*)event)->key() == Qt::Key_Escape )
    {
        ...
    }

    return false;
}

When using the eventFilter approach, install the event filter in the constructor :

this->installEventFilter(this);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • Does not work. I get events at both functions but not when Esc-key is pressed. My class does not inherit directly from QLineEdit, is there a possibility somebody else eats this event? If yes, how could one suppress functionality of keyPressEvent()/eventFilter()? – Elmi Jan 28 '15 at 11:58
  • Nejat: I found the Escape-event is sent to the LineEdits parent window, not to the line edit field itself...any possibility to forward it to the LineEdit object too? – Elmi Jan 28 '15 at 12:04
  • Then why don't you handle the event for pressing Escape in that window? – Nejat Jan 28 '15 at 12:07
  • Nejat: and then do what with this event - send it to all child widgets because one of them needs it? I can't imagine that there is no way to get Esc in a child widget... – Elmi Jan 28 '15 at 12:54
  • If you need to handle it in line edit you can post an event to the child widget when you catch it in the window. You can post a key press event like : http://stackoverflow.com/questions/24322602/qt-sending-keypressevent/24325983#24325983 – Nejat Jan 28 '15 at 12:58
  • Nejat: I understand this concept, but isn't this a bit complicated for such a simple task? Means an application either has to post it to _every_ child widget (which is wasting of resources when one widget is interested in the event and 99 receive it just to do nothing with it) or a functionality for registering child widgets has to be implemented so that the event is posted to the registered widgets only (which is some kind of overkill). Don't misunderstand me, I blame Qt for that, not you! – Elmi Jan 28 '15 at 13:19