3

如何在点击 QCalendarWidget 的年份选项时触发鼠标点击事件。

包围图像

onclick of year(2012),我想使用 pyqt5 打印一些文本有人可以帮忙吗?提前致谢/

4

1 回答 1

2

首先是使用 findChildren 获取显示年份的 QSpinBox,然后是检测鼠标事件,但正如该解决方案指出的那样,这是不可能的,因此解决方法是检测焦点事件:

from PyQt5 import QtCore, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.calendar_widget = QtWidgets.QCalendarWidget()
        self.setCentralWidget(self.calendar_widget)

        self.year_spinbox = self.calendar_widget.findChild(
            QtWidgets.QSpinBox, "qt_calendar_yearedit"
        )

        self.year_spinbox.installEventFilter(self)

    def eventFilter(self, obj, event):
        if obj is self.year_spinbox and event.type() == QtCore.QEvent.FocusIn:
            print(self.year_spinbox.value())

        return super().eventFilter(obj, event)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
于 2019-11-18T12:25:59.270 回答