'Qt5 unable to ignore mouse events on Mac OS

I have written a small script that would display the content of a file on the desktop in a transparent window (which is also always on top window). But when I click on some other window, the focus is automatically taken up by the script's window

Below is the script:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import argparse as ap


class Window(QWidget):
    def __init__(self, content, width, height):
        super().__init__()
        self.content = content.strip()
        self.swidth = width
        self.sheight = height

        self.setWindowTitle("Content Display")
        self.setWindowOpacity(0.1)
        self.setWindowFlags(Qt.WindowTransparentForInput)
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        self.setAttribute(Qt.WA_TransparentForMouseEvents)

        self.setGeometry(0, 0, self.swidth, self.sheight)

        self.text_box = QPlainTextEdit(self)
        self.text_box.move(10, 10)
        self.tb_width = self.width()
        self.tb_height = self.height()

        self.text_box.resize(self.tb_width, self.tb_height)

        self.content = f"{self.content}   " * 1000
        self.text_box.setPlainText(self.content)

        self.show()


    def resizeEvent(self, event):
        self.tb_width = self.width()
        self.tb_height = self.height()

        self.text_box.resize(self.tb_width, self.tb_height)


    def keyPressEvent(self, event):
        if event.key() == Qt.Key_I:
            if event.modifiers() & Qt.ControlModifier:
                cur_opacity = self.windowOpacity()
                self.setWindowOpacity(cur_opacity + 0.05)

        if event.key() == Qt.Key_D:
            if event.modifiers() & Qt.ControlModifier:
                cur_opacity = self.windowOpacity()
                self.setWindowOpacity(cur_opacity - 0.05)


if __name__ == "__main__":
    parser = ap.ArgumentParser()
    parser.add_argument("-f",
                        "--file",
                        required=True,
                        type=str,
                        help="File with content")

    args = parser.parse_args()
    file = args.file
    content = ""

    try:
        fd = open(file, 'r')
        content = fd.read()
        fd.close()
    except:
        print("Unable to open the content file")
        sys.exit(1)

    app = QApplication(sys.argv)
    screen = app.primaryScreen()
    size = screen.size()
    rect = screen.availableGeometry()

    window = Window(content, rect.width(), rect.height())

    sys.exit(app.exec())

Is there a way in which the focus on the window can be ignored when clicking on another window



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source