[PyKDE] viewing a QStandardItemModel

David Boddie david at boddie.org.uk
Thu Mar 1 01:07:08 GMT 2007

On Wednesday 21 February 2007 18:43:03 +0100, alexander krohn wrote:

> i have a column in a QStandardItemModel filled with a numerical value. now
> i want to show this in a QTableView in a different way, the value
> represents some kind of state, so i don't want to see a number, but a
> string like "ok" or "not ok".

You could write a custom table model to do this, or just subclass
QStandardItemModel and reimplement the data() method to return strings
(wrapped in QVariant objects) for items that contain these numerical values.

> although i think that is an aspect of the view, i thought that the roles
> can do this, so i set the numerical value for the Qt.EditRole and the
> string for Qt.DisplayRole.
> so much for the theory, it doesn't work. so attached code shows twice the
> string "one" and not 1 and "one" as i would expect.

It looks like QStandardItemModel doesn't store the data corresponding to
DisplayRole and EditRole separately. They appear to share the same space,
so you end up overwriting data. If you use UserRole, for example, you
should get the expected result.

If you use UserRole and want the user to be able to edit this value rather
than the string, and update the string automatically, you'll need to subclass
QItemDelegate and reimplement the setEditorData() and setModelData() methods
to do this work. Take a look at the Spin Box Delegate example, included with
PyQt, for some more ideas about how you could present the data to be edited.


More information about the PyQt mailing list