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. Help reqested: very simple keypressevent in mainwindow
QtWS25 Last Chance

Help reqested: very simple keypressevent in mainwindow

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 15.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.
  • N Offline
    N Offline
    naizarak
    wrote on last edited by
    #1

    Hello

    I've been trying to add a basic keyboard input handler to my mainwindow. The mainwindow has a few Qlabels and pushbuttons, nothing fancy.

    I've tried adding the following code:

    mainwindow.h
    @ protected:
    void keyPressEvent(QKeyEvent *event);@

    mainwindow.cpp
    @ void MainWindow::keyPressEvent(QKeyEvent *event)
    {
    std::cout << "pressed";
    }@

    But nothing happens when I press a key. I think there's something very simple that I'm missing, but I just can't figure it out. Trust me I've googled quite a bit on the topic but got nowhere. Any help would be greatly appreciated. Thanks.

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

      Hi and welcome to devnet,

      What is your MainWindow ? A QWidget ? A QMainWindow ?

      Does it get keyboard focus ?

      Typically, if your one of your pushbuttons has the focus, your MainWindow won't receive any keyboard event

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • N Offline
        N Offline
        naizarak
        wrote on last edited by
        #3

        Thanks for the reply.

        My mainwindow class inherits from QMainWindow, and yes indeed, the very first pushbutton appears to receive focus by default.

        Can you tell me what I need to run in order for the overall mainwindow to receive universal focus(if that makes sense).

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          It doesn't really make sense (I.E. if you have QLineEdit, you'd rather want it to have the keyboard focus)

          You can however use your MainWindow as a filter, search for event filter in the documentation.

          Why do you need that "universal focus" ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            naizarak
            wrote on last edited by
            #5

            I looked through the eventfilter documentation, and as I understand the filter must be added to an existing object. I'm not sure how that would work for me because I want the program to just catch keyboard input in general. It doesn't have to be tied to a specific control or GUI element.

            Basically the program is open and running.
            The user presses a key.
            The program should catch this input and go from there.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What kind of key ? A short cut ?

              From what "state" to what "state" ?

              You can filter events for several widgets in the same filter event

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                naizarak
                wrote on last edited by
                #7

                Ah ok I think I'm not elaborating enough.

                Basically my program is an extremely simple game.

                There is a qlabel with a car image.

                When the user presses an arrow key, the car-image label should be moved in an appropriate direction. My entire program consists of nothing but a main file, mainwindow.h inhering from qmainwindow, and the mainwindow.cpp. I'm not using widgets or anything like that.

                This is purely conceptual at this point. I just want the program to be open, the user to press a key, the program to read this and then move the car. I don't know how to explain it any simpler lol.

                Maybe if I just added my code...

                @#ifndef MAINWINDOW_H
                #define MAINWINDOW_H

                #include <QMainWindow>
                #include <QGraphicsScene>
                #include <QGraphicsView>
                #include <QPushButton>
                #include <QMessageBox>
                #include <QPixmap>
                #include <QLabel>
                #include <QTimer>
                #include <QTextEdit>
                #include <QInputDialog>
                #include <QString>
                #include <QEvent>
                #include <QObject>
                #include <QKeyEvent>

                //class GUITile;
                class MainWindow : public QMainWindow {
                Q_OBJECT

                public:
                explicit MainWindow();

                void show();
                

                protected:
                void keyPressEvent(QKeyEvent *event);

                private:
                QString name;
                int roadSpeed;
                int score;
                int speed;
                int lives;

                int roadTimerCount;

                bool gameRunning;
                bool timerRunning;

                QGraphicsScene *scene;
                QGraphicsView *view;
                

                QLabel *nameLabel;
                QTextEdit *scoreField;

                QTextEdit *speedField;
                QTextEdit *livesField;

                QPushButton *startButton;
                QPushButton *pauseButton;
                QPushButton *exitButton;
                QPushButton *aboutButton;

                QMessageBox *aboutBox;

                QPixmap *cityMap;
                QLabel *city;

                QPixmap *roadMap;
                QLabel *road;

                QPixmap *roadMap2;
                QLabel *road2;

                QPixmap *playerMap;
                QLabel *player;

                QTimer *roadTimer;

                private slots:
                void startSlot();
                void pauseSlot();
                void exitSlot();

                void aboutSlot();

                void handleRoadTimer();

                };

                #endif
                @

                And in mainwindow.cpp:
                @ void MainWindow::keyPressEvent(QKeyEvent *event)
                {
                std::cout << "pressed";
                //just to test basic input. the actual event will be filtering for the arrow keys and space bar
                }@

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Ok ! I see, then i think "grabKeyboard":http://qt-project.org/doc/qt-4.8/qwidget.html#grabKeyboard is your friend

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    holygirl
                    wrote on last edited by
                    #9

                    Will an installEventFilter help here?

                    Here's some code which might help.

                    In the constructor

                    @
                    installEventFilter(this)
                    @

                    and then a function

                    @
                    bool MainWindow::eventFilter(QObject *target, QEvent *event) {

                    if (event->type() == QEvent::KeyPress) {
                    
                        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
                    
                        switch (keyEvent->key())
                        {
                    
                              case <whatever key you want>:
                         }
                         return QObject::eventFilter(target, event);
                     }
                    

                    }
                    @

                    Here, the key you wish to map can be specified as

                    @
                    Qt::Key_Up
                    Qt::Key_A
                    .
                    .
                    etc
                    @

                    Hope this is what you were looking for!

                    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