Keyboard focus lost when setCentralWidget(view)



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    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()



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


  • Qt Champions 2016

    @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();
    }



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

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



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


  • Qt Champions 2016

    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.


Log in to reply
 

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