DelegateChooser does not work



  • What is wrong in QML code below?

    import QtQuick.Controls 2.13
    import Qt.labs.qmlmodels 1.0
    
    //...
    
    TableView {
              anchors.fill: parent
              columnSpacing: 1
              rowSpacing: 1
              clip: true
    
              model: TableModel {}
    
              delegate: DelegateChooser {
                  role: "heading"
                  DelegateChoice {
                      roleValue: true
                      delegate: Text {
                          text: tabledata
                      }
                  }
                  DelegateChoice {
                      roleValue: false
                      delegate: Text {
                          text: tabledata
                      }
                  }
              }
          }
    

    When I start the app I get the following errors in the application output:

    TableView: failed loading index: 0
    TableView: failed loading index: 21
    TableView: failed loading index: 42
    TableView: failed loading index: 63
    TableView: failed loading index: 84
    TableView: failed loading index: 105
    TableView: failed loading index: 126
    ...
    

    What does this mean?

    There is the following delegate in the original example (that I cloned and modified):

    delegate: Rectangle {
                  implicitWidth: 150
                  implicitHeight: 50
                  border.color: "black"
                  border.width: 2
                  color: (heading==true)?"red":"green"
    
                  TableView.onPooled: console.log(tabledata + " pooled")
                  TableView.onReused: console.log(tabledata + " resused")
    
                  Text {
                      text: tabledata
                      font.pointSize: 12
                      anchors.centerIn: parent
                  }
              }
    

    that works fine, but if I replace it with my DelegateChooser the app stops working.

    My QT version is 5.13.



  • hi @dmitriano

    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        TableView {
            anchors.fill: parent
            columnSpacing: 1
            rowSpacing: 1
            clip: true
            model: TableModel {}
            delegate: DelegateChooser {
                role: "type"
                DelegateChoice {        
                 roleValue: "lab2"     
                    delegate: Rectangle {
                        Text {
                            id: label2
                            text: "type delegate"
                        }
                    }
                }
                DelegateChoice {
                    // roleValue: "String" // default delegate
                    delegate: Rectangle {
                         color: ms.containsMouse ? "#d2cc01" : "lightgrey"
                        MouseArea{
                            anchors.fill: parent
                            hoverEnabled: true
                            id:ms
                        }
                        implicitWidth: defaultLabel.implicitWidth + 8
                        implicitHeight: defaultLabel.implicitHeight + 4
                        Text {
                            id: defaultLabel
                            text: tabledata
                        }
                    }
                }
            }
        }
    }