Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QModelIndex()在seekRoot.parent()中来自何方QModelIndex();_C++_Qt_Model View - Fatal编程技术网

C++ QModelIndex()在seekRoot.parent()中来自何方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

在Qt帮助中,模型/视图教程-3.2中有一个使用选择的示例。资源代码位于Qt\Qt5.9.1\Examples\Qt-5.9.1\widgets\tutorials\modelview\7\u selections中

我无法理解while(seekRoot.parent()!=QModelIndex())中的内容。 它看起来像QModelIndex的构造函数,但这里有什么用途?它返回一个新的空模型索引?或者它是主窗口的一个功能?这似乎不可能

它来自哪里?返回值是多少

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()