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 ListView visible
Forum Update on Monday, May 27th 2025

QML ListView visible

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 3.9k 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.
  • B Offline
    B Offline
    beh_zad
    wrote on 15 Apr 2015, 09:38 last edited by p3c0
    #1

    Hi everybody, I found this codes in qml example file, but I have one question, is there any way to hide children of sectionHeading when user click on it, for example when user click on Tiny, its children visibility "Ant" and "Flea will be hide?

    Rectangle {
        id: container
        width: 300
        height: 360
    
        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" }
        }
    
        // The delegate for each section header
        Component {
            id: sectionHeading
            Rectangle {
                width: container.width
                height: childrenRect.height
                color: "lightsteelblue"
    
                Text {
                    text: section
                    font.bold: true
                    font.pixelSize: 20
                }
            }
        }
    
        ListView {
            id: view
            anchors.top: parent.top
            anchors.bottom: buttonBar.top
            width: parent.width
            model: animalsModel
            delegate: Text { text: name; font.pixelSize: 18 }
    
            section.property: "size"
            section.criteria: ViewSection.FullString
            section.delegate: sectionHeading
        }
    
        Row {
            id: buttonBar
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 1
            spacing: 1
        }
    }
    
    P 1 Reply Last reply 15 Apr 2015, 10:31
    0
    • B beh_zad
      15 Apr 2015, 09:38

      Hi everybody, I found this codes in qml example file, but I have one question, is there any way to hide children of sectionHeading when user click on it, for example when user click on Tiny, its children visibility "Ant" and "Flea will be hide?

      Rectangle {
          id: container
          width: 300
          height: 360
      
          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" }
          }
      
          // The delegate for each section header
          Component {
              id: sectionHeading
              Rectangle {
                  width: container.width
                  height: childrenRect.height
                  color: "lightsteelblue"
      
                  Text {
                      text: section
                      font.bold: true
                      font.pixelSize: 20
                  }
              }
          }
      
          ListView {
              id: view
              anchors.top: parent.top
              anchors.bottom: buttonBar.top
              width: parent.width
              model: animalsModel
              delegate: Text { text: name; font.pixelSize: 18 }
      
              section.property: "size"
              section.criteria: ViewSection.FullString
              section.delegate: sectionHeading
          }
      
          Row {
              id: buttonBar
              anchors.bottom: parent.bottom
              anchors.bottomMargin: 1
              spacing: 1
          }
      }
      
      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 15 Apr 2015, 10:31 last edited by
      #2

      @beh_zad Yes. Create a MouseArea for Rectangle in sectionHeading. Add a global property which will track the current clicked header section. Bind this to the ListView.section of delegate Item ( which is here Text) and change the visible property. Thus when section is clicked the binding will be affected and thus visible property will change. To sum up:

      property string psection  //global property - holds clicked section
      ...
      
      Component {
              id: sectionHeading
              Rectangle {
                  width: container.width
                  ...
                  MouseArea {
                      anchors.fill: parent
                      onClicked: psection = section // update global psection property
                  }
             }
      }
      ...
      
      ListView {
              id: view
              ...
              delegate: Text {
                  text: name; font.pixelSize: 18; visible : ListView.section===psection ? false: true //the binding which will be executed on global property change.
              }
      }
      

      Hope this helps...

      157

      1 Reply Last reply
      1
      • B Offline
        B Offline
        beh_zad
        wrote on 15 Apr 2015, 12:04 last edited by beh_zad
        #3

        @p3c0 thanks for your help. but it works for one section, and when I click another section, the previous section will be opened :( . I want each section work separately. I will be grateful if you could help me to solving this

        P 1 Reply Last reply 16 Apr 2015, 05:56
        0
        • B beh_zad
          15 Apr 2015, 12:04

          @p3c0 thanks for your help. but it works for one section, and when I click another section, the previous section will be opened :( . I want each section work separately. I will be grateful if you could help me to solving this

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 16 Apr 2015, 05:56 last edited by
          #4

          @beh_zad Well you can add a check that if it is already visited then don’t make it visible.

          157

          1 Reply Last reply
          0
          • B Offline
            B Offline
            beh_zad
            wrote on 22 Apr 2015, 06:33 last edited by
            #5

            answer:
            http://stackoverflow.com/questions/29647291/qt-qml-listview-visible

            P 1 Reply Last reply 22 Apr 2015, 07:15
            1
            • B beh_zad
              22 Apr 2015, 06:33

              answer:
              http://stackoverflow.com/questions/29647291/qt-qml-listview-visible

              P Offline
              P Offline
              p3c0
              Moderators
              wrote on 22 Apr 2015, 07:15 last edited by
              #6

              @beh_zad Thanks for sharing.

              157

              1 Reply Last reply
              0

              1/6

              15 Apr 2015, 09:38

              • Login

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