PyQt: Always on top
Asked Answered
L

3

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??

Loincloth answered 17/12, 2009 at 22:22 Comment(0)
A
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_()
Acaleph answered 17/12, 2009 at 22:32 Comment(3)
Wouldn't the QtGui.QMainWindow.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint) be simplified by writing: super().__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)?Giltedged
Is there a way to make the window pop the the front when its created, but not always stay on top?Level
@Giltedged It's the same only if you're using single inheritance. rhettinger.wordpress.com/2011/05/26/super-considered-superTonguelashing
T
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

Torey answered 9/5, 2013 at 9:55 Comment(1)
maybe you can add a one line explication ?Ramirez
S
2

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: Below code won't result in frameless and Always on Top window

self.setWindowFlags(Qt.FramelessWindowHint)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
Standley answered 1/6, 2023 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.