Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

KeyPressEvent onyl works after button is clicked.



  • KeyPressEvent only works after button is clicked, is it supposed to work that way? Or I did some mistakes in the code, the code:

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QObject>
    #include <QWidget>
    #include <QtWidgets>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private:
    
        Ui::MainWindow *ui;
    };
    
    class keyEnterReceiver : public QObject
    {
        Q_OBJECT
    public:
      keyEnterReceiver(QWidget *parent = 0);
    
    
    protected:
        bool eventFilter(QObject* obj, QEvent* event);
    };
    #endif // MAINWINDOW_H
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QKeyEvent>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        keyEnterReceiver* key = new keyEnterReceiver();
        ui->pushButton_2->installEventFilter(key);
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    keyEnterReceiver::keyEnterReceiver(QWidget *parent)
        : QObject(parent)
    {
    
    }
    
    
    bool keyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
    {
        if (event->type()==QEvent::KeyPress) {
            QKeyEvent* key = static_cast<QKeyEvent*>(event);
            if ( (key->key()==Qt::Key_B) || (key->key()==Qt::Key_C) ) {
                
                qDebug() << "The keys were pressed" << key;
            } else {
                return QObject::eventFilter(obj, event);
            }
            return true;
        } else {
            return QObject::eventFilter(obj, event);
        }
        return false;
    }
    
    

    mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QPushButton" name="pushButton">
        <property name="geometry">
         <rect>
          <x>10</x>
          <y>30</y>
          <width>91</width>
          <height>41</height>
         </rect>
        </property>
        <property name="text">
         <string>PushButton</string>
        </property>
       </widget>
       <widget class="QPushButton" name="pushButton_2">
        <property name="geometry">
         <rect>
          <x>10</x>
          <y>80</y>
          <width>91</width>
          <height>41</height>
         </rect>
        </property>
        <property name="text">
         <string>PushButton</string>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>21</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

    Thanks


  • Lifetime Qt Champion

    Hi
    A widget only receives keyboard events if it has the focus.
    So clicking the button first will focus it and then it will get keys.

    So yes its working as intended :)


  • Lifetime Qt Champion

    Hi
    A widget only receives keyboard events if it has the focus.
    So clicking the button first will focus it and then it will get keys.

    So yes its working as intended :)



  • @mrjj Thanks, I solved by setting the focus ... ui->pushButton_2->setFocus();


Log in to reply