Search in QStandardItemModel
-
Hello everybody,
I have read in and displayed a csv file in a QStandardItemModel (QTableView) , this works fine.
Now I would like to search in this model (1 column, <200 lines) for a word, this can occur theoretically in every line.
With this code works the reading in and attach the line, now unfortunately I do not know how I can now show the found words .. For the beginning, I would like to show them in the console, preferably with the line number.Does anyone have any idea how to do this?
QStandardItemModel *csvModel; QList<QStandardItem *> list; for (QString item : line.split(";")) { list.append(new QStandardItem(item)); } csvModel->insertRow(csvModel->rowCount(), list); csvModel->setHorizontalHeaderLabels(QStringList() << "Ausgangssprache"); QList<QStandardItem *> items = csvModel->findItems("GESUCHTER STRING", Qt::MatchExactly); if (items.size() > 0) { qDebug () << "Mind. 1 gefunden"; }
Thank you in advance!
-
Hi and welcome to devnet,
What kind of visualisation do you have in mind for the items found ? Another view ? A change of colour in this one ?
-
@FloQt
So in the list ofQStandardItem *
s returned byfindItems()
: I presumerow()
is the line number, anddata(Qt::DisplayRole)
is the string of the line. As for "show them in the console", presumablycout
will do that to the terminal where you run the program from, so something like (I'm not a C++er):cout << item->row() << item->data(Qt::DisplayRole);