Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML TreeView

QML TreeView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 6 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrahamLa
    wrote on 2 Apr 2019, 10:09 last edited by
    #1

    Hi
    I need to implement a QML TreeView representing some data.
    The representation requires that rows have to be displayed differently - for example some rows have a checkbox some dont.
    I have managed to get all rows to have a checkbox but cant understand how to style each row differently.
    This is what I have so far

    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        TreeView {
            model: myModel
    
            alternatingRowColors: false
            anchors.fill: parent
    
            headerDelegate: {
                visible: false
            }
    
            TableViewColumn {
                title: "Name"
                role: "display"
                width: 300
            }
    
    
            itemDelegate: Item {
                id: itemId
    
                CheckBox {
                    anchors.verticalCenter: parent.verticalCenter
                    text: styleData.value
                }
            }
        }
    }
    

    And this is what I am aiming for
    0_1554199731131_df910128-4d66-4acb-876e-8f368d77f047-image.png

    Thanks

    1 Reply Last reply
    0
    • O Offline
      O Offline
      ocgltd
      wrote on 13 Apr 2021, 21:37 last edited by
      #2

      Sad that after 2 years no one has answered. Many parts of the solution to this question would be of interest.

      D 1 Reply Last reply 14 Apr 2021, 17:53
      0
      • O ocgltd
        13 Apr 2021, 21:37

        Sad that after 2 years no one has answered. Many parts of the solution to this question would be of interest.

        D Offline
        D Offline
        Diracsbracket
        wrote on 14 Apr 2021, 17:53 last edited by Diracsbracket
        #3

        @GrahamLa,
        @ocgltd said in QML TreeView:

        Sad that after 2 years no one has answered. Many parts of the solution to this question would be of interest.

        This may be of interest:
        https://stackoverflow.com/questions/31985972/different-delegates-for-qml-listview

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fcarney
          wrote on 14 Apr 2021, 20:19 last edited by
          #4

          DelegateChooser

          C++ is a perfectly valid school of magic.

          1 Reply Last reply
          2
          • C Offline
            C Offline
            chapayev
            wrote on 20 Mar 2025, 12:31 last edited by
            #5

            import QtQuick 2.15
            import QtQuick.Controls 2.15

            ApplicationWindow {
            visible: true
            width: 400
            height: 600

            ListView {
                id: listView
                anchors.fill: parent
                model: ListModel {
                    ListElement { name: "Google Drive"; expanded: false }
                    ListElement { name: "Slack"; expanded: false }
                    ListElement { name: "Asana"; expanded: false }
                    ListElement { name: "Basecamp 3"; expanded: false }
                    ListElement { name: "GoToMeeting"; expanded: false }
                    ListElement { name: "Bigin"; expanded: false }
                    ListElement { name: "Zoho CRM"; expanded: false }
                    ListElement { name: "Zoho Desk"; expanded: false }
                    ListElement { name: "Shopify"; expanded: false }
                    ListElement { name: "WooCommerce"; expanded: false }
                    ListElement { name: "Zoho Campaigns"; expanded: false }
                }
            
                delegate: Column {
                    width: listView.width
            
                    Rectangle {
                        width: parent.width
                        height: 50
                        color: "white"
                        border.color: "#ddd"
            
                        Row {
                            anchors.fill: parent
                            spacing: 10
                            padding: 10
            
                            Rectangle {
                                width: 30
                                height: 30
                                radius: 5
                                color: "#f0f0f0" // Placeholder for an icon
                                anchors.verticalCenter: parent.verticalCenter
                            }
            
                            Text {
                                text: model.name
                                anchors.verticalCenter: parent.verticalCenter
                                font.pixelSize: 16
                            }
            
                            Item {
                                width: 1
                                height: 1
                                Layout.fillWidth: true
                            }
            
                            Text {
                                text: model.expanded ? "▲" : "▼"
                                font.pixelSize: 18
                                anchors.verticalCenter: parent.verticalCenter
                                MouseArea {
                                    anchors.fill: parent
                                    onClicked: model.expanded = !model.expanded
                                }
                            }
                        }
                    }
            
                    // Expandable sublist
                    Column {
                        visible: model.expanded
                        width: parent.width
            
                        Repeater {
                            model: 3 // Three dummy sub-items for each expanded item
                            delegate: Rectangle {
                                width: parent.width
                                height: 40
                                color: "#f9f9f9"
                                border.color: "#eee"
            
                                Text {
                                    text: modelData
                                    anchors.centerIn: parent
                                    font.pixelSize: 14
                                }
                            }
                        }
                    }
                }
            }
            

            }

            JonBJ 1 Reply Last reply 20 Mar 2025, 12:54
            0
            • C chapayev
              20 Mar 2025, 12:31

              import QtQuick 2.15
              import QtQuick.Controls 2.15

              ApplicationWindow {
              visible: true
              width: 400
              height: 600

              ListView {
                  id: listView
                  anchors.fill: parent
                  model: ListModel {
                      ListElement { name: "Google Drive"; expanded: false }
                      ListElement { name: "Slack"; expanded: false }
                      ListElement { name: "Asana"; expanded: false }
                      ListElement { name: "Basecamp 3"; expanded: false }
                      ListElement { name: "GoToMeeting"; expanded: false }
                      ListElement { name: "Bigin"; expanded: false }
                      ListElement { name: "Zoho CRM"; expanded: false }
                      ListElement { name: "Zoho Desk"; expanded: false }
                      ListElement { name: "Shopify"; expanded: false }
                      ListElement { name: "WooCommerce"; expanded: false }
                      ListElement { name: "Zoho Campaigns"; expanded: false }
                  }
              
                  delegate: Column {
                      width: listView.width
              
                      Rectangle {
                          width: parent.width
                          height: 50
                          color: "white"
                          border.color: "#ddd"
              
                          Row {
                              anchors.fill: parent
                              spacing: 10
                              padding: 10
              
                              Rectangle {
                                  width: 30
                                  height: 30
                                  radius: 5
                                  color: "#f0f0f0" // Placeholder for an icon
                                  anchors.verticalCenter: parent.verticalCenter
                              }
              
                              Text {
                                  text: model.name
                                  anchors.verticalCenter: parent.verticalCenter
                                  font.pixelSize: 16
                              }
              
                              Item {
                                  width: 1
                                  height: 1
                                  Layout.fillWidth: true
                              }
              
                              Text {
                                  text: model.expanded ? "▲" : "▼"
                                  font.pixelSize: 18
                                  anchors.verticalCenter: parent.verticalCenter
                                  MouseArea {
                                      anchors.fill: parent
                                      onClicked: model.expanded = !model.expanded
                                  }
                              }
                          }
                      }
              
                      // Expandable sublist
                      Column {
                          visible: model.expanded
                          width: parent.width
              
                          Repeater {
                              model: 3 // Three dummy sub-items for each expanded item
                              delegate: Rectangle {
                                  width: parent.width
                                  height: 40
                                  color: "#f9f9f9"
                                  border.color: "#eee"
              
                                  Text {
                                      text: modelData
                                      anchors.centerIn: parent
                                      font.pixelSize: 14
                                  }
                              }
                          }
                      }
                  }
              }
              

              }

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 20 Mar 2025, 12:54 last edited by
              #6

              @chapayev
              I know nothing about QML. Your code looks to me as though it handles expansion/contraction, and subitems? Are you saying this addresses the OP's question

              The representation requires that rows have to be displayed differently - for example some rows have a checkbox some dont.

              ?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved