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. Keyboard focus lost when setCentralWidget(view)

Keyboard focus lost when setCentralWidget(view)

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 3.8k 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.
  • jaramos2409J Offline
    jaramos2409J Offline
    jaramos2409
    wrote on last edited by
    #1

    I am attempting to create a game in Qt. I have a Main Menu created using a form and when you click on a Start Game button, I create a QGraphicsView along with everything else necessary to create a QGraphicsScene, and set the QGraphicsView as the centralWidget. This loads the game.

    The problem I am having is that, after setCentralWidget(gameView), I have to click on the screen again in order for the keyboard input to work for the game. Is there a way to set the keyboard focus on the gameView after setCentralWidget? The keyboard focus eventually goes towards the player, but I'd like to be able to move as soon as the view is set as central widget.

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

      Hi
      Have you tried with setFocus?
      http://doc.qt.io/qt-5/qwidget.html#setFocus

      jaramos2409J 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Have you tried with setFocus?
        http://doc.qt.io/qt-5/qwidget.html#setFocus

        jaramos2409J Offline
        jaramos2409J Offline
        jaramos2409
        wrote on last edited by
        #3

        @mrjj I have tried to setFocus on the view before and after setCentralWidget(gameView) and nothing changed in either attempt.

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

          ok.
          impossible to guess what going on.

          Normally setFocus works so something steals your focus.

          try to list who has focus with
          http://doc.qt.io/qt-5/qapplication.html#focusWidget

          before u click.
          you can use QDebug()

          jaramos2409J 1 Reply Last reply
          0
          • mrjjM mrjj

            ok.
            impossible to guess what going on.

            Normally setFocus works so something steals your focus.

            try to list who has focus with
            http://doc.qt.io/qt-5/qapplication.html#focusWidget

            before u click.
            you can use QDebug()

            jaramos2409J Offline
            jaramos2409J Offline
            jaramos2409
            wrote on last edited by
            #5

            @mrjj It seems QObject 0x0 has focus which, from what I can tell, is the parent of the QMainWindow which is the only one I use in my project.

            mrjjM 1 Reply Last reply
            0
            • jaramos2409J jaramos2409

              @mrjj It seems QObject 0x0 has focus which, from what I can tell, is the parent of the QMainWindow which is the only one I use in my project.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @jaramos2409
              ok ? normally
              mainwindow dont have parent :)

              you can try to set focus a bit later then constructor

              void Mainwindow::showEvent( QShowEvent* event ) {
              QWidget::showEvent( event );
              thegamething->setFocus();
              }

              jaramos2409J 1 Reply Last reply
              0
              • mrjjM mrjj

                @jaramos2409
                ok ? normally
                mainwindow dont have parent :)

                you can try to set focus a bit later then constructor

                void Mainwindow::showEvent( QShowEvent* event ) {
                QWidget::showEvent( event );
                thegamething->setFocus();
                }

                jaramos2409J Offline
                jaramos2409J Offline
                jaramos2409
                wrote on last edited by
                #7

                @mrjj Haha sorry. It is late where I am and I found myself misunderstanding.

                Your suggestion did not seem to work. Is it possible I need to make QGraphicsView focusable somehow? It seems to only accept keyboard input after I click on the screen.

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

                  @jaramos2409 said:

                  QGraphicsView

                  Normally the items in side the view get focus but I assume you overrode keypress etc for it
                  and move item as "sprite".

                  what happens when you "click screen" ?
                  You mean u click in the view and then key works?

                  jaramos2409J 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @jaramos2409 said:

                    QGraphicsView

                    Normally the items in side the view get focus but I assume you overrode keypress etc for it
                    and move item as "sprite".

                    what happens when you "click screen" ?
                    You mean u click in the view and then key works?

                    jaramos2409J Offline
                    jaramos2409J Offline
                    jaramos2409
                    wrote on last edited by jaramos2409
                    #9

                    @mrjj Yes yes.

                    Let me clarify what I have done. I have a QGraphicsScene with an object that has inherited QGraphicsPixMapItem inside of it. I have overridden this object's keyPressEvent(event) function in order to move the object move around the screen when Qt::Key_Left is pressed etc. This input isn't responsive after setCentralWidget(gameView) until I click somewhere on the screen. When I click on the screen, I am now able to move the object around pressing the arrow keys.

                    Edit: I feel I should also mention that, after the object I am moving around is constructed, I have indeed done the following:

                    player->setFlag(QGraphicsItem::ItemIsFocusable);
                    player->setFocus();
                    

                    In order to make sure it recieves the input. It was working initially when the gameView was initial screen/centralWidget set in the MainWindow's constructor, but ever since I made it so you switch to the gameView through QWidget based menu, I have had to click on the screen to active input responsiveness.

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

                      hmm and after this menu is closed,
                      you set focus (back) to player or gameview?

                      jaramos2409J 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        hmm and after this menu is closed,
                        you set focus (back) to player or gameview?

                        jaramos2409J Offline
                        jaramos2409J Offline
                        jaramos2409
                        wrote on last edited by jaramos2409
                        #11

                        @mrjj Player is set as the focus before I attempt to set gameView as the centralWidget. There is also a check I run to see if player has the focus once the game is running in gameView. If player has lost focus, then focus is given back to player object.

                        I have been attempting to set focus on gameView in between the initial player setFocus and the focusChk() in order to make initial input in the gameView responsive to no avail.

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

                          Ok.
                          It might be a bug with QGraphics view.
                          with set focus.
                          you can browse
                          https://bugreports.qt.io/secure/Dashboard.jspa
                          to see if can spot anything related.

                          1 Reply Last reply
                          0

                          • Login

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