qt

How to set focus to the playing field after I have pushed buttons


This is a tetris.

In Qt Designer I added a new Mani Window. Then I added a frame widget on to the window and a couple of Push Buttons (New game and Pause). And promoted the frame to QtGlass class.

Then I organized classes:

class Field : public QMainWindow {
    Q_OBJECT
...

class QtGlass : public QFrame {
        Q_OBJECT    
    ...

After clicking the buttons I would like to immediately return the focus to my playing field, that is frame.

When I'm inside Field class, I can do this:

widget.frame->setFocus();

But when I click a button I seem to be out of the Field class. I seem to be inside QtGlass as in the Signal/Slot editor I connected the events with QtGlass slots.

How can I set the focus?


Solution

  • QFrame doesn't accept focus by default. Add the following line to the constructor of QtGlass:

    setFocusPolicy(Qt::StrongFocus);
    

    Also names of your classes should not start with Q. See this question.