繁体   English   中英

在 seekRoot.parent();= QModelIndex(); 中 QModelIndex() 来自哪里

[英]Where does QModelIndex() come from in seekRoot.parent() != QModelIndex();

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

我无法理解while(seekRoot.parent() != QModelIndex())中的 QModelIndex( )是什么。 看起来像是 QModelIndex 的构造函数,但是这里有什么用呢? 它返回一个新的空 model 索引? 还是MainWindow的function? 这似乎是不可能的。

它从何而来? 什么是返回值?

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()构造函数表示无效(即不存在) QModelIndex

创建一个新的空 model 索引。 该类型的model索引用于表示model中的position无效。

所以seekRoot.parent() != QModelIndex()检查seekRoot是否有父级(即它的父级不是无效的)。

它也可以(更清楚地)写成seekRoot.parent().isValid() (参见QModelIndex::isValid )。

默认构造函数QModelIndex()创建一个临时无效索引,与seekRoot.parent()调用的 output 进行比较。 换句话说,这个表达式检查父索引是否有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM