<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Il giorno sab 2 mar 2019 alle ore 21:02 Sibylle Koczian <<a href="mailto:nulla.epistola@web.de">nulla.epistola@web.de</a>> ha scritto:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">How can I get a QDateEdit to show an empty date, or how can I clear the<br>
date it shows? </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">[...]</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Of course I can replace the QDateEdit with a QLineEdit and work with a<br>
string representation of the date column throughout. Is that the only<br>
solution?<br></blockquote><div><br></div><div>QDateTimeEdit and its inherited QDateEdit/QTimeEdit all inherit from QAbstractSpinBox, which contains a "private" QLineEdit.<br></div><div>While that is not publicly accessible, you can just use findChild() to get its reference.</div><div>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:</div><div><br></div><div><div><font face="monospace, monospace">class ClearableDateEdit(QtWidgets.QDateEdit):</font></div><div><font face="monospace, monospace">    def __init__(self, *args, **kwargs):</font></div><div><font face="monospace, monospace">        QtWidgets.QDateEdit.__init__(self, *args, **kwargs)</font></div><div><font face="monospace, monospace">        self.lineEdit = self.findChild(QtWidgets.QLineEdit)</font></div><div><font face="monospace, monospace">        self.clear()</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    def clear(self):</font></div><div><font face="monospace, monospace">        self.lineEdit.setText('')</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">class Widget(QtWidgets.QWidget):</font></div><div><font face="monospace, monospace">    def __init__(self):</font></div><div><font face="monospace, monospace">        QtWidgets.QWidget.__init__(self)</font></div><div><font face="monospace, monospace">        layout = QtWidgets.QGridLayout()</font></div><div><font face="monospace, monospace">        self.setLayout(layout)</font></div><div><font face="monospace, monospace">        dateEdit = ClearableDateEdit()</font></div><div><font face="monospace, monospace">        layout.addWidget(dateEdit)</font></div><div><font face="monospace, monospace">        clearButton = QtWidgets.QPushButton('Clear date')</font></div><div><font face="monospace, monospace">        layout.addWidget(clearButton)</font></div><div><font face="monospace, monospace">        clearButton.clicked.connect(dateEdit.clear)</font></div></div><div><br></div><div>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.</div><div><br></div><div>Maurizio</div><div><br></div><div>-- <br></div></div><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div></div></div></div>