简体   繁体   English

如何将自定义事件添加到 PyQt5 事件循环

[英]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).我正在尝试添加一个事件,该事件在 window 处于非活动状态时触发(- 用户的鼠标已单击另一个应用程序或桌面)。 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:您必须使用每次 QWindow 更改时发出的activeChanged信号 state 和isActive()指示它是否处于活动状态:

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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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