Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Qt signal from model item to QML slot
Forum Updated to NodeBB v4.3 + New Features

Qt signal from model item to QML slot

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlsignal & slotlistview
4 Posts 3 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ionick
    wrote on last edited by
    #1

    Hey there. I've exhausted my googling and was hoping to get some direction from the forums.

    I'm feeding a ListView with a model derived from QAbstractList. The items in this model are QObjects. Within the ListView, I'd like to be able to have QML elements in the delegate listen to signals on their respective QObjects. Is this possible?

    What I've found suggests that every model item has to somehow be exposed using setContextProperty, but that seems extremely tedious. Does the QML front-end not already know that the object in the ListView model has available signals?

    Thanks for any guidance.

    An example of the QML. In this case, the color of the model item is being changed somewhere in the backend, and I'd like the QML to listen for the color change signals.

    ListView {
        id: dataListView
        anchors.fill: parent
        model: itemModel
    
        delegate: ItemDelegate {
            Rectangle {
                id: itemColorIcon
                anchors.left: parent.left
                width: 20; height: 20
                radius: width * 0.5
                color: model.item.color
    
                Component.onCompleted: {
                    // This is where I'd like to listen for signals on the model item
                    model.item.color_changed.connect(
                        function () {
                            color = model.item.color;
                        }
                    )
                }
            }
    
            Text {
                id: itemText
                Layout.fillWidth: true
                text: model.item.name
                elide: Text.ElideRight
            }
        }
    }
    
    1 Reply Last reply
    0
    • I Offline
      I Offline
      ionick
      wrote on last edited by ionick
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • N Offline
        N Offline
        NunuM
        wrote on last edited by
        #3

        Hi Ionick,

        Did you find any way to do what you sad? Turns out that I am being searching the same thing, and I do not found any example in how to attach a handler for each item from an exposed C++ model.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          In this case, the color of the model item is being changed somewhere in the backend

          You don't require to do this. This is where Model View framework comes into picture. color change should automatically taken care by the model itself. I'm assuming that you have written custom model. Are you sending the dataChanged(..) signal whenever data is changed at model ?

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved