LKBEN11646: How to make a window to always stay on top with c++ and Qt?


Symptom

You need a window which will stay above all other windows like taskmanager.

Cause

Sometimes this makes sense.

Solution

To create a window which will stay on top, you can use window flags. When you have a QMainWindow, you can set window flags with the function setWinowFlags().

The following line will make the window stay above other windows.

setWindowFlags(Qt::WindowStaysOnTopHint);

Please note the consequenses when using a QMessageBox. The messageboxes need to have a parent. If you create them without the parent and this parentwindow stays on top, you will have the problem that your messagebox is behind the mainwindow. This will make it impossible to interact with it. Creating a messagebox with the parent set to the mainwindow will prevent this problem.

Here an example:

QMessageBox msgBox(this); //will work

QMessageBox msgBox(); //you might not be able to interact with it

Have fun.

Disclaimer:

The information provided in this document is intended for your information only. Lubby makes no claims to the validity of this information. Use of this information is at own risk!

About the Author

Author: Wim Peeters - Keskon GmbH & Co. KG

Wim Peeters is electronics engineer with an additional master in IT and over 30 years of experience, including time spent in support, development, consulting, training and database administration. Wim has worked with SQL Server since version 6.5. He has developed in C/C++, Java and C# on Windows and Linux. He writes knowledge base articles to solve IT problems and publishes them on the Lubby Knowledge Platform.

Latest update: 27.01.2023