QCalendarWidget on year使用pyqt5单击

2024-03-28 21:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在QCalendarWidget的年中单击选项时触发鼠标单击事件。你知道吗

encircle image

年度最佳(2012年), 我想用pyqt5打印一些文本 有人能帮忙吗。提前感谢/


Tags: 文本选项事件鼠标pyqt5年度qcalendarwidget
1条回答
网友
1楼 · 发布于 2024-03-28 21:26:18

第一件事是使用findChildren获取显示年份的QSpinBox,然后它将检测鼠标事件,但正如this solution指出的,这是不可能的,因此解决方法是检测焦点事件:

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_())

相关问题 更多 >