QCalendarWidgetの年オプションをクリックするとマウスクリックイベントを発生させる方法。
onclick of year(2012)、pyqt5 を使用してテキストを印刷したい 前もって感謝します/
QCalendarWidgetの年オプションをクリックするとマウスクリックイベントを発生させる方法。
onclick of year(2012)、pyqt5 を使用してテキストを印刷したい 前もって感謝します/
最初に、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_())