Search code examples
qtwindowfocus

How to set a window as the current one


My Qt app has multiple windows, and sometimes, even though the windows are already open but buried under other windows, the user will select an option to open one from the mainwindow's menubar. In this case, I want to simply bring it up and make it the current one.

Now using QWidget->raise makes this window go on top of all other windows, but it doesn't select it, and that is what I need to do.

I tried QWidget->setFocus but that doesn't do anything.

In the meantime, I am using a combination of QWidget->close followed by QWidget->show, but I would like to know if there is a command to use with raise.

I tried:

someWindow->raise();
someWindow->setFocus(Qt::ActiveWindowFocusReason);

but it didn't work, so I used:

someWindow->close();
someWindow->show();

Solution

  • Try QWidget::activateWindow.

    From the documentation, this function:

    Sets the top-level widget containing this widget to be the active window. An active window is a visible top-level window that has the keyboard input focus.