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

How to update component(text) in itemDelegate at the run time?



  • import QtQuick 2.13
    import QtQuick.Controls 2.13

    Item {
    id: menu
    width: parent.width
    height: parent.height

    SwipeView{
        id:menuSwipe
        x:20
        y:100
        height: parent.height
        width: 600
        clip: true
        orientation: Qt.Horizontal
        currentIndex: 0
    	    Item {
            id: pageOne
            Rectangle{
                id: rect1
                height: 144
                width: 132
                anchors.left: parent.left
                anchors.leftMargin: 18
                anchors.top: parent.top
                anchors.topMargin: 44
                color: "Transparent"
    			
             ListView {
                id: submenu1
                width: parent.width
                height: parent.height
                spacing: 12
                x:28
                y:216
                anchors.left: parent.left
                anchors.right:parent.right
                anchors.leftMargin: 28
                anchors.rightMargin: 28
                model: modelOne
    		delegate: Component {
                       ItemDelegate{
    
                        height: 68
                        width: 396
                        anchors.horizontalCenter: parent.horizontalCenter
                        Rectangle {
                            anchors.fill: parent
                            radius: 10
                            color: "transparent"
    						
    		        Image {
                                id: arrowImage
                                relativeSource: "image.png"
                                anchors.right: parent.right
                                anchors.verticalCenter: parent.verticalCenter
                                anchors.rightMargin: 20
                            }
    						
    						    Text {
                                id:  title
                                text: model.title
                                width: parent.width
                                height: parent.height
                                anchors.left: parent.left
                                anchors.leftMargin: 28
                                font.family: Theme.fontFamily
                                font.pixelSize: 32
                                horizontalAlignment: Text.AlignLeft
                                verticalAlignment: Text.AlignVCenter
                                color: "#ffffff"
    
                            }
                        }
                        onClicked: {
                            console.log("onClicked!")
    
                            }
    			}
    		}
    	}
        }
        }
       }
    
        ListModel {
        id: modelOne
        ListElement {
            title: title1
    		
        }
        ListElement {
            title: title2
           
        }
        ListElement {
            title: title3
            
        }
    }
    function signalUpdate()
    {
    	//disable the title1 button and change color of text
    }
    

    }

    How to update the title color of text component and disable the title1 button at the run time based upon signalUpdate.
    Please help.

    Note:This is a sample code of my problem.


  • Moderators

    This is described in depth in the documentation: https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html

    A ListModel is generally speaking static - for dynamic models it's generally better to go with C++ models.

    But still, ListModel does have some functions you can use to modify the model at runtime, see "Methods" section in the docs: https://doc.qt.io/qt-5/qml-qtqml-models-listmodel.html



  • I don't want to update the listmodel, instead would like to disable the title1 button in itemDelegate.



  • Updating via C++ model is best way to do it .Since you don't want it , you can define a signal for listview and write connections inside delegate.When you want to disable ,emit the signal and handle the functionality inside connections based on some condition matching for title1.But you should remember the connections will be executed for all delegates. For ex:

    ListView {
    id: view
    model: modelOne
    anchors.fill: parent
    signal update()
    delegate: Text {
    text: title
    Connections {
    target: view
    onUpdate:{
    console.log("updated "+title)
    }
    }
    }
    }


  • Moderators

    @Rajesh_Mandava said in How to update component(text) in itemDelegate at the run time?:

    would like to disable the title1 button in itemDelegate.

    That's a modification of the model. The delegate can't have any saved state - it only displays what the model tells it to. So even turning something on/off is a boolean property of the model - at least should be.


Log in to reply