[PyQt] QDateEdit, QStandardItemModel and empty date fields

Maurizio Berti maurizio.berti at gmail.com
Sun Mar 3 15:55:14 GMT 2019

Il giorno sab 2 mar 2019 alle ore 21:02 Sibylle Koczian <
nulla.epistola at web.de> ha scritto:

> How can I get a QDateEdit to show an empty date, or how can I clear the
> date it shows?


Of course I can replace the QDateEdit with a QLineEdit and work with a
> string representation of the date column throughout. Is that the only
> solution?

QDateTimeEdit and its inherited QDateEdit/QTimeEdit all inherit from
QAbstractSpinBox, which contains a "private" QLineEdit.
While that is not publicly accessible, you can just use findChild() to get
its reference.
Here's a small example you can implement in a model view by using a
delegate and checking for the data in the setEditorData() method:

class ClearableDateEdit(QtWidgets.QDateEdit):
    def __init__(self, *args, **kwargs):
        QtWidgets.QDateEdit.__init__(self, *args, **kwargs)
        self.lineEdit = self.findChild(QtWidgets.QLineEdit)

    def clear(self):

class Widget(QtWidgets.QWidget):
    def __init__(self):
        layout = QtWidgets.QGridLayout()
        dateEdit = ClearableDateEdit()
        clearButton = QtWidgets.QPushButton('Clear date')

There are some things you've to take into account, anyway, most importantly
whenever the focus is get or lost or the cell is changed, which will
require some control over the DateEdit widget *changed() signals and
methods like dateTimeFromText(), and finally check everything before using


È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20190303/5019cca1/attachment.html>

More information about the PyQt mailing list