Help reqested: very simple keypressevent in mainwindow



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


  • Lifetime Qt Champion

    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



  • 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).


  • Lifetime Qt Champion

    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" ?



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


  • Lifetime Qt Champion

    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



  • 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
    }@


  • Lifetime Qt Champion

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



  • 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!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.