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 onyl works after button is clicked.
Qt 6.11 is out! See what's new in the release blog

KeyPressEvent onyl works after button is clicked.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.0k Views 1 Watching
  • 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.
  • U Offline
    U Offline
    Ucn_
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 :)

      U 1 Reply Last reply
      2
      • mrjjM mrjj

        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 :)

        U Offline
        U Offline
        Ucn_
        wrote on last edited by
        #3

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

        1 Reply Last reply
        1

        • Login

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