0

I have am trying to let my user pick a date and enter into the datebase using the dateedit widget and im doing this by using a delegate but for some reason it attaches the time aswell

class ProductDelegate(QtSql.QSqlRelationalDelegate):
    def __init__(self):
        super().__init__()      

    def createEditor(self, parent, option, index):

        if index.column() == 3:
            editor = QtGui.QDateEdit(parent)
            now = QtCore.QDate.currentDate()
            editor.setMinimumDate(now)
            editor.setCalendarPopup(True)
            return editor
        else:
            return QtSql.QSqlRelationalDelegate.createEditor(self, parent, option, index)

the string that's left once the date is picked is something like '30/01/2015 00:00:00' I do not want the time in there? what is the work around this?

enter image description here

Inthu
  • 1,009
  • 1
  • 8
  • 16
  • Why does it matter if the time's included? You can specify the format if and when it's shown back to the user to whatever you want. – jonrsharpe Jan 17 '15 at 14:00
  • I have edited the question, if you look at the once without time were entered using a database tool, but the ones that have the time attached is how its displayed to the user, how do I specify the format? – Inthu Jan 17 '15 at 14:08

1 Answers1

2

It looks like you may not be formatting the values correctly when setting the editor and/or model data. The delegate should probably look something like this:

class ProductDelegate(QtSql.QSqlRelationalDelegate):
    def createEditor(self, parent, option, index):
        if index.column() == 3:
            editor = QtGui.QDateEdit(parent)
            now = QtCore.QDate.currentDate()
            editor.setDisplayFormat('yyyy-MM-dd')
            editor.setMinimumDate(now)
            editor.setCalendarPopup(True)
            return editor
        return super(ProductDelegate, self).createEditor(parent, option, index)

    def setEditorData(self, editor, index):
        if index.column() == 3:
            data = index.data()
            if not isinstance(data, QtCore.QPyNullVariant):
                 editor.setDate(QtCore.QDate.fromString(data))
        else:
            super(ProductDelegate, self).setEditorData(editor, index)

    def setModelData(self, editor, model, index):
        if index.column() == 3:
            value = editor.date().toString('yyyy-MM-dd')
            model.setData(index, value, QtCore.Qt.EditRole)
        else:
            super(ProductDelegate, self).setModelData(editor, model, index)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • @Inthuson. They do exactly what their names suggest. If you reimplement `createEditor` you must reimplement them as well, otherwise the delegate cannot work properly. My example code might be a bit simplistic, though, because it doesn't attempt to account for the fact that you are using a `QSqlRelationalDelegate`. However, it might be good enough to at least solve the issue with the date formatting - did you try it? – ekhumoro Jan 18 '15 at 00:32
  • Nope, getting an error /: `.py", line 41, in setEditorData editor.setDate(QtCore.QDate.fromString(index.data())) TypeError: arguments did not match any overloaded call: QDate.fromString(str, Qt.DateFormat format=Qt.TextDate): argument 1 has unexpected type 'QPyNullVariant' QDate.fromString(str, str): argument 1 has unexpected type 'QPyNullVariant''` – Inthu Jan 18 '15 at 18:10
  • @Inthuson. Yeah - I already explained about that in your [other question on this subject](http://stackoverflow.com/q/27929342/984421). You must be using Python 2, so you have to deal with `QVariant`. Also, I see that you were already using `setEditorData` and `setModelData` in that question, so why the confusion? Anyway, I've updated my answer. – ekhumoro Jan 18 '15 at 21:15
  • No! That's the thing I'm still using 3.4 /: which is why I was confused when it didn't work – Inthu Jan 18 '15 at 21:19
  • @Inthuson. It's because [SQL models can return QPyNullVariant to represent null](http://pyqt.sourceforge.net/Docs/PyQt4/incompatibilities.html#pyqt4-v4-8-3). So it looks like an `isinstance` check is needed (I said my example may be too simplistic). I've updated again with a possible workaround. – ekhumoro Jan 18 '15 at 21:33