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 - how to highlight current item
QtWS25 Last Chance

ListView - how to highlight current item

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 2.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.
  • P Offline
    P Offline
    PawlosCK
    wrote on last edited by
    #1

    How to highlight current item? I tried a lot of things and nothing work.

    import QtQuick.Window 2.15
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.15
    import QtQuick 2.15
    import QtQml 2.15
    //import QtQuick 2.0
    
    Window
    {
        id: alarmListWindow
    
        width: 400
        height: 400
    
        ListModel
        {
            id: modelID
    
            ListElement
            {
                idAlarm: 0
                remainingTime: 0
                typeAlarm: ""
                messageAlarm: ""
            }
        }
        Component
        {
            id: delegatID
            Rectangle
            {
                id: alarmItemDelegate
                width: listviewID.width
                height: 45
                border.width: 1
                radius: 5
    
                Text
                {
                    id: remainingAl
                    x: 5
                    y: 0
                    text: "Remaining: " + model.remainingTime
                }
                Text
                {
                    id: typeAl
                    x: 5
                    y: remainingAl.height
                    text: "Type: " + model.typeAlarm
                }
                Text
                {
                    id: messageAl
                    x: 5
                    y: remainingAl.height*2
                    text: "message: " + model.messageAlarm
                }
            }
        }
        ListView
        {
            spacing: 5
            clip: true
    
            id: listviewID
    //        anchors.fill: parent
            width: parent.width
            height: parent.height/2
            model: modelID
            delegate: delegatID
    
            focus: true
            MouseArea
            {
                anchors.fill: parent
                onClicked: console.log("Wybrany alarm: " + listviewID.currentIndex)
            }
        }
    
        Timer
        {
            id: timerUpdateListView
            interval: 1000; running: true; repeat: true; triggeredOnStart: true
            onTriggered: updateListView()
        }
        function updateListView()
        {
    
            if (list_of_alarms.length !== modelID.count)
            {
                modelID.clear()
                for (var alarm of list_of_alarms)
                {
                    modelID.append( { "remainingTime": alarm.getRemainingTime(), "typeAlarm": alarm.getTypeAlarm(), "messageAlarm": alarm.getMessage()} )
                }
            }
            else
            {
                var index = 0
                for (var alarm of list_of_alarms)
                {
                    modelID.setProperty(index, "remainingTime", alarm.getRemainingTime() )
    //                modelID.setProperty(index, "typeAlarm", alarm.getTypeAlarm() )
    //                modelID.setProperty(index, "messageAlarm", alarm.getMessage() )
                    index++
                }
            }
        }
    }
    
    
    
    1 Reply Last reply
    0
    • DiracsbracketD Offline
      DiracsbracketD Offline
      Diracsbracket
      wrote on last edited by Diracsbracket
      #2

      See the example code at https://doc.qt.io/qt-5/qml-qtquick-listview.html#isCurrentItem-attached-prop

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PawlosCK
        wrote on last edited by
        #3

        I tried it and other ideas. I put

        color: ListView.isCurrentItem ? "black" : "red"
        

        into Rectangle and only first item is black and other are red. When I click other item, color is not changed.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PawlosCK
          wrote on last edited by PawlosCK
          #4

          https://github.com/pawlosck/QMLClock/blob/aaf6c65e5c1cd775c65a510f72d11de339da3709/alarm_list_window.qml

          The most important things:

          1. Item change color if is selected or not. Selected Item is black and other is red
          color: ListView.isCurrentItem ? "black" : "red"
          
          1. Add MouseArea with onClicked into ListView to change current Item using mouse.
                  MouseArea
                  {
                      anchors.fill: parent
          
                      onClicked: setCurrentItem(mouseX, mouseY)
                  }
          

          Full code of ListView

              ListView
              {
                  spacing: 5
                  clip: true
          
                  id: listviewID
                  anchors.fill: parent
          
                  width: parent.width
                  height: parent.height/2
                  model: modelID
                  delegate: delegatID
          
                  MouseArea
                  {
                      anchors.fill: parent
          
                      onClicked: setCurrentItem(mouseX, mouseY)
                  }
              }
          

          Full code of delegate with Items

            Component
              {
                  id: delegatID
                  Rectangle
                  {
                      id: alarmItemDelegate
                      width: listviewID.width
                      height: 50
                      border.width: 1
                      radius: 5
          
                      color: ListView.isCurrentItem ? "grey" : "transparent"
                      Item
                      {
                          anchors.fill: parent
                          Text
                          {
                              id: remainingAl
                              x: 5
                              anchors.top: parent.top
                              text: "Remaining: " + model.remainingTime
                              color: alarmItemDelegate.ListView.isCurrentItem ? "yellow" : "black"
                          }
                          Text
                          {
                              id: typeAl
                              x: 5
                              anchors.verticalCenter: parent.verticalCenter
                              text: "Type: " + model.typeAlarm
                              color: alarmItemDelegate.ListView.isCurrentItem ? "yellow" : "black"
                          }
                          Text
                          {
                              id: messageAl
                              x: 5
                              anchors.bottom: parent.bottom
                              text: "message: " + model.messageAlarm
                              color: alarmItemDelegate.ListView.isCurrentItem ? "yellow" : "black"
                          }
                      }
                  }
              }
          
          1 Reply Last reply
          1

          • Login

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