[PyQt] QDateEdit, QStandardItemModel and empty date fields

Sibylle Koczian nulla.epistola at web.de
Tue Mar 5 15:30:31 GMT 2019

Many thanks to you both for your solutions. I'll need a little time to 
try them out - and this looks like the first time I might put an 
additional widget into the designer. That is documented, I think, but 
I'll have to look at it.

Am 05.03.2019 um 09:43 schrieb J Barchan:
> On Sun, 3 Mar 2019 at 15:57, Maurizio Berti <maurizio.berti at gmail.com 
> <mailto:maurizio.berti at gmail.com>> wrote:
>     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)
>              self.clear()
>          def clear(self):
>              self.lineEdit.setText('')
>     class Widget(QtWidgets.QWidget):
>          def __init__(self):
>              QtWidgets.QWidget.__init__(self)
>              layout = QtWidgets.QGridLayout()
>              self.setLayout(layout)
>              dateEdit = ClearableDateEdit()
>              layout.addWidget(dateEdit)
>              clearButton = QtWidgets.QPushButton('Clear date')
>              layout.addWidget(clearButton)
>              clearButton.clicked.connect(dateEdit.clear)
>     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 setModelData.
>     Maurizio
> You cannot have an empty QDateEdit, as per my topic long ago at 
> https://forum.qt.io/topic/86749/qdateedit-qabstractspinbox-blank-empty-value-problem.

Or rather, you cannot have an empty QDate, as this very interesting 
discussion shows. Happily I'm not plagued with pernickety users, but 
just writing something I intend to use myself. So possibly I'll just use 
a QLineEdit with an input mask.

> I had to deal with this.  I did not take @Maurizio's approach.  I do not 
> know what his solution does about QDateEdit.setData/data() for an empty 
> date, and other things.  Until now I didn't know you could get at its 
> QLineEdit.  For my part I chose to have a QLineEdit plus an associated 
> ... button to its right which leads to a modal dialog.  I paste extracts 
> below, feel free to cannibalise it if you want my approach. If @Maurizio 
> wishes to criticize mine, that's fine, I will read and consider!
I think in Maurizios approach much depends on the delegate that's used.


More information about the PyQt mailing list