Results 1 to 1 of 1

Thread: keyPressEvent Key_Up/Down/Right//Left

  1. #1
    Join Date
    Apr 2020
    Location
    Lithuania
    Posts
    24
    Thanks
    17
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: keyPressEvent Key_Up/Down/Right//Left

    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.

    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event)
    2. {
    3. switch (event->key()) {
    4. case Qt::Key_Up:
    5. if (current_square_y != 0 && is_legal_move(current_square_y, current_square_x, current_square_y -1, current_square_x)){
    6.  
    7. current_square_y--;
    8.  
    9. background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
    10. background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
    11. previous_square_cord = {current_square_y, current_square_x};
    12.  
    13. square_moved++;
    14.  
    15. }break;
    16. case Qt::Key_Down:
    17. if (current_square_y != maze_height - 1 && is_legal_move(current_square_y, current_square_x, current_square_y + 1, current_square_x)){
    18.  
    19. current_square_y++;
    20.  
    21. background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
    22. background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
    23. previous_square_cord = {current_square_y, current_square_x};
    24.  
    25. square_moved++;
    26.  
    27. }break;
    28. case Qt::Key_Right:
    29. if (current_square_x != maze_width - 1 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x + 1)){
    30.  
    31. current_square_x++;
    32.  
    33. background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
    34. background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
    35. previous_square_cord = {current_square_y, current_square_x};
    36.  
    37. square_moved++;
    38.  
    39. }break;
    40.  
    41. case Qt::Key_Left:
    42. if (current_square_x != 0 && is_legal_move(current_square_y, current_square_x, current_square_y, current_square_x - 1)){
    43.  
    44. current_square_x--;
    45.  
    46. background_color_squares[current_square_y][current_square_x]->setBrush(start_and_path_background_color);
    47. background_color_squares[previous_square_cord.first][previous_square_cord.second]->setBrush(maze_background_color);
    48. previous_square_cord = {current_square_y, current_square_x};
    49.  
    50. square_moved++;
    51. }break;
    52. }
    53. if (won_game()){
    54. QMessageBox::about(this, "t", "You won!!!");
    55. on_pushButton_3_clicked();
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 


    Added after 29 minutes:


    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 focusPolicy "setFocusPolicy" makes to work my Key_Up/Down/Left/Right, maybe someone could explain it, why
    this code enables to work my arrow keys?

    Qt Code:
    1. this->setFocusPolicy(Qt::StrongFocus);
    To copy to clipboard, switch view to plain text mode 
    Last edited by laurynas2003; 29th April 2020 at 19:00.

Similar Threads

  1. Replies: 6
    Last Post: 16th April 2013, 17:13
  2. Replies: 7
    Last Post: 26th April 2012, 15:45
  3. Replies: 4
    Last Post: 29th August 2010, 19:16
  4. Cannot capture Key_Up
    By xenome in forum Qt Programming
    Replies: 7
    Last Post: 10th September 2009, 17:21
  5. Capturing Key_up and Key_down for QTreeView
    By forrestfsu in forum Qt Programming
    Replies: 4
    Last Post: 23rd February 2007, 17:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.