簡體   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