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. Update and reset the text color of Selected List Item

Update and reset the text color of Selected List Item

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

    Hi,

    I am trying to update and reset the text color of current selected list item and resetting the color to default on change in the current list item. But all the time the items are selected with default color
    Any help is appreciated, please find my code below

    main.qml

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        ListView {
            id: lv
            anchors.fill: parent
    
            model: ListModel {
                ListElement {name: "Hai"}
                ListElement {name: "Hai1"}
                ListElement {name: "Hai2"}
                ListElement {name: "Hai3"}
                ListElement {name: "Hai4"}
                ListElement {name: "Hai5"}
                ListElement {name: "Hai6"}
                ListElement {name: "Hai7"}
            }
    
            delegate: MsgItemDelegate {
                    onButtonClicked: {
                        lv.currentIndex = index
                        console.error("==============ListView.currentIndex==>", ListView.isCurrentItem)
                    }
            }
    
            highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
        }
    }
    

    MsgItemDelegate.qml

    import QtQuick 2.9
    
    import QtQuick.Controls 2.2
    
    ItemDelegate {
        id: root
    
        signal buttonClicked
    
        MouseArea{
            anchors.fill: parent
            onClicked: {
            root.buttonClicked()
            }
        }
    
        Label {
            id: txtLbl
    
            text: model.name
            color: lv.isCurrentItem ? "white" : "blue"
        }
    }
    

    Thanks in advance !

    Regards,
    AROH

    TassosT 1 Reply Last reply
    0
    • A AROH

      Hi,

      I am trying to update and reset the text color of current selected list item and resetting the color to default on change in the current list item. But all the time the items are selected with default color
      Any help is appreciated, please find my code below

      main.qml

      import QtQuick 2.9
      import QtQuick.Window 2.2
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
      
          ListView {
              id: lv
              anchors.fill: parent
      
              model: ListModel {
                  ListElement {name: "Hai"}
                  ListElement {name: "Hai1"}
                  ListElement {name: "Hai2"}
                  ListElement {name: "Hai3"}
                  ListElement {name: "Hai4"}
                  ListElement {name: "Hai5"}
                  ListElement {name: "Hai6"}
                  ListElement {name: "Hai7"}
              }
      
              delegate: MsgItemDelegate {
                      onButtonClicked: {
                          lv.currentIndex = index
                          console.error("==============ListView.currentIndex==>", ListView.isCurrentItem)
                      }
              }
      
              highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
          }
      }
      

      MsgItemDelegate.qml

      import QtQuick 2.9
      
      import QtQuick.Controls 2.2
      
      ItemDelegate {
          id: root
      
          signal buttonClicked
      
          MouseArea{
              anchors.fill: parent
              onClicked: {
              root.buttonClicked()
              }
          }
      
          Label {
              id: txtLbl
      
              text: model.name
              color: lv.isCurrentItem ? "white" : "blue"
          }
      }
      

      Thanks in advance !

      Regards,
      AROH

      TassosT Offline
      TassosT Offline
      Tassos
      wrote on last edited by
      #2

      @AROH ListView's isCurrentItem property is an attached one.

      You should used it as:

          Label {
              id: txtLbl
              text: model.name
              color: root.ListView.isCurrentItem ? "white" : "blue"
          }
      
      A 1 Reply Last reply
      1
      • TassosT Tassos

        @AROH ListView's isCurrentItem property is an attached one.

        You should used it as:

            Label {
                id: txtLbl
                text: model.name
                color: root.ListView.isCurrentItem ? "white" : "blue"
            }
        
        A Offline
        A Offline
        AROH
        wrote on last edited by
        #3

        @Tassos Thanks for the solution it worked.

        1 Reply Last reply
        0
        • SGaistS SGaist has marked this topic as solved on

        • Login

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