wesanyer
wesanyer

Reputation: 1002

How to perform action when QCalendarWidget popup closes?

I am using a QDateEdit widget with QDateEdit.setCalendarPopup(True). I am trying to connect a slot to the event when the calendar popup closes. See my example below for my attempts so far, found in MyCalendarWidget. None of my attempts so far have worked. What can I do to perform an action every time the calendar widget popup closes, not only when the date is changed?

from PyQt4 import QtGui, QtCore
import sys

class MainWindow(QtGui.QMainWindow):
    def __init__(self, *args):
        super(MainWindow,self).__init__(*args)
        self._date = QtGui.QDateEdit()
        self._date.setCalendarPopup(True)
        self._date.setCalendarWidget(MyCalendarWidget())
        self.setCentralWidget(self._date)

class App(QtGui.QApplication):
    def __init__(self, *args):
        super(App,self).__init__(*args)
        self.main = MainWindow()
        self.connect(self, QtCore.SIGNAL("lastWindowClosed()"), self.byebye )
        self.main.show()

    def byebye( self ):
        self.exit(0)

class MyCalendarWidget(QtGui.QCalendarWidget):
    def __init__(self, parent=None):
        print("mycal initialized")
        super(MyCalendarWidget, self).__init__(parent)

        self.installEventFilter(self)
        self._many = 2
        self._many2 = 2

    def focusInEvent(self, event):
        print('-'*self._many + 'focus in')
        if self._many == 2:
            self._many = 4
        else:
            self._many = 2
        super(MyCalendarWidget, self).focusInEvent(event)

    def focusOutEvent(self, event):
        print('-'*self._many2+'focus out')
        if self._many2 == 2:
            self._many2 = 4
        else:
            self._many2 = 2
        super(MyCalendarWidget, self).focusOutEvent(event)

    def closeEvent(self, event):
        print('close')
        super(MyCalendarWidget, self).closeEvent(event)

    def mouseReleaseEvent(self, event):
        print('mouse')
        super(MyCalendarWidget, self).mouseReleaseEvent(event)

def main(args):
    global app
    app = App(args)
    app.exec_()

if __name__ == "__main__":
    main(sys.argv)

Upvotes: 0

Views: 1080

Answers (1)

wesanyer
wesanyer

Reputation: 1002

Figured it out - turns out I need to use the clicked signal in QCalendarWidget. This removes the need to sub-class QCalendarWidget as well.

from PyQt4 import QtGui, QtCore
import sys

class MainWindow(QtGui.QMainWindow):
    def __init__(self, *args):
        super(MainWindow,self).__init__(*args)
        self._date = QtGui.QDateEdit()
        self._date.setCalendarPopup(True)
        calendar = self._date.calendarWidget()
        calendar.clicked.connect(self._clicked)
        self.setCentralWidget(self._date)

    def _clicked(self, date):
        print('clicked')

class App(QtGui.QApplication):
    def __init__(self, *args):
        super(App,self).__init__(*args)
        self.main = MainWindow()
        self.connect(self, QtCore.SIGNAL("lastWindowClosed()"), self.byebye )
        self.main.show()

    def byebye( self ):
        self.exit(0)

def main(args):
    global app
    app = App(args)
    app.exec_()

if __name__ == "__main__":
    main(sys.argv)

Upvotes: 1

Related Questions