QCalendarWidget 弹出窗口打开时发出信号?
Signal when QCalendarWidget popup opens?
这是一个函数,它正在绘制一些先前计算并保存在 "dates" 列表中的日期的单元格,函数工作正常但我想在单击 QDateEdit 时调用该函数(当弹出日历是显示)
def init_gui(self):
# Set signals of widgets
self.dockwidget.date_to.calendarWidget().clicked.connect(self.paint_cell) # !! the signal I'm looking for
def paint_cell(self):
#QDateEdit / QCalendarWidget Highlight Dates
keyword_format = QTextCharFormat()
keyword_format.setBackground(Qt.gray)
for date in dates:
self.dockwidget.date_from.calendarWidget().setDateTextFormat(QDate.fromString(date,"yyyy-MM-dd") ,keyword_format)
self.dockwidget.date_from() # QDateEdit
self.dockwidget.date_from.calendarWidget() # QCalendarWidget
我知道有信号,但点击 QDate 时它们都在工作:
self.dockwidget.date_to.calendarWidget().activated.connect(self.paint_cell)
self.dockwidget.date_to.calendarWidget().clicked.connect(self.paint_cell) self.dockwidget.date_to.calendarWidget().selectionChanged.connect(self.paint_cell)
但是当显示弹出窗口时,我必须在这些信号之前绘制单元格。
有人知道那个信号是什么吗?
注意:代码将成为 QGis 插件的一部分
如果要在显示calendarWidget之前设置,则不必使用任何信号,只需在构造函数中调用paint_cell即可。
# constructor
self.paint_cell()
self.dockwidget.date_to.calendarWidget().activated.connect(self.paint_cell)
# ...
更新:
这种情况下没有默认信号,相信它,您可以使用事件过滤器来监视 Event.Show 事件,然后发出信号。
from PyQt5 import QtCore, QtWidgets
class DateEdit(QtWidgets.QDateEdit):
popupSignal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(DateEdit, self).__init__(parent)
self.setCalendarPopup(True)
self.calendarWidget().installEventFilter(self)
def eventFilter(self, obj, event):
if self.calendarWidget() is obj and event.type() == QtCore.QEvent.Show:
self.popupSignal.emit()
return super(DateEdit, self).eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = DateEdit()
w.popupSignal.connect(lambda: print("popup"))
w.show()
sys.exit(app.exec_())
根据@eyllanesc post 的提示,我现在的工作代码是:
class Project(QDockWidget):
""" QGIS Plugin Implementation. """
popupSignal = QtCore.pyqtSignal()
def __init__(self, iface):
""" Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgisInterface
"""
QDockWidget.__init__(self)
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
...
def eventFilter(self, obj, event):
if self.dockwidget.date_to.calendarWidget() is obj and event.type() == QtCore.QEvent.Show:
self.popupSignal.emit()
return super(Project, self).eventFilter(obj, event)
....
def run(self):
""" Run method that loads and starts the plugin """
self.dockwidget.date_to.calendarWidget().installEventFilter(self)
self.popupSignal.connect(self.paint_cell)
def paint_cell(self):
#QDateEdit / QCalendarWidget Highlight Dates
keyword_format = QTextCharFormat()
keyword_format.setBackground(Qt.gray)
for date in dates:
self.dockwidget.date_from.calendarWidget().setDateTextFormat(QDate.fromString(date, "yyyy-MM-dd"), keyword_format)
这是一个函数,它正在绘制一些先前计算并保存在 "dates" 列表中的日期的单元格,函数工作正常但我想在单击 QDateEdit 时调用该函数(当弹出日历是显示)
def init_gui(self):
# Set signals of widgets
self.dockwidget.date_to.calendarWidget().clicked.connect(self.paint_cell) # !! the signal I'm looking for
def paint_cell(self):
#QDateEdit / QCalendarWidget Highlight Dates
keyword_format = QTextCharFormat()
keyword_format.setBackground(Qt.gray)
for date in dates:
self.dockwidget.date_from.calendarWidget().setDateTextFormat(QDate.fromString(date,"yyyy-MM-dd") ,keyword_format)
self.dockwidget.date_from() # QDateEdit
self.dockwidget.date_from.calendarWidget() # QCalendarWidget
我知道有信号,但点击 QDate 时它们都在工作: self.dockwidget.date_to.calendarWidget().activated.connect(self.paint_cell) self.dockwidget.date_to.calendarWidget().clicked.connect(self.paint_cell) self.dockwidget.date_to.calendarWidget().selectionChanged.connect(self.paint_cell)
但是当显示弹出窗口时,我必须在这些信号之前绘制单元格。
有人知道那个信号是什么吗?
注意:代码将成为 QGis 插件的一部分
如果要在显示calendarWidget之前设置,则不必使用任何信号,只需在构造函数中调用paint_cell即可。
# constructor
self.paint_cell()
self.dockwidget.date_to.calendarWidget().activated.connect(self.paint_cell)
# ...
更新:
这种情况下没有默认信号,相信它,您可以使用事件过滤器来监视 Event.Show 事件,然后发出信号。
from PyQt5 import QtCore, QtWidgets
class DateEdit(QtWidgets.QDateEdit):
popupSignal = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(DateEdit, self).__init__(parent)
self.setCalendarPopup(True)
self.calendarWidget().installEventFilter(self)
def eventFilter(self, obj, event):
if self.calendarWidget() is obj and event.type() == QtCore.QEvent.Show:
self.popupSignal.emit()
return super(DateEdit, self).eventFilter(obj, event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = DateEdit()
w.popupSignal.connect(lambda: print("popup"))
w.show()
sys.exit(app.exec_())
根据@eyllanesc post 的提示,我现在的工作代码是:
class Project(QDockWidget):
""" QGIS Plugin Implementation. """
popupSignal = QtCore.pyqtSignal()
def __init__(self, iface):
""" Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgisInterface
"""
QDockWidget.__init__(self)
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
...
def eventFilter(self, obj, event):
if self.dockwidget.date_to.calendarWidget() is obj and event.type() == QtCore.QEvent.Show:
self.popupSignal.emit()
return super(Project, self).eventFilter(obj, event)
....
def run(self):
""" Run method that loads and starts the plugin """
self.dockwidget.date_to.calendarWidget().installEventFilter(self)
self.popupSignal.connect(self.paint_cell)
def paint_cell(self):
#QDateEdit / QCalendarWidget Highlight Dates
keyword_format = QTextCharFormat()
keyword_format.setBackground(Qt.gray)
for date in dates:
self.dockwidget.date_from.calendarWidget().setDateTextFormat(QDate.fromString(date, "yyyy-MM-dd"), keyword_format)