Could highlight object in Listview/Gridview be changed while running?



  • As title describing?
    Thank you.



  • I means that Could a highlight hold State inside?



  • Not really. Highlight requires Component (http://doc.qt.digia.com/qt/qml-component.html) and only elements that inherit from Qml Item can have state (http://doc.qt.digia.com/qt/qml-item.html).

    I guess you want to have different highlight bar based on some condition. That basically means you should use different highlight based on list state (example below that changes highlight component based on current item. It is without states but should be easy rewrite to use states).

    As side note: I personally don't use highlight at all. There might be good reasons to use it but I have found that it gives me more problems than solves. isCurrentItem is enough for me.

    @import QtQuick 1.1

    Rectangle {
    width: 180; height: 200

    ListModel {
        id: contactsModel
        ListElement {
            name: "Bill Smith"
            number: "555 3264"
        }
        ListElement {
            name: "John Brown"
            number: "555 8426"
        }
        ListElement {
            name: "Sam Wise"
            number: "555 0473"
        }
    }
    
    Component {
        id: contactDelegate
        Item {
            width: 180; height: 40
            Column {
                Text { text: '<b>Name:</b> ' + name }
                Text { text: '<b>Number:</b> ' + number }
            }
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    myList.currentIndex = index
                    myList.highlight = index % 2 == 0 ? highlightBar : highlightBar2;
                }
            }
        }
    }
    
    Component {
             id: highlightBar
    
             Rectangle {
                 color: "blue"
             }
         }
    
    Component {
             id: highlightBar2
    
             Rectangle {
                 color: "red"
             }
         }
    
    ListView {
        id: myList
        anchors.fill: parent
        model: contactsModel
        delegate: contactDelegate
        highlight: highlightBar
        focus: true
    }
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.