Reputation: 1115
I have a QTableview with model. I populate dummy data in the model using this code
horizontalHeader.append("Name");
horizontalHeader.append("Type");
horizontalHeader.append("Unit price");
horizontalHeader.append("qty");
item00 = new QStandardItem(QString("0"));
item01 = new QStandardItem(QString("0"));
item02 = new QStandardItem(QString("0"));
item03 = new QStandardItem(QString("0"));
QList<QStandardItem*> list;
list.push_back(item00);
list.push_back(item01);
list.push_back(item02);
list.push_back(item03);
for (int i=0; i<100; i++){
list.at(0)->setText(QString::number(i));
list.at(1)->setText(QString::number(i));
list.at(2)->setText(QString::number(i));
list.at(3)->setText(QString::number(i));
model.insertRow(i,list);
}
the result should be a table of 100 rows and four columns and the content of the rows is a number but instead I am getting a the first row populated with data and the other rows are just empty as per the following screenshot
Upvotes: 1
Views: 129
Reputation: 8355
You are inserting the same QStandardItem
s a 100 times, this should result in the following warning for every insert operation, starting from the second:
QStandardItem::insertRows: Ignoring duplicate insertion of item 0xxxxxxxxx
So, in your code, all insert operations (starting from the second) will be ignored, and then the QStandardItem
(that was already inserted in the first operation) is set to the new value of i
(that explains why you get one row 99
s in it).
Instead, you should use new QStandardItem
s for each row insert, like this:
for (int i=0; i<100; i++){
QList<QStandardItem*> list;
list.append(new QStandardItem(QString::number(i)));
list.append(new QStandardItem(QString::number(i)));
list.append(new QStandardItem(QString::number(i)));
list.append(new QStandardItem(QString::number(i)));
model.insertRow(i,list);
}
Upvotes: 3
Reputation: 5207
You need to create the items inside the loop
for (int i = 0; i < 100; ++i) {
QList<QStandardItem*> list;
list << new QStandardItem(QString::number(i));
list << new QStandardItem(QString::number(i));
list << new QStandardItem(QString::number(i));
list << new QStandardItem(QString::number(i));
model.appendRow(list);
}
Or even with an inner loop to create the four items.
Upvotes: 1