简体   繁体   中英

QT always on top on windows7/8

I would like to know if it's possible to set my QMainWindow always on top .

I tried:

mainWindow.setWindowFlags(Qt::WindowStaysOnBottomHint);

mainWindow is a QMainWindow extended object. But it doesn't work and my window disapear.

Yes, it is possible but there are two errors in your code:

  1. You are clearing all flags but Qt::WindowStaysOnBottomHint which is set.
  2. You're using Qt::WindowStaysOnBottomHint flag (which represent the opposite of what you want) instead of Qt::WindowStaysOnTopHint .

A correct way of doing that is:

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

Note that on some window managers on X11 you also have to pass Qt::X11BypassWindowManagerHint for this flag to work correctly.

In that case you should do:

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

If you want to make a window as Dialog, there is another way. Just call setModal(true) or setWindowModality(), afterward show(). Unlike exec(), show() returns control to the caller instantaneously.It wont stuck as QDialog in exec().

ie

setModel(true);//In Constructor

then while calling or invoking the new window,

MyWindow* myWindow = new MyWindow(this);
myWindow->show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM