[PyQt] QDateEdit, QStandardItemModel and empty date fields

Sibylle Koczian nulla.epistola at web.de
Sat Mar 2 20:01:34 GMT 2019


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

Background: my application puts data from a MS Access database table
into a QStandardItemModel. One of the table columns contains dates and
those dates may be NULL. This is no problem for the QTableView that
shows all the data: these cells are empty.

For editing data several input fields are connected to the model using a
QDataWidgetMapper. The date column is connected to a QDateEdit.

If a record contains NULL in its date column, then the QDateEdit shows
the date of the last selected record with a valid date. I can change
this to another valid date, but it should be changed to None. The
QTableView with the correct empty cell is in the same window as the
input fields or I would save wrong dates without noticing.

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?

I wrote a minimal test application that takes a date string from a
QLineEdit, converts it into a QDate and puts this into a QDateEdit - and
vice versa. I see that the QDateEdit simply keeps its last value, if the
date it gets is invalid (empty or invalid for other reasons). No exception.

Using PyQt5 5.11.3, Python 3.7.2, Windows 10.

Thank you for advice,
Sibylle


More information about the PyQt mailing list