2014-12-14 2 views
2

섹션이있는 listview를 구현할 수 없습니다. Qt 설명서의 예제를 성공적으로 반복했지만 ListModel이 사용되었는데 괜찮 았지만 var은 사용되지 않았습니다. 이 예제를 작동하는 방법코드에서 QML ListView 섹션

:

ListView { 
    width: 100 
    height: 100 
    id: listview 
    model: ListModel { 
     id: animalsModel 
     ListElement { name: "Ant"; size: "Tiny" } 
     ListElement { name: "Flea"; size: "Tiny" } 
     ListElement { name: "Parrot"; size: "Small" } 
     ListElement { name: "Guinea pig"; size: "Small" } 
     ListElement { name: "Rat"; size: "Small" } 
     ListElement { name: "Butterfly"; size: "Small" } 
     ListElement { name: "Dog"; size: "Medium" } 
     ListElement { name: "Cat"; size: "Medium" } 
     ListElement { name: "Pony"; size: "Medium" } 
     ListElement { name: "Koala"; size: "Medium" } 
     ListElement { name: "Horse"; size: "Large" } 
     ListElement { name: "Tiger"; size: "Large" } 
     ListElement { name: "Giraffe"; size: "Large" } 
     ListElement { name: "Elephant"; size: "Huge" } 
     ListElement { name: "Whale"; size: "Huge" } 
    } 

    delegate: Text { text: name; font.pixelSize: 18 } 

    section.property: "size" 
    section.criteria: ViewSection.FullString 
    section.delegate: Component { 
     id: sectionHeading 
     Rectangle { 
      width: container.width 
      height: childrenRect.height 
      color: "lightsteelblue" 

      Text { 
       text: section 
       font.bold: true 
       font.pixelSize: 20 
      } 
     } 
    } 
} 

GUI qt example

하지만이 코드에서 어떤 모델을 사용하려고하면 전혀 작동하지 않습니다 (내 경우는 PyQt5에서 QVariant입니다) :

ListView { 
    width: 100 
    height: 100 
    id: listview 
    property var m: [ 
     { 
      name: "Animal", 
      size: "Big" 
     }, 
     { 
      name: "Dog", 
      size: "Small" 
     }, 
     { 
      name: "Cat", 
      size: "Small" 
     } 
    ] 
    model: m 

    delegate: Text { text: modelData.name; font.pixelSize: 18 } 

    section.property: "modelData.size" 
    section.criteria: ViewSection.FullString 
    section.delegate: Component { 
     id: sectionHeading 
     Rectangle { 
      width: container.width 
      height: childrenRect.height 
      color: "lightsteelblue" 

      Text { 
       text: section 
       font.bold: true 
       font.pixelSize: 20 
      } 
     } 
    } 
} 

,의 파이썬에서 모델을받을 수있는 다른 방법이 없기 때문에 내가 여기 var를 선택하는 이유 o list 또는 map에서 python까지는 QVariant으로 싸여 야합니다. Qt는 문서에서

답변

2

:

모델 : 모델

This property holds the model providing data for the list. The model provides the set of data that is used to create the items in the view. Models can be created directly in QML using ListModel, XmlListModel or VisualItemModel, or provided by C++ model classes. If a C++ model class is used, it must be a subclass of QAbstractItemModel or a simple list.

그래서 당신이 모델로 배열을 제공 할 수 없습니다, 그것은 위의 개체 중 하나이어야한다. 그런 모델을 만들고 파이썬 코드에서 액세스하여 항목을 추가/제거 할 수 있습니다.

ListView { 
    width: 100 
    height: 100 
    id: listview 
    delegate: Text { text: name; font.pixelSize: 18 } 

    model: ListModel { id: listModel } 

    section.property: "size" 
    section.criteria: ViewSection.FullString 
    section.delegate: Component { 
     id: sectionHeading 
     Rectangle { 
      width: container.width 
      height: childrenRect.height 
      color: "lightsteelblue" 

      Text { 
       text: section 
       font.bold: true 
       font.pixelSize: 20 
      } 
     } 
    } 

    function callFromPython { 
     listModel.append({name: "Animal",size: "Big"}); 
     listModel.append({name: "Dog",size: "Small"}); 
     listModel.append({name: "Cat",size: "Small"}); 
    } 
} 
+0

동일한 문제 (일반 배열 사용)로 실행됩니다. 왜 섹션 만 작동하지 않는지 나는 이해할 수 없다. 문서에 따르면 그것은 C++의 간단한리스트를 지원하는데, 왜 자바 스크립트리스트와 다른가? – RvdK

관련 문제