Search code examples
c++windowsqtwindow

QT always on top on windows7/8


I would like to know if it's possible to set my QMainWindow always on top .

I tried:

mainWindow.setWindowFlags(Qt::WindowStaysOnBottomHint);

mainWindow is a QMainWindow extended object. But it doesn't work and my window disapear.


Solution

  • Yes, it is possible but there are two errors in your code:

    1. You are clearing all flags but Qt::WindowStaysOnBottomHint which is set.
    2. You're using Qt::WindowStaysOnBottomHint flag (which represent the opposite of what you want) instead of Qt::WindowStaysOnTopHint.

    A correct way of doing that is:

    Qt::WindowFlags flags = mainWindow.windowFlags();
    mainWindow.setWindowFlags(flags | Qt::WindowStaysOnTopHint);
    

    Note that on some window managers on X11 you also have to pass Qt::X11BypassWindowManagerHint for this flag to work correctly.

    In that case you should do:

    Qt::WindowFlags flags = mainWindow.windowFlags();
    mainWindow.setWindowFlags(flags | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint);