Python QSQLTableModel中的值格式化问题
子类化QSQLTableModel并重写数据方法:Python QSQLTableModel中的值格式化问题,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,子类化QSQLTableModel并重写数据方法: class SclDataModel(QSqlTableModel): def __init__(self, parent=None): super(SclDataModel, self).__init__(parent) def data(self, index, role=None): if role == Qt.DisplayRole: if index.colum
class SclDataModel(QSqlTableModel):
def __init__(self, parent=None):
super(SclDataModel, self).__init__(parent)
def data(self, index, role=None):
if role == Qt.DisplayRole:
if index.column() == 2 or index.column() == 3:
val = QSqlTableModel.data(self, index, Qt.DisplayRole) #<--Is set to None on cell edit.
print('Value={}'.format(val))
return '${:,.2f}'.format(val)
else:
return super(SclDataModel,self).data(index,role)
elif role == Qt.TextAlignmentRole:
return Qt.AlignVCenter | Qt.AlignRight
else:
return QVariant()
通常,不建议修改模型,因为它表示数据,并且在您的情况下,符号$只是可视的,因此可视任务是委托的:
class MoneyDelegate(QStyledItemDelegate):
def initStyleOption(self, option, index):
super(MoneyDelegate, self).initStyleOption(option, index)
option.text = '${:,.2f}'.format(index.data())
option.displayAlignment = Qt.AlignVCenter | Qt.AlignRight
...
view = QTableView()
delegate = MoneyDelegate(view)
for i in (2, 3):
view.setItemDelegateForColumn(i, delegate)
class MoneyDelegate(QStyledItemDelegate):
def initStyleOption(self, option, index):
super(MoneyDelegate, self).initStyleOption(option, index)
option.text = '${:,.2f}'.format(index.data())
option.displayAlignment = Qt.AlignVCenter | Qt.AlignRight
def createEditor(self, parent, option, index):
editor = super(MoneyDelegate, self).createEditor(parent, option, index)
if any(isinstance(editor, t) for t in (QDoubleSpinBox, QSpinBox)):
editor.setMinimum(0)
editor.setMaximum(2**15)
return editor
另一方面,如果要修改数据,默认角色是Qt::DisplayRole
:
class SclDataModel(QSqlTableModel):
def data(self, index, role=Qt.DisplayRole):
...
更新:如果要修改编辑器,必须覆盖委托的createEditor()
方法:
class MoneyDelegate(QStyledItemDelegate):
def initStyleOption(self, option, index):
super(MoneyDelegate, self).initStyleOption(option, index)
option.text = '${:,.2f}'.format(index.data())
option.displayAlignment = Qt.AlignVCenter | Qt.AlignRight
...
view = QTableView()
delegate = MoneyDelegate(view)
for i in (2, 3):
view.setItemDelegateForColumn(i, delegate)
class MoneyDelegate(QStyledItemDelegate):
def initStyleOption(self, option, index):
super(MoneyDelegate, self).initStyleOption(option, index)
option.text = '${:,.2f}'.format(index.data())
option.displayAlignment = Qt.AlignVCenter | Qt.AlignRight
def createEditor(self, parent, option, index):
editor = super(MoneyDelegate, self).createEditor(parent, option, index)
if any(isinstance(editor, t) for t in (QDoubleSpinBox, QSpinBox)):
editor.setMinimum(0)
editor.setMaximum(2**15)
return editor
我正在尝试将值格式化(用于显示)为US Money您可以显示您所指内容的图片您希望“$”符号在编辑时显示吗?不,仅当它显示在表中时显示。好的,最后一个问题:数据库中要修改的列是哪种类型的字段?这非常有效。谢谢我是否会使用同一个代理来设置Spinbox以进行编辑???@Elcid_91代理有两个部分:用于无编辑模式的内容,即我们现在使用的内容,以及用于编辑器的版本的内容,具体取决于您想要的内容,您可以指示适当的方法。您想修改QDoubleSpinbox的什么?我想设置最小值和最大值。再次感谢您接受我的所有问题。仍在学习Qt5模型,您拥有极其宝贵的资源。