Ed Nio
Ed Nio

Reputation: 593

How to send a signal to all element of a listView from one of these element

I use a ListView and I try to send a signal to all my Elements of this ListView.

The Signal has to be sent by one of the ListView's Element.

I tried this but only the sending Element catch the Signal :

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    id: window
    visible: true
    width: 640
    height: 480

    ListModel {
        id: modelList
        ListElement {
            name: "Bill Smith"
        }
        ListElement {
            name: "John Brown"    
        }
        ListElement {
            name: "Sam Wise"
        }
    }

    ListView {
        width: window.width
        height: window.height    
        model: modelList
        delegate: Rectangle {
            id:elem
            signal activated()    
            width: window.width
            height: window.height/10
            color: "red"    
            Button {
                text: name
                anchors{
                    right: parent.right
                    top: parent.top
                    bottom: parent.bottom
                    margins: 10
                }
                onClicked: {
                    elem.activated()
                }
            }
            onActivated: {
                console.log("test")
            }
        }
    }
}

I think I misunderstood the doc but I have trouble to find a good example for this case, any idea ?

Upvotes: 1

Views: 909

Answers (1)

e.jahandar
e.jahandar

Reputation: 1763

You can define a signal in ListView's parent (eg. ApplicationWindow ) and write a signal handler inside delegates and connect it to signal defined in parent

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0

ApplicationWindow {
    id: window
    visible: true
    width: 640
    height: 480

    signal someSignal(int arg);

    ListModel {
        id: modelList
        ListElement {
            name: "Bill Smith"
        }
        ListElement {
            name: "John Brown"    
        }
        ListElement {
            name: "Sam Wise"
        }
    }

    ListView {
        width: window.width
        height: window.height    
        model: modelList
        delegate: Rectangle {
            id:elem
            signal activated()    
            width: window.width
            height: window.height/10
            color: "red"
            Connections{
                target: window
                onSomeSignal: {
                     console.log("Argument: " + arg);
                }
            } 
            Button {
                text: name
                anchors{
                    right: parent.right
                    top: parent.top
                    bottom: parent.bottom
                    margins: 10
                }
                onClicked: {
                    window.someSignal(1);
                }
            }
            onActivated: {
                console.log("test")
            }
        }
    }
}

Upvotes: 2

Related Questions