Further_Reading
Further_Reading

Reputation: 83

How to make application window stay on top in pyqt5?

I'm trying to make a desktop application in pyqt5 that will stay on top of all windows. I've been looking around online and they all say that the solution is to set the window flags using the setWindowFlags(Qt.WindowStaysOnTopHint) method, but this isn't working for me. Is there some other way I can do this?

I'm on Windows 10 and using Python 3.6 + pyqt5 version 5.9.2. My code is as follows:

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys


class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.initUI()
        self.show()

    def initUI(self):
        self.alertWidget = AlertWidget()
        self.setCentralWidget(self.alertWidget)


class AlertWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        grid = QGridLayout()
        self.setLayout(grid)
        grid.setAlignment(Qt.AlignTop)

        self.alertTextBox = QTextEdit()
        grid.addWidget(self.alertTextBox, 0, 0)

if __name__ == '__main__':
        app = QApplication(sys.argv)
        main = Main()
        sys.exit(app.exec_())

Upvotes: 3

Views: 9897

Answers (1)

Hybr13
Hybr13

Reputation: 100

Assuming the rest of your code is good, change the following line of code:

self.setWindowFlags(Qt.WindowStaysOnTopHint)

to the following line of code:

self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.CustomizeWindowHint | QtCore.Qt.WindowStaysOnTopHint)

Link to an answer explaining why the code change above is required for the Qt.WindowStaysOnTop flag to work.

Upvotes: 7

Related Questions