PDA

View Full Version : How to use standard model?



freiza
19th February 2015, 08:25
How to add data to standard model using data and not item.
And please explain me who is the parent of the model and how to set them.

I tried reading it from various source including Qt documentation but my concepts are not getting clear.
So please do not tell me to google. I have already come here from googling.

Thank You

wysota
19th February 2015, 09:12
How to add data to standard model using data and not item.
Huh? You mean how to set the data using setData()? Hmm... use setData() :)


And please explain me who is the parent of the model
The object you set as the parent in the constructor or by calling setParent.


and how to set them.
QObject::setParent()

freiza
19th February 2015, 09:21
suppose I want my model to have 2 rows and 3 column then how i should I set my Qstringlist?

wysota
19th February 2015, 09:29
What QStringList? I'm afraid you have to be more specific. QStringListModel is by definition one column only, of course you can override but then it's better to just use a model more tailored to your problem. However last time it seemed to me you were talking about QStandardItemModel.

freiza
19th February 2015, 09:47
void Dialog::createModel()
{
QStringList qstr;
// qstr << "hello"<<"world"<<"this"<<"is"<<"cool"<<"qt";
qstr.append("hello");
qstr.append("world");
qstr.append("this");
qstr.append("is");
qstr.append("cool");
qstr.append("qt");
booksModel = new QStandardItemModel();
booksModel->insertRows(0,2);
booksModel->insertColumns(0,3);
int k=0;
for(int i=0;i<booksModel->rowCount();i++)
{
for(int j=0;j<booksModel->columnCount();j++)
{
// QStandardItem *item = new QStandardItem(QString::number(j));
QModelIndex index = booksModel->index(i,j,QModelIndex());
//booksModel->setData(index,QVariant((i+1)*(j+1)));
booksModel->setData(index,QVariant(qstr[k]));
k++;
}
}

}
Question 1) Is there any way that I can pass "qstr" instead of "qstr[k]" and still it prints out everything correctly in tableview? I mean a structure that can be passed directly to set data such that I don't have to use for loop?
Question 2) Instead of append can I populate qstr using "qstr << "hello"<<"world"<<"this"<<"is"<<"cool"<<"qt";"
Question 3) How to use foreach loop instead of for loop in above case?

wysota
19th February 2015, 10:02
QStandardItemModel is meant to be used in such a way that you access elements as items so trying to work around it means you probably shouldn't be using that class. Regarding your questions:
1. Not using setData and pure standard item model. You can subclass the model and introduce a method which will accept a list and distribute its content among the items.
2. Yes
3. You would have to track current values of i and j yourself which would make the loop quite complicated.

Now towards a solution - if what you want is a model which will distribute its data in a tabular way among a specified number of columns then it's best to subclass QAbstractTableModel and implement a custom model which will do just that. Internally you can store the data in a stringlist and append to that list when new data arrives. Knowing the number of columns you can use values of row and column from the model index to calculate which item in the list the index corresponds to. The whole class should be less than 20 lines of code.

jefftee
19th February 2015, 10:22
Question 1) Is there any way that I can pass "qstr" instead of "qstr[k]" and still it prints out everything correctly in tableview? I mean a structure that can be passed directly to set data such that I don't have to use for loop?
Question 2) Instead of append can I populate qstr using "qstr << "hello"<<"world"<<"this"<<"is"<<"cool"<<"qt";"
Question 3) How to use foreach loop instead of for loop in above case?

1) No, not if you use QStandardItemModel. If you subclass QAbstractItemModel, you can implement the model using whatever container type you desire.

2) Yes, of course.

3) See below:



int i = 0;

foreach (str, qstr)
{
int row = i / model.columnCount();
int col = i % model.columnCount();
QModelIndex index = model.index(row, col);
model.setData(index, str);
i++;
}