简体   繁体   中英

How to add a custom event to a PyQt5 Event loop

I am trying to add an event that fires when the window is inactive (- where the user's mouse has clicked on another app or the desktop). I after reading a lot of the official documentation, I am still lost. I also hope to do this with other actions, but this would be the first step.

You have to use the activeChanged signal that is emitted every time the QWindow changes state and isActive() that indicates if it is active or not:

import sys

from PyQt5 import QtWidgets


app = QtWidgets.QApplication(sys.argv)

w = QtWidgets.QWidget()
w.show()

qwindow = w.windowHandle()

if qwindow is not None:

    def handle_activeChanged():
        print("isActive? {}".format(qwindow.isActive()))

    qwindow.activeChanged.connect(handle_activeChanged)

sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM