如何检测任务栏上的点击?

How to detect a click on the taskbar?

我正在使用 PyQt4。我可以最小化和最大化 window,但我无法通过单击任务栏图标来最小化它。

程序由py2exe编译,在任务栏显示为"python.exe"。如何捕捉点击事件?

我正在使用 QWebView。事件 QWebView.event(e) 没有帮助。

下一个代码提供 window 状态变化的事件:

...

class LauncherView(QWebView, object):
    def __init__(self, QWidget_parent=None):
        super(LauncherView, self).__init__(QWidget_parent)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setContextMenuPolicy(Qt.CustomContextMenu)
        self.resize(801, 601)

    ...

    def event(self, e):
        if e.type() == e.WindowStateChange and self.windowState() & QtCore.Qt.WindowMinimized:  # Event if I click the minimize button 
            self.showMinimized()
        elif e.type() == e.WindowStateChange and self.windowState() == QtCore.Qt.WindowNoState:  # Event if I restore the window by clicking on taskbar
            self.showMaximized()  # or showNormal
        elif ???????:  # What event I must catch if I want to minimize window by clicking on taskbar? Now it does not occur...
            self.showMinimized()
        return super(QWebView, self).event(e)

...


def Main(*args):
    app = QApplication(args)
    app.setWindowIcon(QIcon('icon.png'))
    view = LauncherView()

    view.setWindowTitle('*** Launcher')
    frame = view.page().mainFrame()
    JavaScript = JSCaller(view)
    events = PyEvents(view, JavaScript)
    Python = PyCaller(events)
    html = HTML_data()
    thisDirPath = 'file:///' + getCurrentPath() + '/Views/'
    view.setHtml(html, QtCore.QUrl(thisDirPath))
        frame.addToJavaScriptWindowObject('Python', Python)
        frame.evaluateJavaScript("Python.Print('Python context works normally');")
        view.show()
    app.exec_()

if __name__ = '__main__':
    Main(*sys.argv)

您无法使用任务栏图标最小化应用程序的原因是您覆盖了现有的 window 标志。

现在,通常您会这样做 self.setWindowFlags(self.windowFlags()|Qt.FramelessWindowHint) 但是我对此进行了测试,并且在不应该显示的情况下显示了框架。据推测,现有标志之一与 frameless 标志冲突。

所以,至少,您似乎需要这些标志:

self.setWindowFlags(Qt.Window|Qt.FramelessWindowHint|Qt.WindowMinMaxButtonsHint)

完成后,您不需要任何特殊代码即可通过单击任务栏图标来 minimise/maximise window。

您可能还需要其他标志来获得其他行为。您可以在此处查看完整列表:http://qt-project.org/doc/qt-4.8/qt.html#WindowType-enum