Cilenco
Cilenco

Reputation: 7117

Get cells of QCalendarWidget

At the moment I'm writing a calendar program with QT. My main window holds a QCalendarWidget and now I want to listen to double click events of the cells. My problem is that I do not know how I can get a cell (which ia a child of the QCalendarWidget) so I can add an event listener to it. With:

calendarWidget.findChildren(QtCore.QObject)

I can get all children of the Widget but I do not know how to identify a cell. Do you have any ideas how I can do this?

Upvotes: 1

Views: 1003

Answers (1)

ekhumoro
ekhumoro

Reputation: 120578

The calendar widget contains a QTableView, so you can get a reference to that and query its contents.

The demo below installs an event-filter on the table to get double-clicks, because the table's doubleClicked signal is disabled by the calendar (presumably to prevent editing of the cells).

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.calendar = QtGui.QCalendarWidget(self)
        self.table = self.calendar.findChild(QtGui.QTableView)
        self.table.viewport().installEventFilter(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.calendar)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.MouseButtonDblClick and
            source is self.table.viewport()):
            index = self.table.indexAt(event.pos())
            print('row: %s, column: %s, text: %s' % (
                  index.row(), index.column(), index.data()))
        return super(Window, self).eventFilter(source, event)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(750, 250, 300, 300)
    window.show()
    sys.exit(app.exec_())

Upvotes: 3

Related Questions