In my Qt application I'm using the QCalendarWidget
and I would like to get notified when the mouse enters a new cell of the calendar. I know that the QCalendarWidget
is using a QTableView
internally which inherits from QAbstractItemView and this has an entered
signal:
This signal is emitted when the mouse cursor enters the item specified by index. Mouse tracking needs to be enabled for this feature to work.
I tried to receive the signal with following code:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MyCalendar:
def __init__(self):
app = QApplication(sys.argv)
window = QMainWindow()
cal = QCalendarWidget(window)
window.resize(320, 240)
cal.resize(320, 240)
table = cal.findChild(QTableView)
table.setMouseTracking(True)
table.entered.connect(self.onCellEntered)
window.show()
sys.exit(app.exec_())
def onCellEntered(self, index):
print("CellEntered")
if __name__ == "__main__":
window = MyCalendar()
But my callback function is never called. Do you have any ideas why?
The
QCalendarWidget
class uses a custom table-view which bypasses the normal mouse-event handlers - so theenetered
signal never gets emitted. However, it is possible to work-around that by using an event-filter to emit a custom signal that does the same thing.Here is a demo script that implements that: