Artem Selivanov
Artem Selivanov

Reputation: 1957

How to detect a click on the taskbar?

I am using PyQt4. I can minimize and maximize a window, but I cannot minimize it by clicking on the taskbar icon.

The program is compiled by py2exe and shows as "python.exe" in the taskbar. How can I catch the click event?

I'm using QWebView. The event QWebView.event(e) does not help.

The next code provides the event for window state changes:

...

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)

Upvotes: 1

Views: 1643

Answers (1)

three_pineapples
three_pineapples

Reputation: 11849

The reason you are unable to minimise the app using the taskbar icon is because you have overwritten the existing window flags.

Now, usually you would do self.setWindowFlags(self.windowFlags()|Qt.FramelessWindowHint) however I tested this and the frame is shown when it shouldn't be. Presumably, one of the existing flags is conflicting with the frameless flag.

So, At the minimum, it appears you need to have these flags:

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

Once you have this, you shouldn't need any special code to minimise/maximise the window by clicking on the taskbar icon.

You may also need other flags to get other behaviour. You can see the full list here: http://qt-project.org/doc/qt-4.8/qt.html#WindowType-enum

Upvotes: 5

Related Questions