Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. KeyPressEvent
Qt 6.11 is out! See what's new in the release blog

KeyPressEvent

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ApprenticeRenoA Offline
    ApprenticeRenoA Offline
    ApprenticeReno
    wrote on last edited by
    #1

    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_H
    
    

    Main 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.

    jsulmJ 1 Reply Last reply
    0
    • ApprenticeRenoA ApprenticeReno

      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_H
      
      

      Main 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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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;
      
      

      See also https://doc.qt.io/qt-6/eventsandfilters.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      ApprenticeRenoA 1 Reply Last reply
      1
      • jsulmJ jsulm

        @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;
        
        

        See also https://doc.qt.io/qt-6/eventsandfilters.html

        ApprenticeRenoA Offline
        ApprenticeRenoA Offline
        ApprenticeReno
        wrote on last edited by
        #3

        @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.

        JonBJ 1 Reply Last reply
        2
        • ApprenticeRenoA ApprenticeReno

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @ApprenticeReno
          Qt uses a convention that if a method name ends in Event it is a virtual method you can override, 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 that Event in the name :)

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved