Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Access delegate item properties from outside



  • I have a flickable GridView. In this GridView I have custom buttons. My custom buttons change their border color when they are pressed, and when they are released, the border color changes back to the original.

    Now my problem: If I start a flick on a button, the border color is changing (which is OK). However, after the flick is done, the release event is stolen by the GridView, so the button color does not change back. I thought I will fix this by creating a function which is called inside onFlickEnded{}:

    function resetButtonColorAfterFlick() {
        var i
        for(i=0; i<gridView.model.count;i++) {
            var myob =gridView.children[0].children[i]
            if(myob.children[0].state=="normal")
                myob.children[0].border.color="#646464"
        }
    }
    

    While this is working, I don't know how I am supposed to access the delegate properties here. The above solution with children cannot be how it is supposed to be done, or?



  • @maxwell31 Hi,

    How do you make the border color changing ? Can we see the code of the delegate ?
    In my opinion it's better to handle this in the code of the button instead of accessing to the delegate to change it's state.

    Here is a simple code which work on my side. It uses the State mechanism:

    //main.qml
    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        GridView{
            anchors.fill: parent
            model: 50
            delegate: MyButton{
                text: "" + index
            }
        }
    }
    
    //MyButton.qml
    
    Button {
        background: Rectangle{
            id: bg
            border.width: 1
            border.color: "blue"
        }
    
        states: [
            State {
                name: "pressed"
                PropertyChanges { target: bg; border.color: "red" }        }
        ]
        state: pressed ? "pressed" : ""
    }
    

Log in to reply