Unsolved Where is the Esc key press event?
-
I have an application with an editable QComboBox. Using an event filter, I've found out that the QComboBox object itself receives the keyPress/keyRelease events (its lineEdit does not). However, neither receives any key events when I press Esc.
Curiously, this event does not even pass through QApplication::notify. How so? How can I detect and handle it?
-
Hi
The combobox itself have an event filter to catch clicks on the
viewport that is the dropdown list.
Since esc on a "open" combox, closes the dropdown, its very likely
that its keyevent handle it.you can try put a event filter on
QAbstractItemView * QComboBox::view() const
and see what comes in.
They are called in reverse order of install so you should be called first. -
@mrjj said in Where is the Esc key press event?:
you can try put a event filter on
QAbstractItemView * QComboBox::view() const
and see what comes in.
They are called in reverse order of install so you should be called first.Nope, it's not there, either.
Isn't it weird that even QApplication::notify does not receive this event? -
Any advice for me? Sorry to be annoying, but this is required for an important feature of my pet project. And even more importantly, I just don't see why it should be impossible to handle this key.
-
Waiting 3 days and then ask is not annoying :)
I would go look at the combo box source to be sure its as expected and then i would try to installing an event filter on the qApp object and look for QEvent::KeyPress .
Did you already try that ? I mean filter directly on Application?
-
@mrjj said in Where is the Esc key press event?:
Did you already try that ? I mean filter directly on Application?
I have tried that just now, it also does not detect this event. Which is not surprising, given that I already tried QApplication::notify.
Time to look at the QComboBox source, I guess :/ Not my favorite part of Qt development, sifting through heaps of unfamiliar code written in a style that I find hard to read :)
-
You might want to try installing the event filter on the the combobox->view() object.
Using this I can get the esc key press event. The esc key release event shows up in the parent combo box. -
@Violet-Giraffe
Purely, purely OOI, what is the important feature that your pet project would like to do when the user presses Esc to dismiss a combo? :) -
@mranger90 said in Where is the Esc key press event?:
You might want to try installing the event filter on the the combobox->view() object.
Using this I can get the esc key press event. The esc key release event shows up in the parent combo box.Nothing. No press, no release.
I have installed the filter to the combobox object, to its view, and to its lineEdit. The filter looks like this:if (e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease) { QKeyEvent * keyEvent = dynamic_cast<QKeyEvent*>(e); qDebug() << keyEvent; {
-
Tried the same in a new empty project, Esc is detected no problem there.
So what, my application is doing something that prevents even QApplication::notify from seeing this event? How is that even possible? -
-
@JNBarchan said in Where is the Esc key press event?:
@Violet-Giraffe
https://stackoverflow.com/a/27607947/489865
?Yes, that is exactly what I did. I already use it for catching another elusive event, so I know
notfy()
itself is working. -
@Violet-Giraffe
I meant (I could be way out, mind...) that you asked how the existing application code might have already done an override and grabbed away the ESC ?So what, my application is doing something that prevents even QApplication::notify from seeing this event? How is that even possible?
-
@JNBarchan said in Where is the Esc key press event?:
@Violet-Giraffe
I meant (I could be way out, mind...) that you asked how the existing application code might have already done an override and grabbed away the ESC ?So what, my application is doing something that prevents even QApplication::notify from seeing this event? How is that even possible?
Oh! No, I have looked very closely at my
notify()
to make sure it doesn't do anything unexpected. It really does not:inline bool notify(QObject * receiver, QEvent * e) override { if (e->type() == QEvent::KeyPress) { QKeyEvent * keyEvent = static_cast<QKeyEvent*>(e); if (keyEvent->key() == Qt::Key_Tab) { // Handles Tab press.... return true; } } return QApplication::notify(receiver, e); }
-
Here's what worked for me. I was able to get the esc key event with the drop down active using this code....
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_comboBox(nullptr) { ui->setupUi(this); m_comboBox = ui->comboBox; m_comboBox->addItem("Summer"); m_comboBox->addItem("Rose"); m_comboBox->addItem("Gimpy"); m_comboBox->installEventFilter(this); m_comboBox->view()->installEventFilter(this); } MainWindow::~MainWindow() { delete ui; } bool MainWindow::eventFilter(QObject *obj, QEvent *ev) { if (obj == m_comboBox) { if (ev->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev); qDebug() << "Key Press event for " << keyEvent->key(); } if (ev->type() == QEvent::KeyRelease) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev); qDebug() << "Key Release event for " << keyEvent->key(); } return false; } else if (obj == m_comboBox->view()) { if (ev->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev); qDebug() << "View Key Press event for " << keyEvent->key(); } if (ev->type() == QEvent::KeyRelease) { QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev); qDebug() << "View Key Release event for " << keyEvent->key(); } return false; } else { return QMainWindow::eventFilter(obj, ev); } }
-
@mranger90 said in Where is the Esc key press event?:
Here's what worked for me. I was able to get the esc key event with the drop down active using this code....
So I've tried exactly that already, and it doesn't work in my target application, while it does work in a new test application. I can't understand why.
Meanwhile, if I install the native event filter to QApplication, I can detect the Exc press there. But this is not a solution, implementing such a minute thing thrice (for each major desktop OS).