How to make a Window always stay above the others in PYQT5?

Asked

Viewed 27 times

0

I am creating a small application and need the main window to always be above the others, which attribute to use in pyqt 5 ? From now on I thank

1 answer

1

There is no guarantee that it will stay on top always, especially if other programs use the same feature, I’ll have to be a little more honest about it, programs at the top have always bothered me, I’m the user, I know what I want at the top and what I don’t want, of course if it is an option within the program chosen by the user it would be acceptable to use it, but if it is forcibly and not a user option I would refuse to use such a program.

Going back to the technical part, basically the flag to leave at the top is Qt.WindowStaysOnTopHint

<objeto>.setWindowFlags(<objeto>.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)

Change <objeto> by the context (widget or Qmainwindow) it will apply, remembering that the same program can have multiple windows, a basic example:

from PyQt5 import Qt, QtCore, QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)

w = QtWidgets.QWidget()
w.setWindowFlags(w.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
w.resize(800, 600)
w.show()
sys.exit(app.exec_())

Note that I used .windowFlags() to get the default flags, then with the | "match" with QtCore.Qt.WindowStaysOnTopHint.

Almost everything from Qt in C++ in applicable similarly in Pyqt5 and as an alternative to Pyqt there is also pyside, which is quite similar, so consult the official Qt documentation can help you:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.