简体   繁体   English

PyQt5 检测到警报事件

[英]PyQt5 detect an alert event

is there a way to detect when an alert happens on the main window?有没有办法检测主窗口上何时发生alert I know there is a showEvent and closeEvent , but is there an alertEvent equivalent?我知道有一个showEventcloseEvent ,但是有alertEvent等价的alertEvent

What I am hoping to do is trigger an alert for the builtin QApplication alerts be able to detect when that alert happens but also when that alert is cleared.我所希望做的是触发了alert的内置QApplication的警报可以当警报情况发生,而且当清除警报检测。 The alert will make the windows taskbar icon flash an orange color, and when the user brings the window back up the flashing is stopped and clears the alert.警报将使 Windows 任务栏图标闪烁橙色,当用户将窗口重新打开时,闪烁停止并清除警报。

To put it simply I want to detect when the flashing begins and when the flashing ends.简而言之,我想检测闪烁何时开始以及何时结束。

Below I will post a simple bit of code for what I am thinking.下面我将根据我的想法发布一些简单的代码。

Main.py主文件

import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
import time



class MyMainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()


        self.central_widget = QtWidgets.QWidget()
        self.setCentralWidget(self.central_widget)
        vbox = QtWidgets.QVBoxLayout()
        self.central_widget.setLayout(vbox)

        self.start_pushButton = QtWidgets.QPushButton()
        self.start_pushButton.setText("Alert")
        self.start_pushButton.clicked.connect(self.wait)
        vbox.addWidget(self.start_pushButton)


    def wait(self):
        main_window.showMinimized()
        time.sleep(2)
        print("Alert")
        app.alert(self,0)

    def event(self,evt):
        print(evt)
        return True

    def alertDetectedJob(self):
        print("Alert Detected")

    def alertClearedJob(self):
        print("Alert Cleared")


app = QtWidgets.QApplication(sys.argv)
main_window = MyMainWindow()
main_window.show()
sys.exit(app.exec_())

Tested for Windows!经过 Windows 测试!

Note!笔记! On Windows, this causes the window's taskbar entry to flash for a time.在 Windows 上,这会导致窗口的任务栏条目闪烁一段时间。 If msec is zero, the flashing will stop and the taskbar entry will turn a different color (currently orange).如果 msec 为零,则闪烁将停止并且任务栏条目将变为不同的颜色(当前为橙色)。

import sys
from PyQt5 import QtCore
from PyQt5 import QtWidgets
#import time


class MyMainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.central_widget = QtWidgets.QWidget()
        self.setCentralWidget(self.central_widget)
        vbox = QtWidgets.QVBoxLayout()
        self.central_widget.setLayout(vbox)

        self.start_pushButton = QtWidgets.QPushButton()
        self.start_pushButton.setText("Alert")
        self.start_pushButton.clicked.connect(self.wait)
        vbox.addWidget(self.start_pushButton)

# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        self.flagWindowDeactivate = False                   # +++
        
        self.timer = QtCore.QTimer(self)       
        self.timer.timeout.connect(self.update_func)     
        self.timer.start(5500)                              # + 5500
                                                            #   ^^
    def update_func(self):                        
        QtWidgets.QApplication.alert(self, 5000)            # + 5000
        if self.flagWindowDeactivate:                       #   ^
            self.alertDetectedJob()
        else: 
            self.alertClearedJob()

    def wait(self):
        self.showMinimized()
#        main_window.showMinimized()
#        time.sleep(2)
#        print("Alert")
#        app.alert(self, 0)
        QtWidgets.QApplication.alert(self, 5000)            # + 5000
                                                            #   ^
    def event(self, event):
#        print(event.type())
        if event.type() == QtCore.QEvent.WindowDeactivate:  # !!!
            print('WindowDeactivate')
            self.flagWindowDeactivate = True
        if event.type() == QtCore.QEvent.WindowActivate:    # !!!
            print('WindowActivate')
            self.flagWindowDeactivate = False
        return True
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    def alertDetectedJob(self):
        print("Alert Detected")

    def alertClearedJob(self):
        print("Alert Cleared")          


app = QtWidgets.QApplication(sys.argv)
main_window = MyMainWindow()
main_window.show()
sys.exit(app.exec_())

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

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