pyqt4 dateedit 委托不想显示时间

pyqt4 dateedit delegate don't want to display time

我试图让我的用户选择一个日期并使用 dateedit 小部件输入数据库,我通过使用委托来执行此操作但出于某种原因它也附加了时间

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)

选择日期后留下的字符串类似于“2015 年 1 月 30 日 00:00:00”我不想要其中的时间?解决这个问题的方法是什么?

看起来您在设置编辑器 and/or 模型数据时可能没有正确设置值的格式。代表应该看起来像这样:

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)