C++ QModelIndex()在seekRoot.parent()中来自何方QModelIndex();
在Qt帮助中,模型/视图教程-3.2中有一个使用选择的示例。资源代码位于Qt\Qt5.9.1\Examples\Qt-5.9.1\widgets\tutorials\modelview\7\u selections中 我无法理解while(seekRoot.parent()!=QModelIndex())中的内容。 它看起来像QModelIndex的构造函数,但这里有什么用途?它返回一个新的空模型索引?或者它是主窗口的一个功能?这似乎不可能 它来自哪里?返回值是多少C++ QModelIndex()在seekRoot.parent()中来自何方QModelIndex();,c++,qt,model-view,C++,Qt,Model View,在Qt帮助中,模型/视图教程-3.2中有一个使用选择的示例。资源代码位于Qt\Qt5.9.1\Examples\Qt-5.9.1\widgets\tutorials\modelview\7\u selections中 我无法理解while(seekRoot.parent()!=QModelIndex())中的内容。 它看起来像QModelIndex的构造函数,但这里有什么用途?它返回一个新的空模型索引?或者它是主窗口的一个功能?这似乎不可能 它来自哪里?返回值是多少 void MainWindo
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);
}
默认构造函数
QModelIndex()
创建一个临时无效索引,与seekRoot.parent()调用的输出进行比较。换句话说,此表达式检查父索引是否有效。空构造函数表示无效(即不存在)QModelIndex
:
创建新的空模型索引。这种类型的模型索引用于
指示模型中的位置无效
所以seekRoot.parent()!=QModelIndex()
检查seekRoot
是否有父项(即其父项不是无效的)
也可以(更清楚地)写为seekRoot.parent().isValid()
(请参阅)。感谢您指出isValid
。它的可读性比!=QModelIndex()
。