34

This is on PyQt4, Linux and Python 2.5

Can I make PyQt set my window "always on top" over other applications?

For example, in GTK i use the property: Modal.

Now, in PyQt I am using a QWidget, but, I can't find a way to do that.

Any ideas??

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
mRt
  • 1,223
  • 6
  • 18
  • 32

3 Answers3

42

Pass the QMainWindow the WindowStaysOnTopHint window flag (or use setWindowFlags).

As in the name, this is a hint to the windowing manager (not a hard guarantee).

Simplest possible example:

import sys
from PyQt4 import QtGui, QtCore

class mymainwindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)

app = QtGui.QApplication(sys.argv)
mywindow = mymainwindow()
mywindow.show()
app.exec_()
Michael Scheper
  • 6,514
  • 7
  • 63
  • 76
ChristopheD
  • 112,638
  • 29
  • 165
  • 179
  • 5
    Wouldn't the `QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)` be simplified by writing: `super().__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)`? – NuclearPeon Aug 18 '13 at 17:21
  • 1
    Is there a way to make the window pop the the front when its created, but not always stay on top? – mingxiao Nov 01 '13 at 19:26
  • @NuclearPeon It's the same only if you're using single inheritance. https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ – Nuno André Mar 19 '16 at 02:03
26
setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

setwindowaFlags is a method that can call it from form object and just take one parameter is a constant QtCore.Qt.WindowStaysOnTopHint that refer to make your form Stays On Top

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
Abdo
  • 600
  • 6
  • 9
0

For both frameless window(no header) and Always on top.
USE: setWindowFlags(QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.FramelessWindowHint)

NOTE: If you try to set as individually window flags, then frameless window won't work. e.g:

self.setWindowFlags(Qt.FramelessWindowHint)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
Tushar
  • 880
  • 9
  • 17