How to Keep PyQt6 Window Always on Top on macOS
Unsolved
Qt for Python
-
I've tried many times and consulted ChatGPT, but when I switch to another app (like Chrome), my window gets covered.
Even though I have added Qt.WindowType.WindowStaysOnTopHint, the window still gets covered by other applications.
How can I fix this?Here's my current code:
import sys from PyQt6.QtWidgets import QApplication, QMainWindow, QWidget, QLabel from PyQt6.QtGui import QPixmap, QMouseEvent from PyQt6.QtCore import Qt, QPoint class Toolkit(QWidget): def __init__(self, parent=None): super().__init__(parent) self.initUI() def initUI(self): self.setWindowFlags( Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint ) self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground) tool_image = "me.png" pixmap = QPixmap(tool_image) self.lbl = QLabel(self) self.lbl.setPixmap(pixmap) self.lbl.setStyleSheet("background: transparent;") self.lbl.setFixedSize(pixmap.width(), pixmap.height()) self.setFixedSize(pixmap.width(), pixmap.height()) screen_geometry = QApplication.primaryScreen().availableGeometry() self.move(screen_geometry.width() - pixmap.width(), screen_geometry.height() - pixmap.height()) self.show() self.dragging = False self.offset = QPoint() def mousePressEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.dragging = True self.offset = event.pos() def mouseMoveEvent(self, event: QMouseEvent): if self.dragging: self.move(event.globalPosition().toPoint() - self.offset) def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.MouseButton.LeftButton: self.dragging = False def main(): app = QApplication(sys.argv) tools = Toolkit() tools.show() sys.exit(app.exec()) if __name__ == '__main__': main()
-
Hi and welcome to devnet
Which OS are you using ?
-
I tested your code on 14.7.3 removing all things that would not work such as the label stuff and it behaves correctly with PyQt6 6.7.1 as well as 6.8.1.
Which version of PyQt6 are you using ?