Search code examples
c++qtfocusz-order

How to make a `Tool` window always on top of 2 or more Main window?


Fast question

In a QT C++ project, there are 2 main windows (focus can be set indiferently to any of them), and a tool window which shall be on top of the 2 main windows.

How to implement such feature?

Detailed question:

For one main window and one tool window, is quite easy to solve:

#include <QApplication>
#include <QWidget>

int main( int n, char* args[])
{
    QApplication app(n, args);
    QWidget mainWindow;
    QWidget subWindow(&mainWindow);
    subWindow.setWindowFlags(subWindow.windowFlags() | Qt::Tool);
    mainWindow.show();
    subWindow.show();


    return app.exec();
}
  • The main window is always below the tool window.
  • The interaction with the main window is possible
  • minimizing/closing the main window will affect the tool window
  • Another application could cover the main window or both windows (they are not top-most)

I would like the same feature, but having 2 main window. Imagine a Video player in which the tool window provide "play/stop" control over both images:

  • Each main window has the same focus weight: focus could be given to any of them, which would cover the other, but never the tool window.
  • The tool is always on top of the two others.

Solution

  • You can raise() the tool window above the others.

    This may help: void QWidget::raise() documentation

    Also, see the "Note" on that function.