Unsolved Where is the Esc key press event?
-
@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).