Erwan Douaille
Erwan Douaille

Reputation: 583

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.

Upvotes: 1

Views: 2488

Answers (2)

Vinoj John Hosan
Vinoj John Hosan

Reputation: 6893

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().

i.e

setModel(true);//In Constructor

then while calling or invoking the new window,

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

Upvotes: 0

mhcuervo
mhcuervo

Reputation: 2660

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);

Upvotes: 3

Related Questions