简体   繁体   English

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

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

In Qt help, there is an example in Model/View Tutorial - 3.2 Working with Selections.在 Qt 帮助中,模型/视图教程 - 3.2 使用选择中有一个示例。 The resource codes are in Qt\Qt5.9.1\Examples\Qt-5.9.1\widgets\tutorials\modelview\7_selections .资源代码位于Qt\Qt5.9.1\Examples\Qt-5.9.1\widgets\tutorials\modelview\7_selections中。

I cannot understand what is QModelIndex() in while(seekRoot.parent() != QModelIndex()) .我无法理解while(seekRoot.parent() != QModelIndex())中的 QModelIndex( )是什么。 It looks like a constructor of QModelIndex, but what's the usage here?看起来像是 QModelIndex 的构造函数,但是这里有什么用呢? It returns a new empty model index?它返回一个新的空 model 索引? Or it is a function of MainWindow?还是MainWindow的function? It seems impossible.这似乎是不可能的。

Where does it come from?它从何而来? And what is the return value?什么是返回值?

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);
}

The empty QModelIndex() constructor indicates an invalid (ie non existing) QModelIndex :QModelIndex()构造函数表示无效(即不存在) QModelIndex

Creates a new empty model index.创建一个新的空 model 索引。 This type of model index is used to indicate that the position in the model is invalid.该类型的model索引用于表示model中的position无效。

So seekRoot.parent() != QModelIndex() checks if seekRoot has a parent (ie its parent is not invalid).所以seekRoot.parent() != QModelIndex()检查seekRoot是否有父级(即它的父级不是无效的)。

It could also be written (more clearly) as seekRoot.parent().isValid() (see QModelIndex::isValid ).它也可以(更清楚地)写成seekRoot.parent().isValid() (参见QModelIndex::isValid )。

The default constructor QModelIndex() creates a temporary invalid index the output of seekRoot.parent() call compared with.默认构造函数QModelIndex()创建一个临时无效索引,与seekRoot.parent()调用的 output 进行比较。 In other words, this expression checks whether the parent index is valid or not.换句话说,这个表达式检查父索引是否有效。

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

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