Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-69771

Functions in QML model data not accessible

    XMLWordPrintable

Details

    Description

      When populating the model of a Repeater with Javascript objects and later accessing properties from those objects, all properties seem to be available except for functions (or possibly all non-POD types). These will be undefined when trying to access them through the model.

      See following code for illustration:

      import QtQuick 2.9
      import QtQuick.Window 2.2
      
      Window {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          Column {
              anchors.centerIn: parent
      
              Repeater {
                  id: menu
      
                  model: [
                      {
                          label: "menu item #1",
                          payload: 123,
                          trigger: function() {
                              console.debug("menu item 1 triggered")
                          }
                      },
      
                      {
                          label: "menu item #2",
                          payload: 345,
                          trigger: function() {
                              console.debug("menu item 2 triggered")
                          }
                      }
                  ]
      
                  delegate: Text {
                      text: modelData.label
      
                      MouseArea {
                          anchors.fill: parent
                          onClicked: {
                              console.debug("Clicked "
                                            + modelData.label
                                            + " with payload "
                                            + modelData.payload
                                            + " and trigger function: " + modelData.trigger)
                              modelData.trigger()
                          }
                      }
                  }
              }
          }
      }
      

      Notice when clicking the menu items that the label and payload are accessible, but that the trigger function is considered undefined.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              esabraha Eskil Abrahamsen Blomfeldt
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes