В справке Qt есть пример в Model/View Tutorial - 3.2 Работа с выборками. Коды ресурсов находятся в Qt\Qt5.9.1\Examples\Qt-5.9.1\widgets\tutorials\modelview\7_selections.
Я не могу понять, что такое QModelIndex() в while(seekRoot.parent() != QModelIndex())
. Это похоже на конструктор QModelIndex, но что здесь используется? Он возвращает новый пустой индекс модели? Или это функция MainWindow? Это кажется невозможным.
Откуда это взялось? И каково возвращаемое значение?
void MainWindow::selectionChangedSlot(const QItemSelection & /*newSelection*/, const QItemSelection & /*oldSelection*/)
{
//get the text of the selected item
const QModelIndex index = treeView->selectionModel()->currentIndex();
QString selectedText = index.data(Qt::DisplayRole).toString();
//find out the hierarchy level of the selected item
int hierarchyLevel=1;
QModelIndex seekRoot = index;
while(seekRoot.parent() != QModelIndex())
{
seekRoot = seekRoot.parent();
hierarchyLevel++;
}
QString showString = QString("%1, Level %2").arg(selectedText)
.arg(hierarchyLevel);
setWindowTitle(showString);
}