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

Art*_*nov 1 python taskbar pyqt click py2exe

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

该程序由py2exe编译并在任务栏中显示为“python.exe”。如何捕捉点击事件?

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

下一个代码提供窗口状态更改的事件:

...

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)
Run Code Online (Sandbox Code Playgroud)

thr*_*les 5

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

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

因此,至少您需要拥有这些标志:

self.setWindowFlags(Qt.Window|Qt.FramelessWindowHint|Qt.WindowMinMaxButtonsHint)
Run Code Online (Sandbox Code Playgroud)

一旦有了这个,您就不需要任何特殊代码来通过单击任务栏图标来最小化/最大化窗口。

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