I have a QStandardItemModel that I am showing in q QTreeView. It works great.
To highlight the corresponding lines, I want to highlight some of them: therefore, I have a QStringList where the names QStandItem * will be highlighted.
QStringList namesToBeHighlighted = getNames();
QModelIndex in = myModel->index(0, 0);
if ( in.isValid() ) {
for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex) {
QModelIndex si = myModel->index(curIndex, 0, in);
QStandardItem *curItem = myModel->itemFromIndex(si);
if (curItem) {
QString curItemName = curItem->text();
if ( namesToBeHighlighted.contains(curItem->text()) ) {
curItem->setFont(highlightFont);
}
else curItem->setFont(unHighlightFont);
}
}
}
My model has the following structure:
level_1
+ → Level_11
+ → Level_12
+ → Level_13
level_2
+ → Level_21
+ → Level_22
+ → Level_23
...
Here, it iterates through levels 11, 12, and 13, then stops.
source
share