我试图让我的用户选择一个日期并使用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)
选择日期后留下的字符串就像'30 / 01/2015 00:00:00'我不想在那里呆的时间?围绕这个是什么工作?
答案 0 :(得分:2)
在设置编辑器和/或模型数据时,您可能无法正确格式化值。代表应该看起来像这样:
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)