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. ListView example
Forum Updated to NodeBB v4.3 + New Features

ListView example

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 390 Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    qcoderpro
    wrote on last edited by
    #1

    This program runs well:

    import QtQuick
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        Rectangle {
            id: root
            anchors.centerIn: parent
            width: 200; height: 150
    
            ListModel {
                id: listMd
                ListElement { name: "Bill Smith" }
                ListElement { name: "John Brown" }
                ListElement { name: "Sam Wise"   }
                ListElement { name: "John Brown" }
                ListElement { name: "Bill Smith" }
                ListElement { name: "John Brown" }
                ListElement { name: "Bill Smith" }
                ListElement { name: "John Brown" }
                ListElement { name: "Sam Wise"   }
                ListElement { name: "John Brown" }
                ListElement { name: "Bill Smith" }
                ListElement { name: "John Brown" }
            }
    
            Component {
                id: contactDelegate
                Rectangle {
                    width: root.width
                    implicitHeight: txt.height
                    color: lv.currentIndex == index ? "lightgreen" : "lightblue"
    
                    MouseArea {
                        anchors.fill: parent
                        onClicked: lv.currentIndex = index
                    }
    
                    Text {
                        id: txt
                        text: qsTr(name)
                        font.pixelSize: 30
                    }
                }
            }
    
            ListView {
                id: lv
                anchors.fill: parent
                model: listMd
                delegate: contactDelegate
                clip: true
                focus: true
            }
        }
    }
    

    What I want to is to have the list view without a default selected item (with the color of light green). That is, all elements are to be in "lightblue" except for the time we click one. Any idea?

    My second question is about index in color: lv.currentIndex == index ? "lightgreen" : "lightblue" which I couldn't find its definition in the Docs!

    L 1 Reply Last reply
    0
    • Q qcoderpro

      This program runs well:

      import QtQuick
      
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          Rectangle {
              id: root
              anchors.centerIn: parent
              width: 200; height: 150
      
              ListModel {
                  id: listMd
                  ListElement { name: "Bill Smith" }
                  ListElement { name: "John Brown" }
                  ListElement { name: "Sam Wise"   }
                  ListElement { name: "John Brown" }
                  ListElement { name: "Bill Smith" }
                  ListElement { name: "John Brown" }
                  ListElement { name: "Bill Smith" }
                  ListElement { name: "John Brown" }
                  ListElement { name: "Sam Wise"   }
                  ListElement { name: "John Brown" }
                  ListElement { name: "Bill Smith" }
                  ListElement { name: "John Brown" }
              }
      
              Component {
                  id: contactDelegate
                  Rectangle {
                      width: root.width
                      implicitHeight: txt.height
                      color: lv.currentIndex == index ? "lightgreen" : "lightblue"
      
                      MouseArea {
                          anchors.fill: parent
                          onClicked: lv.currentIndex = index
                      }
      
                      Text {
                          id: txt
                          text: qsTr(name)
                          font.pixelSize: 30
                      }
                  }
              }
      
              ListView {
                  id: lv
                  anchors.fill: parent
                  model: listMd
                  delegate: contactDelegate
                  clip: true
                  focus: true
              }
          }
      }
      

      What I want to is to have the list view without a default selected item (with the color of light green). That is, all elements are to be in "lightblue" except for the time we click one. Any idea?

      My second question is about index in color: lv.currentIndex == index ? "lightgreen" : "lightblue" which I couldn't find its definition in the Docs!

      L Offline
      L Offline
      lemons
      wrote on last edited by
      #2

      @qcoderpro

      1. simply set the currentIndex to -1
      ListView {
          id: lv
          // ...
          currentIndex: -1
          // ...
      }
      
      1. currentIndex is the currently selected index of the ListView and index is the index of the current delegate.
        So index just reflects the "loop index" for filling the ListView with your components
        (first component has index=0, second component has index=1, etc.)
      Q 1 Reply Last reply
      1
      • L lemons

        @qcoderpro

        1. simply set the currentIndex to -1
        ListView {
            id: lv
            // ...
            currentIndex: -1
            // ...
        }
        
        1. currentIndex is the currently selected index of the ListView and index is the index of the current delegate.
          So index just reflects the "loop index" for filling the ListView with your components
          (first component has index=0, second component has index=1, etc.)
        Q Offline
        Q Offline
        qcoderpro
        wrote on last edited by
        #3

        @lemons
        So currentIndex: -1 means no index is selected by default, right? (since indexes starts from 0 on the ListView by default)

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          Do you read the doc?
          https://doc.qt.io/qt-6/qml-qtquick-listview.html#currentIndex-prop

          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