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

keyPressEvent



  • Hello,

    Could someone tell me why my keyPressEvent doesn't work when I use arrow keys?
    When I use Qt::Key_W/S/D/A everything works fine. I also tried to use Key_Up/Down..., this way also didn't work

    void MainWindow::keyPressEvent(QKeyEvent *event)
    {
        switch (event->key()) {
            case Qt::UpArrow:
            if (current_square_y != 0 && is_legal_move(current_square_y, current_square_x, current_square_y -1, current_square_x)){
    
                current_square_y--;
    
                background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
                background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
                previous_square_cord = {current_square_y, current_square_x};
    
                square_moved++;
    
            }break;
            case Qt::DownArrow:
            if (current_square_y != maze_height - 1 && is_legal_move(current_square_y, current_square_x, current_square_y + 1, current_square_x)){
    
                current_square_y++;
    
                background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
                background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
                previous_square_cord = {current_square_y, current_square_x};
    
                square_moved++;
    
            }break;
            case Qt::RightArrow:
            if (current_square_x != maze_width - 1 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x + 1)){
    
                current_square_x++;
    
                background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
                background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
                previous_square_cord = {current_square_y, current_square_x};
    
                square_moved++;
    
            }break;
    
            case Qt::Key_Left:
            if (current_square_x != 0 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x - 1)){
    
                current_square_x--;
    
                background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
                background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
                previous_square_cord = {current_square_y, current_square_x};
    
                square_moved++;
            }break;
        }
        if (won_game()){
            QMessageBox::about(this, "t", "You won!!!");
            on_pushButton_3_clicked();
        }
    }
    


  • Qt::UpArrow/Qt::DownArrow/Qt::RightArrow are not Keys.
    I think it should be Key_Up/Down, but you said you already tried that.

    Step 1: Print log in the keyPressEvent no matter what key it is.
    Then try to press up/down to see is there any log from keyPressEvent.
    If there is, then it is simple, you can print the key value in the log.

    Step 2: If there is no log, then maybe the up/down events are handled by other widgets.
    If the MainWindow is the only widget that need to response to key events, you could use

    void QWidget::grabKeyboard()
    

    to make sure all key events go through it.



  • How can I print to log to check which key is pressed? I know that I can use qDebug to print to console, but how to print a key which is pressed?



  • oh I figured out how to print to log to check which key is pressed:D
    But it didn't work:/, only with normal keys.

    if (event->key () == Qt::Key_Up){
            qDebug() << "working";
        }
    


  • Okay, so I figured out how to make it to work, I changed my widget focus to StrongFocus, but
    I don't understand why this "setFocusPolicy" makes to work my Key_Up/Down/Left/Right, maybe someone could explain it?

    this->setFocusPolicy(Qt::StrongFocus);
    


  • @privatepepper In this code that you shared:

    this->setFocusPolicy(Qt::StrongFocus);
    

    What is the type of "this"? You mentioned it was a widget. Is it a QWidget? If so, then this should be the relevant doc:

    That doc mentions that the default is Qt::NoFocus, and that "You must enable keyboard focus for a widget if it processes keyboard events."

    Does that help?

    I ran into similar confusion, but in QML. I just reread my notes about QML focusPolicy, but the QML related issues seem unlreated to yours. I was hoping maybe some of my "struggle" could translate back to the non-QML world. But I guess you solved your problem anyway, so congrats!



  • @KH-219Design

    Thanks for the reply :) , but "this" is QMainWindow, oh and good luck with your problem:)



  • @privatepepper ah. Well based on the doc for QMainWindow:

    // QMainWindow Class
    
    // The QMainWindow class provides a main application window. More...
    
    // Header:	#include <QMainWindow>
    
    // Inherits:	QWidget
    

    Via inheritance, it still "is a" QWidget. So I still think the relevant doc to explain your outcome is:

    https://doc.qt.io/qt-5/qwidget.html#focusPolicy-prop



  • @KH-219Design

    Oh:D, thanks for the reply again:), I am such a newbie


Log in to reply