PDA

View Full Version : QAbstractItemModel, QStandardItemModel



homerun4711
16th December 2010, 01:03
Hello!

I am new to Qt and hope you can help me. I am trying to write a simple application for invoice and customer management.

The situation is:

Header-File:

...
QAbstractItemModel *model;
...

CPP-File:

...
model = new QStandardItemModel(modelSizeRow, modelSizeColumn, this);
...

Now I was trying to populate a table with


model->setData(model->index(row, counter, QModelIndex()), pieces.value(counter));

So far everything works fine. But after some reading I thought that populating the model with QStandardItem might be a better idea. (Is this right?)


QStandardItem *item = new QStandardItem(QString(pieces.value(counter)));
model->setItem(row, counter, item);

This does not work, I get the following message.


class QAbstractItemModel has no member named setItem

Can someone tell me why? I am confused by the following code:

...
QAbstractItemModel *model;
...
model = new QStandardItemModel(modelSizeRow, modelSizeColumn, this);
...

What happens if a type is set to a different type using the keyword new? Is this possible if the objects are related by inheritance?

Thanks in advance!
Kind regards,
HomeR

tbscope
16th December 2010, 05:49
model->setData(model->index(row, counter, QModelIndex()), pieces.value(counter));
While this might work if you created enough indexes when creating the model, technically, this is not the correct way to add items.
In fact, the items are already there, but this will not work when you need to add data to an item with an index that doesn't exist.


But after some reading I thought that populating the model with QStandardItem might be a better idea. (Is this right?)
In the case of a QStanderItemModel, then yes, use a QStandardItem.



QStandardItem *item = new QStandardItem(QString(pieces.value(counter)));
model->setItem(row, counter, item);
This does not work,
As explained above, if row and counter point to a place with no index, this will not work.
Let the model add the indexes for you and use appendItem or insertItem


I am confused by the following code:

...
QAbstractItemModel *model;
...
model = new QStandardItemModel(modelSizeRow, modelSizeColumn, this);
...

What happens if a type is set to a different type using the keyword new? Is this possible if the objects are related by inheritance?

Since a QStandarItemModel inherits a QAbstractItemModel, this is allowed, but it is not correct.
Why do you want to define the model as a QAbstractItemModel and not as a QStandardItemModel?

homerun4711
16th December 2010, 09:18
Thanks for your answer, this cleared some things for me.


Why do you want to define the model as a QAbstractItemModel and not as a QStandardItemModel?

Well, this was not my idea :) I grabbed some code from Qt's build-in examples, this one was from itemviews/chart/mainwindow.h and mainwindow.cpp.


Let the model add the indexes for you and use appendItem or insertItem.

This is done with QModelIndex(), right? I have to read about that first. Does a model assign an QModelIndex to its items automatically or do I have to create one?

Kind regards,
HomeR