Nominate our 2022 Qt Champions!

QMainWindow windows always on top not working sometimes

  • On windows 7 I have problem with window flag. I added this line in the constructor

    setWindowFlags(Qt::WindowStaysOnTopHint );

    when windows is going to render (after ctrl + r in qt creator) if suddenly i click on somewhere on text editor(qt creator text editor or in notepad if is open) this flag is not working correctly and rendered window stays on background. I found windows this solution:

    if(GetForegroundWindow() != reinterpret_cast<HWND>(winId())){
       qDebug() << "Here is my problem";

    but now If I set again it doesn't working and window anyway stays background.

    setWindowFlags(Qt::WindowStaysOnTopHint );

    Can someone help me?

  • Lifetime Qt Champion

    @Giorgi said:

    setWindowFlags(Qt::WindowStaysOnTopHint );

    Works here on win 7, when set on the mainwindow. never falls behind anything.

    Have you tried with a new project and only
    setWindowFlags(Qt::WindowStaysOnTopHint );
    in constructor ?

    also ctrl +r is run app with no debug. Guess you can call it render :)

  • Hi,

    Have you found the solution to your problem? I am also having the same problem and am still cracking my head to solve this. If anyone has the solution, please post it here.

  • Moderators

    Where are you setting the hint? Is it in the constructor? It has always worked for me, but there maybe some subtlety in your code.

  • @Giorgi

    MSDN said: To use SetWindowPos to bring a window to the top, the process that owns the window must have SetForegroundWindow permission.


    #include <Windows.h>


    // in .pro
    win32-g++:LIBS += libUser32
    win32-msvc*:LIBS += User32.lib

Log in to reply