KeyPressEvent
-
Hi guys
So i have this code where i try to press delete from my keyboard and expect the program to catch this event and do something like a MessageBox or a qDebug.
It doesn't give me error but doesn't work either.
I don't understand what i'm doing wrong.Main Window.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTableWidget> #include <QTableWidgetItem> #include <QListWidgetItem> #include <QKeyEvent> #include <QMessageBox> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); private slots: void on_keyPressEvent(QKeyEvent *event); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_HMain Window .cpp
void MainWindow::on_keyPressEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Delete){ QMessageBox::information(this,"Hello","You Have pressed delete"); qDebug()<<"do some code here "; }If anyone knows what's missing i would really appreciate some help, thanks.
-
Hi guys
So i have this code where i try to press delete from my keyboard and expect the program to catch this event and do something like a MessageBox or a qDebug.
It doesn't give me error but doesn't work either.
I don't understand what i'm doing wrong.Main Window.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTableWidget> #include <QTableWidgetItem> #include <QListWidgetItem> #include <QKeyEvent> #include <QMessageBox> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); private slots: void on_keyPressEvent(QKeyEvent *event); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_HMain Window .cpp
void MainWindow::on_keyPressEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Delete){ QMessageBox::information(this,"Hello","You Have pressed delete"); qDebug()<<"do some code here "; }If anyone knows what's missing i would really appreciate some help, thanks.
@ApprenticeReno Key press event (as its name suggests) is an event, not a signal. So, you need to override the https://doc.qt.io/qt-6/qwidget.html#keyPressEvent method in your subclass.
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); protected: void keyPressEvent(QKeyEvent *event) override; -
@ApprenticeReno Key press event (as its name suggests) is an event, not a signal. So, you need to override the https://doc.qt.io/qt-6/qwidget.html#keyPressEvent method in your subclass.
class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); protected: void keyPressEvent(QKeyEvent *event) override;@jsulm I thought there was no difference between a signal and an event, my bad.
//mainwindow.h protected: void keyPressEvent(QKeyEvent *event) override;And
//mainwindow.cpp void MainWindow::keyPressEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Delete){ QMessageBox::information(this,"hello","you pressed delete"); } }Tried this way and it works. @jsulm Thank you very much.
-
@jsulm I thought there was no difference between a signal and an event, my bad.
//mainwindow.h protected: void keyPressEvent(QKeyEvent *event) override;And
//mainwindow.cpp void MainWindow::keyPressEvent(QKeyEvent *event){ if(event->key() == Qt::Key_Delete){ QMessageBox::information(this,"hello","you pressed delete"); } }Tried this way and it works. @jsulm Thank you very much.
@ApprenticeReno
Qt uses a convention that if a method name ends inEventit is avirtualmethod you canoverride, and not a signal/slot. There has long been debate about which "happenings" should be "events" versus "signals", not for you to worry, just keep an eye out for thatEventin the name :)