PDA

View Full Version : QStandardItemModel Help



frenk_castle
16th January 2010, 14:59
I have three tree views in my application and every view have its model which basically looks like this:



|
|
|--------Printer1
| |
| |------------Tray1
| | |
| | |----------Paper type
| | |----------value
| | |----------Amount
| | |----------value
| |
| |------------Tray2
| |
| |----------Paper type
| |----------value
| |----------Amount
| |----------value
|
|--------Printer2
| |
| |------------Tray1
| | |
| | |----------Paper type
| | |----------value
| | |----------Amount
| | |----------value
| |
| |------------Tray2
| |
| |----------Paper type
| |----------value
| |----------Amount
| |----------value
|
|--------Printer3
|
|------------Tray1
| |
| |----------Paper type
| |----------value
| |----------Amount
| |----------value
|
|------------Tray2
|
|----------Paper type
|----------value
|----------Amount
|----------value



I have a function which creates this empty model. Every value field is empty. I need to be able to do three things:

One: When the user chooses the type of paper he wants for a specific tray on a specific printer and the amount of paper he need I need to set the corresponding value fields.

Two: I need to reset the model, clear all values.

Three: I need to retrieve all values, for storing, printing etc.

I thought about making new model every time and copying it to the model that is associated to the view but I am not sure since model inherits QObject plus it doesn't solve my need number three. I am sure there is an answer in the documentation but so far I failed to find it. If somebody could help me save some time and tell me where to look for a class or a function to use it would really help me.

All I need is a way to set and get the value of the specific item in the model.

Thanks in advance.

Coises
16th January 2010, 18:54
All I need is a way to set and get the value of the specific item in the model.

Since you imply by the title that you’re using a QStandardItemModel, there are two ways you can access the data: you can employ the basic model/view technique and use QStandardItemModel::setData and QStandardItemModel::data; or you can follow the item-based approach and useQStandardItemModel::item or QStandardItemModel::itemFromIndex to access a QStandardItem and then use that class’s methods to proceed.

You can mix these approaches; QStandardItemModel is just a QAbstractItemModel that uses instances of QStandardItem as its data store. Sometimes one way of accessing the data may be simpler and sometimes the other.