2016-08-02 68 views
2

我正在尋找一種方法來訪問在QML中的模型中找到的元素的數量。在QML中使用模型的大小

例如:

Item { 
    id: root 
    Row { 
     id: row 
     height: root.height 
     width: root.width 
     spacing: 5 
     Repeater { 
      id: rep 
      width: root.width 
      height: root.height 
      model: [5, 3, 3, 1, 12] 
      delegate: myDelegate 
      Text { 
       id: myDelegate 
       text: "Element " + index + " of " size + ":" + modelData 
     } 
    } 
} 

但我無法弄清楚如何檢索模型的大小。 在文檔中我可以找到一個名爲count的屬性,但沒有提示如何訪問它。

回答

5

這取決於您使用的模型。在你的情況下,模型是一個普通的舊JavaScript數組,所以你會使用model.length。與模型或視圖相關的每個其他Qt類型都有一個計數屬性:ListModel,Repeater,ListView等等。因此,您也可以使用rep.count

+0

謝謝! rep.count像一個魅力。我會立即嘗試另一個! – derM