【问题标题】:pyqt4 dateedit delegate don't want to display timepyqt4 dateedit 代表不想显示时间
【发布时间】:2015-01-17 13:52:45
【问题描述】:

我试图让我的用户选择一个日期并使用 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' 我不想要时间吗?解决这个问题的方法是什么?

【问题讨论】:

  • 包含时间有什么关系?您可以指定格式是否以及何时显示给用户任何您想要的。
  • 我已经编辑了这个问题,如果你看一次没有时间是使用数据库工具输入的,但附有时间的是它如何显示给用户,我该如何指定格式?

标签: python datepicker delegates pyqt4 qsqltablemodel


【解决方案1】:

您在设置编辑器和/或模型数据时似乎没有正确格式化值。委托可能看起来像这样:

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)

【讨论】:

  • @Inthuson。他们完全按照他们的名字所暗示的那样去做。如果您重新实现createEditor,您也必须重新实现它们,否则委托无法正常工作。不过,我的示例代码可能有点简单,因为它没有考虑到您使用的是QSqlRelationalDelegate 的事实。但是,至少解决日期格式的问题可能就足够了——你试过了吗?
  • 不,收到错误/:.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''
  • @Inthuson。是的 - 我已经在你的other question on this subject 中解释过了。您必须使用 Python 2,因此您必须处理 QVariant。另外,我看到你已经在那个问题中使用了setEditorDatasetModelData,那么为什么会混淆呢?无论如何,我已经更新了我的答案。
  • 不!这就是我仍在使用 3.4 / 的东西:这就是为什么我在它不起作用时感到困惑
  • @Inthuson。这是因为SQL models can return QPyNullVariant to represent null。所以看起来需要isinstance 检查(我说我的例子可能太简单了)。我再次更新了一个可能的解决方法。
猜你喜欢
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-26
  • 1970-01-01
  • 2010-10-07
相关资源
最近更新 更多