No rows are added (or are there no added ones?) In QStandardItemModel .

Here is the actual code:

  QStandardItem * pProduct = new QStandardItem("3"); pProduct->setData("product", strProductName); this->appendRow(pProduct); QList<QStandardItem *> entries = this->findItems("product"); qDebug() << entries.count(); 

And accordingly, the output is always 0 . What am I doing wrong? How to add and get back the string?

UPD: it was possible to localize the problem:

  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 displays normally, hence the search does not work, not the addition of lines

  • void QStandardItem::setData(const QVariant & value, int role = Qt::UserRole + 1) . What does the second parameter mean in your code? - user194374
  • This is the header listing: enum PRODUCT_MODEL_ROLES {strProductName = Qt :: UserRole, strName, ... etc strAbout, - Mira
  • Setting the standard role of Qt :: DisplayRole did not save the situation. Can a line be added at all, but not found? How can I view the model after adding (without output to the user interface)? - Mira
  • You can see. Print to console or take a look at the debugger. - user194374
  • Model??? I have only addresses in the debugger, and what to output to the console - if there is no result finditems. - Mira

1 answer 1

Lines can be added, for example, like this:

 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 the model has only one column, then it can be a bit simpler:

 QStandardItemModel* model = new QStandardItemModel(0, 0); for (int i = 0; i < 10; ++i) model->appendRow(new QStandardItem(QString("Row %1").arg(i))); 

The QStandardItemModel::findItems method searches, comparing only data that has the Qt::DisplayRole role. Following code

 QStandardItemModel* 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(); 

displays in console

 Item0: 1 Item1: 1 Item2: 0 

Thus, if you set the data by calling the QStandardItem::setData method with a second parameter other than Qt::DisplayRole , QStandardItemModel::findItems will not find this data.

  • Strangely enough, I have a working class model inherited from QStandardItem, that is, this syntax is familiar to me and works in one place. But for some reason, an attempt to create a similar one failed. Maybe it's the case in the wrong roles (in the working class I have them standard) - Mira
  • Thanks for the answer, it helped! As if I looked through that the search goes on only one role :) - Mira