[PyQt] Model/view: values of type Decimal not shown

Sibylle Koczian nulla.epistola at web.de
Thu Jul 26 19:24:13 BST 2012


Hello,

I don't know how to get numeric data from my model to my GUI, if they 
aren't floats but decimal.Decimal. In the following example the last 
column of the view and the last QLineEdit of the form both stay empty. 
The model data are printed to the console and it can be seen that the 
model contains the correct data for the last column and that they have 
the right type. But the GUI doesn't show them. What should I change?

Thanks for hints,
Sibylle


# modeltest.py

import sys
import decimal
import datetime
from PyQt4 import QtCore
from PyQt4 import QtGui

RAWDATA = [(1, "First", datetime.date(2012, 7, 15), 1.25, 
decimal.Decimal(2.5)),
            (2, "Second", datetime.date(2011, 6, 10), 3.77,
             decimal.Decimal(7.54)),
            (3, "Third", datetime.date(2010, 5, 27), 5.03,
             decimal.Decimal(10.06))]

class MyDialog(QtGui.QDialog):

     def __init__(self, data, parent=None):
         super(MyDialog, self).__init__(parent)
         self.myModel = self.setupModel(data)
         self.showModelData()
         view = self.setupView()
         form = self.setupForm()
         view.selectionModel().currentRowChanged.connect(
             self.mapper.setCurrentModelIndex)
         layout = QtGui.QVBoxLayout()
         layout.addWidget(view)
         layout.addWidget(form)
         self.setLayout(layout)
         #self.mapper.toFirst()
         view.selectRow(0)

     def setupModel(self, data):
         md = QtGui.QStandardItemModel(len(data), 5)
         md.setHorizontalHeaderLabels(["Int", "String", "Date", "Float",
                                       "Decimal"])
         for (i, row) in enumerate(data):
             # Integer
             item = QtGui.QStandardItem()
             item.setData(row[0], QtCore.Qt.EditRole)
             md.setItem(i, 0, item)
             # String
             md.setItem(i, 1, QtGui.QStandardItem(row[1]))
             item = QtGui.QStandardItem()
             # First try: use datetime.date as is. Result:
             # Date column in view empty, QDateEdit shows 1.1.2000
             # item.setData(row[2], QtCore.Qt.EditRole)
             # Second try: convert to QDate via string
             dt = QtCore.QDate.fromString(row[2].isoformat(),
                                          QtCore.Qt.ISODate)
             item.setData(dt, QtCore.Qt.EditRole)
             md.setItem(i, 2, item)
             # Float
             item = QtGui.QStandardItem()
             item.setData(row[3], QtCore.Qt.EditRole)
             md.setItem(i, 3, item)
             # Decimal
             item = QtGui.QStandardItem()
             item.setData(row[4], QtCore.Qt.EditRole)
             md.setItem(i, 4, item)
         return md

     def setupView(self):
         view = QtGui.QTableView()
         view.setModel(self.myModel)
         view.setAlternatingRowColors(True)
         view.setSelectionMode(QtGui.QTableView.SingleSelection)
         view.setSelectionBehavior(QtGui.QTableView.SelectRows)
         view.resizeColumnsToContents()
         return view

     def setupForm(self):
         # set up the widgets
         intBox = QtGui.QSpinBox()
         strEdit = QtGui.QLineEdit()
         dateEdit = QtGui.QDateEdit()
         floatEdit = QtGui.QLineEdit()
         decEdit = QtGui.QLineEdit()
         # set up the mapper
         self.mapper = QtGui.QDataWidgetMapper(self)
         self.mapper.setModel(self.myModel)
         self.mapper.addMapping(intBox, 0)
         self.mapper.addMapping(strEdit, 1)
         self.mapper.addMapping(dateEdit, 2)
         self.mapper.addMapping(floatEdit, 3)
         self.mapper.addMapping(decEdit, 4)
         # set up the layout
         form = QtGui.QWidget()
         layout = QtGui.QFormLayout()
         layout.addRow("Int:", intBox)
         layout.addRow("String:", strEdit)
         layout.addRow("Date:", dateEdit)
         layout.addRow("Float:", floatEdit)
         layout.addRow("Decimal:", decEdit)
         form.setLayout(layout)
         return form

     def showModelData(self):
         print("--- Model data: ---")
         for i in range(self.myModel.rowCount()):
             inr = self.myModel.data(self.myModel.index(i, 0))
             desc = self.myModel.data(self.myModel.index(i, 1))
             day = self.myModel.data(self.myModel.index(i, 2))
             fl = self.myModel.data(self.myModel.index(i, 3))
             dec = self.myModel.data(self.myModel.index(i, 4))
             print("{0}: {1} / {2}, {3}, {4} ({5})".format(inr, desc, 
day, fl,
                                                           dec, type(dec)))
         print("--- End of model data ---")

app = QtGui.QApplication(sys.argv)
dia = MyDialog(RAWDATA)
dia.show()
app.exec_()


More information about the PyQt mailing list