Search code examples
qtmodel-view

How to display a manipulated model content in QAbstractItemView


I have a QSqlTableModel and a QTableView displaying the contents of the model.

I've managed to customize the editing of models subclassing QStyledItemDelegate and reimplementing the methods setEditorData, setModelData and updateEditorGeometry.

What do I need to do now is put in the view a text based on the original model. Let me give a toy example:

Suppose I have stored the value "10" in the model. What I want is to display a manipulated version of this value in the view, for example "10 kg", but without changing the actual content of the model.

(This was only a simplified example, the real problem is not just putting a suffix.)

How can I do this? I could not find the answer in the documentation and looking at the two examples I could find similar to my problem, but they deal with new representation, drawn as stars (for example). What I want is simpler: I want to be able to rewrite the string.

Sorry about the crap english.


Solution

  • I finally managed to find an answer.

    The truth is that the answer was in the documentation and I let it go unnoticed. Just reimplement the method displayText of QStyledItemDelegate.

    Here is the original implementation. It was good to see it to get an idea of how to do the modifications.

    To do more complex manipulations, it is also good to take a look at the method initStyleOption (implementation).