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

[Solved] Receiving signals emitted by Repeater item



  • Is there a way to receive a signal from an item created by a Repeater in the QML itself? Consider the following simplified example:

    @Rectangle {

    Repeater {
    model: cppModel
    delegate: SimpleDelegate{}
    }
    }

    // SimpleDelegate.qml
    Item{

    signal itemSignal(int index)

    }@

    Is there a way to receive itemSignal somewhere where I can then operate on the item and, most likely, all the other items in the repeater as well? So far I haven’t been able to figure out how to handle this scenario without involving the C++ model.



  • I think you can accomplish that using connect (http://doc.qt.digia.com/qt/qmlevents.html#connecting-signals-to-methods-and-signals).

    Here is my guess:

    @delegate: SimpleDelegate{
    Component.onCompleted: {
    itemSignal.connect(someFunction)
    }
    }@



  • Certainly. You might want to read up on how to do signal and slot connections with [removed]
    http://doc.qt.digia.com/qt/qmlevents.html

    The most relevant example is this one:
    @
    Rectangle {
    id: forwarder
    width: 100; height: 100

     signal send()
     onSend: console.log("Send clicked")
    
     MouseArea {
         id: mousearea
         anchors.fill: parent
         onClicked: console.log("MouseArea clicked")
     }
     Component.onCompleted: {
         mousearea.clicked.connect(send)
     }
    

    }
    @

    Just connect your itemSignal to a function in the SimpleDelegate::onCompleted handler. (or alternatively in the Repeater::onItemAdded handler)



  • Yes, thank you! I was under the mistaken impression that I needed Connections items to implement signals and slots in QML. This solution works perfectly.


Log in to reply