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

send signal from ListElement to button



  • hello
    i have two popup components ( popid1 & popid2) which i want to opens by buttons in a GridView.
    the data of this button stored in a ListMode and ListElement.each element have a parameter called "nambero". i wanna by clicking on each button diffrent popup open.

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.0
    import QtQuick.Controls 2.2
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        GridView {
            id: gridView
            width: 88
            height: 79
            transformOrigin: Item.Center
            contentHeight: 0
            cellHeight: 40
            delegate: Item {
                x: 5
                height: 50
                Column {
                    x: 202
                    y: 157
                    width: 160
                    height: 206
                    Button {
                        id: button
                        text: qsTr("Button")
                        width: 30
                        height:30
                        onClicked: popid+numbero.open()
                    }
    
                    spacing: 5
                }
            }
            cellWidth: 40
            model: ListModel {
                ListElement {
                    name: "Grey"
                    colorCode: "grey"
                    numbero:1
                }
    
                ListElement {
                    name: "Red"
                    colorCode: "red"
                    numbero:2
    
                }
    
                ListElement {
                    name: "Blue"
                    colorCode: "blue"
                    numbero:3
    
                }
    
                ListElement {
                    name: "Green"
                    colorCode: "green"
                }
            }
            Popup {
    
                id:popid1
                width: parent.width / 1.2
                height: parent.height / 1.2
                x: (parent.width - width) / 2
                y: (parent.height - height) / 2
                dim: true
                modal: true
                focus: true
                closePolicy: Popup.Popup.CloseOnPressOutside | Popup.CloseOnPressOutsideParent
            }
    
            Popup {
    
                id:popid2
                width: parent.width /2
                height: parent.height / 2
                x: (parent.width - width) / 2
                y: (parent.height - height) / 2
                dim: true
                modal: true
                focus: true
                closePolicy: Popup.Popup.CloseOnPressOutside | Popup.CloseOnPressOutsideParent
            }
        }
    
    
    }
    
    


  • @Gojir4 Thanks my friend



  • @amir.sanaat Hi, I think you could store the reference on the popup in you ListElement:

    onClicked: popup.open()
    ...
    ListElement {
        name: "Grey"
        colorCode: "grey"
        numbero:1
        popup: popid1
    }
    

    Another solution could be having only one popup and then using a loader to display the GUI according to numbero



  • Dear @Gojir4 thank you for snap reply
    the way you said dosent work and i faced with "ListElement: cannot use script for property value" error.



  • @amir.sanaat Ok, was a long time I didn't code in QML, sorry. May be a switch can make the job. That's not very elegant but I think it will work. I don't have anything else yet.

    onClicked:{
        switch(numbero){
            1: popid1.open()
            2: popid2.open()
        }
    }
    


  • @Gojir4 Thanks my friend


Log in to reply