How to change event behavior minimization in PyQt or PySide?

I am developing a Qt application and changing the closing behavior using a closeEventvirtual function as follows:

class MainWindow(QMainWindow):
    def closeEvent(self, event):
            event.ignore()
            self.hide()
            self.trayicon.showMessage('Running', 'Running in the background.')

This works as expected. If I delete event.ignore(), the application will stop working, everything will be fine.

I also want to control the minimize event, so when the user clicks the minimize button in the title bar, I want to move the window, not minimize it. I cannot use the hideEventvirtual function because the event will still be sent to the window, so this code:

def hideEvent(self, event):
    event.ignore()
    self.move(0,0)

moves the window to the upper left and then minimizes it. event.ignore()not valid here, so I tried using QtCore.QObject.eventas follows:

def event(self, event):
    if event.type() == QEvent.WindowStateChange:
        if self.isMinimized():
            event.ignore()
            self.move(0,0)
            return True
    return False

, . ? ?

+5
1

changeEvent WindowMinimized , - :

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.systemTrayIcon = QtGui.QSystemTrayIcon(self)
        self.systemTrayIcon.setIcon(QtGui.QIcon.fromTheme("face-smile"))
        self.systemTrayIcon.setVisible(True)
        self.systemTrayIcon.activated.connect(self.on_systemTrayIcon_activated)

        self.label = QtGui.QLabel(self)
        self.label.setText("Minimize me!")

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.label)

    @QtCore.pyqtSlot(QtGui.QSystemTrayIcon.ActivationReason)
    def on_systemTrayIcon_activated(self, reason):
        if reason == QtGui.QSystemTrayIcon.DoubleClick:
            if self.isHidden():
                self.show()

            else:
                self.hide()

    def changeEvent(self, event):
        if event.type() == QtCore.QEvent.WindowStateChange:
            if self.windowState() & QtCore.Qt.WindowMinimized:
                event.ignore()
                self.close()
                return

        super(MyWindow, self).changeEvent(event)

    def closeEvent(self, event):
        event.ignore()
        self.hide()
        self.systemTrayIcon.showMessage('Running', 'Running in the background.')

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.show()

    sys.exit(app.exec_())
+5

All Articles