简体   繁体   English

QCalendarWidget 年度点击使用 pyqt5

[英]QCalendarWidget on year click using pyqt5

How to fire mouse click event on clicking in year option for QCalendarWidget.如何在点击 QCalendarWidget 的年份选项时触发鼠标点击事件。

包围图像

onclick of year(2012), i want to print some text using pyqt5 Can anyone help. onclick(2012 年),我想使用 pyqt5 打印一些文本有人可以帮忙吗? Thanks in advance/提前致谢/

The first thing is to obtain the QSpinBox that shows the year using findChildren, then it is to detect the mouse event but as this solution points out it is not possible so a workaround is to detect the focus event:首先是使用 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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM