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?


  • Qt Champions 2016

    @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.


  • Qt Champions 2016

    @Connie
    Hello,
    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.

    eg.

    #include <Windows.h>

    SetForegroundWindow((HWND)winId());
    setWindowFlags(Qt::WindowStaysOnTopHint);

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.