QStandardItemModel:appendRow. How to add and get the line back.
-
Do not add lines (or are not added?)
QStandardItemModel
♪Here's the code:
QStandardItem * pProduct = new QStandardItem("3"); pProduct->setData("product", strProductName); this->appendRow(pProduct);
QList<QStandardItem *> entries = this->findItems("product"); qDebug() << entries.count();
And therefore, the conclusion is always
0
♪
What am I doing wrong?
How can I add and get the line back?UPD: The problem was solved:
QStandardItem * pChildEntry = new QStandardItem(QString("name"));
pChildEntry->setData(QString("product"), strProductName);
model.appendRow(pChildEntry);
qDebug() << model.rowCount();QList<QStandardItem *> entries = model.findItems(QString("product")); entries.count();
The number of lines is normal, therefore the search is not working, not the addition of the lines.
-
Lines may be added, for example:
QStandardItemModel model(10, 10); QList<QStandardItem*> row; for (int i = 0; i < model.colCount(); i++) { QStandardItem* item = new QStandardItem(QString("column %1").arg(i)); row.append(item); } model.append(row);
If there's only one column in the model, it's a little easier:
QStandardItemModel* model = new QStandardItemModel(0, 0); for (int i = 0; i < 10; ++i) model->appendRow(new QStandardItem(QString("Row %1").arg(i)));
Method
QStandardItemModel::findItems
searches, comparing only data with a roleQt::DisplayRole
♪ Next codeQStandardItemModel* model = new QStandardItemModel();
QStandardItem* item0 = new QStandardItem("Item0");
model->appendRow(item0);
qDebug() << "Item0:" << model->findItems("Item0").count();QStandardItem* item1 = new QStandardItem();
item1->setData("Item1", Qt::DisplayRole);
model->appendRow(item1);
qDebug() << "Item1:" << model->findItems("Item1").count();QStandardItem* item2 = new QStandardItem();
item1->setData("Item2", Qt::UserRole);
model->appendRow(item2);
qDebug() << "Item2:" << model->findItems("Item2").count();
Ejects into the console
Item0: 1
Item1: 1
Item2: 0
So, if you've identified the data as a challenge to the method
QStandardItem::setData
with a second parameter, different fromQt::DisplayRole
♪QStandardItemModel::findItems
That's not gonna work.