Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to Keep PyQt6 Window Always on Top on macOS

How to Keep PyQt6 Window Always on Top on macOS

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 239 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leoan
    wrote on last edited by
    #1

    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()
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet

      Which OS are you using ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet

        Which OS are you using ?

        L Offline
        L Offline
        Leoan
        wrote on last edited by
        #3

        @SGaist
        ProductName: macOS
        ProductVersion: 15.3.1
        BuildVersion: 24D70
        Apple M3

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved