How to make Qt Application mainwindow to remain always on top of other windows in Windows OS?
Asked Answered
T

5

5

Platform - Windows 7,8,10

I have created a QApplication from QMainWindow. I want it to remain always on top of all other windows.

I have used Qt flags ( Qt::WindowStaysOnTopHint ) to achieve this. But this Qt flag does not work. The application is a frameless application.

Please find below the code of the constructor of my Qt App.

myApp::myApp(QWidget *parent)
: QMainWindow(parent)
{
setWindowFlags(Qt::Widget |  Qt::FramelessWindowHint);
setWindowFlags(this->windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint );
ui.setupUi(this);
}

How can I make this flag work ?

I have tried all the options suggested by several members of the community. My present code is as follows

Qt::WindowFlags flags = this->windowFlags();
this->setWindowFlags(flags  | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
ui.setupUi(this);

Strange fact is that this never works on my machine. When I create an installer or copy the required files and run on a different machines(Windows 7, 8, 10) then I get my application on top of all other windows. Note: I am using Visual Studio Community Edition 2015 OS - Windows 7 Professional Service Pack 1.

Titanothere answered 21/7, 2016 at 5:53 Comment(0)
T
6

The following code finally worked for me to keep my window always above other windows

SetForegroundWindow((HWND)winId());
Qt::WindowFlags flags = this->windowFlags();
flags = flags & ~Qt::WindowMinimizeButtonHint;
this->setWindowFlags(flags|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint );
ui.setupUi(this);

I have also blocked the minimze option for the window by unsetting the Minimize flag. But still there is one problem. The lower portion of the window goes past the taskbar. I have to click on the application icon to bring the lower portion above the taskbar.

Titanothere answered 28/9, 2016 at 12:18 Comment(1)
Also I'd like to add you need to include "windows.h" to use SetForegroundWindow()Drouin
O
3

To make a window sit on top of all applications.

myApp.h

    class myApp: public QMainWindow
    {
        Q_OBJECT
        public:
        explicit myApp(QWidget *parent = 0);
        ~myApp();
    protected:
        bool event(QEvent *event);
        ----
    };

myApp.cpp

#include <windows.h>
#include <winuser.h>    
myApp::myApp(QWidget *parent): QMainWindow(parent)
{
    setWindowFlags(Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
    ui.setupUi(this);
}
bool myApp::event(QEvent *event){
    switch (event->type())
    {
    case QEvent::Show:
    {
        HWND winHWND =(HWND) winId();
        if( winHWND ){
            qDebug() << endl << "Setting up associated console window ON TOP !";
            SetWindowPos(
                        winHWND, // window handle
                        HWND_TOPMOST, // "handle to the window to precede
                        // the positioned window in the Z order
                        // OR one of the following:"
                        // HWND_BOTTOM or HWND_NOTOPMOST or HWND_TOP or HWND_TOPMOST
                        0, 0, // X, Y position of the window (in client coordinates)
                        0, 0, // cx, cy => width & height of the window in pixels
                        SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW // The window sizing and positioning flags.
                        );
            // OPTIONAL ! - SET WINDOW'S "SHOW STATE"
            ShowWindow(
                        winHWND, // window handle
                        SW_NORMAL // how the window is to be shown
                        // SW_NORMAL => "Activates and displays a window.
                        // If the window is minimized or maximized,
                        // the system restores it to its original size and position.
                        // An application should specify this flag
                        // when displaying the window for the first time."
                        );
            qDebug() << endl << "Done.";
        } else {
            qDebug() << endl << "There is no console window associated with this app :(";
        }
        break;
    }
    default:
        break;
    }

    return QMainWindow::event(event);
}

For more help

Outdo answered 21/7, 2016 at 10:6 Comment(1)
I have tried your solution. I have tried the other answer as well. Nothing works for me properly. Sometimes I get the window as topmost window and sometimes I don't. Is there any way I can debug what's going wrong with the flags ?Titanothere
M
1

write one simple line in constructor. (no need to include any other headers)

setWindowFlag(Qt::WindowStaysOnTopHint);
Mcewen answered 29/8, 2020 at 12:19 Comment(1)
This doesn't work: that's the issue. The issue in the OP is that Windows is ignoring this hint.Skier
J
-1

See:

http://doc.qt.io/qt-5/qt.html http://doc.qt.io/qt-5/qtwidgets-widgets-windowflags-example.html

Use WindowStaysOnTopHint flag. From Qt Doc:

"Informs the window system that the window should stay on top of all other windows. Note that on some window managers on X11 you also have to pass Qt::X11BypassWindowManagerHint for this flag to work correctly."

Your fault is that you called setWindowFlags twice for FramelessWindowHint and WindowStaysOnTopHint respectively. Try:

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

Or you can use the Windows System API:

#include <Windows.h>
....

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

In your .pro file:

win32-g++:LIBS += libUser32
win32-msvc*:LIBS += User32.lib
Jdavie answered 21/7, 2016 at 9:48 Comment(1)
I have tried your solution . Sometimes it is working and sometimes it is not. I don't know why it does not work sometimes.Titanothere
L
-1

You have to set setWindowFlags after ui.setupUi(this);

Lidstone answered 22/7, 2016 at 12:19 Comment(1)
I have tried this option too. All the suggestion which I have tried does not work on my machine but works nicely on other machines.Titanothere

© 2022 - 2024 — McMap. All rights reserved.