Reputation: 842
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.
Upvotes: 5
Views: 6310
Reputation: 426
write one simple line in constructor. (no need to include any other headers)
setWindowFlag(Qt::WindowStaysOnTopHint);
Upvotes: 1
Reputation: 842
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.
Upvotes: 6
Reputation: 1473
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);
}
Upvotes: 3
Reputation: 4050
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
Upvotes: -1