Сигнал при открытии всплывающего окна QCalendarWidget?

Вот функция, которая рисует ячейки некоторых дат, которые были предварительно рассчитаны и сохранены в списке "дат", функция работает нормально, но я хочу вызывать эту функцию, когда нажимается 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(). Activ.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

2 ответа

Решение

С подсказками из поста @eyllanesc мой рабочий код теперь:

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)

Вам не нужно использовать какой-либо сигнал, если вы хотите установить его перед отображением 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_())
Другие вопросы по тегам