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

Error assigning a function declaration to a ListElement role

    XMLWordPrintable

Details

    • Linux/X11

    Description

      Documentation states:

      Beginning with Qt 5.11 ListElement also allows assigning a function declaration to a role. This allows the definition of ListElements with callable actions.

      Trying to emit signal from ListView delegate:

          signal signal1
          signal signal2
          signal signal3
      
          ListView {
              id: listView
              model: myModel
      
              delegate: Item {
                  Label {
                      id: text
                      text: model.name
                  }
                  
                  MouseArea {
                      anchors.fill: parent
      
                      onClicked: {
                          model.func()
                      }
                  }
              }
          }
      

       

      1st model variant with func:

          ListModel {
              id: myModel
              ListElement {
                  name: qsTr("Item1")
                  func: signal1()
              }
              ListElement {
                  name: qsTr("Item2")
                  func: signal2()
              }
              ListElement {
                  name: qsTr("Item2")
                  func: signal3()
              }
          }
      

       

      Produces error:

       ListElement: cannot use script for property value

       

      2nd variant with append:

           ListModel {
              id: myModel
              Component.onCompleted: {
                  append({ name: qsTr("Item1"), func: function() {  signal1() }})
                  append({ name: qsTr("Item2"), func: function() { signal2() }})
                  append({  name: qsTr("Item3"), func: function() { signal3() }})
              }
          }
      

      Has other error:

      TypeError: Property 'func' of object QQmlDMAbstractItemModelData(0x67a25f0) is not a function

      Is it a bug or incorrect syntax? Documentation has no example.

       

      P.S. Current workaround with list<QtObject> array: https://stackoverflow.com/a/50625374/630169

      Attachments

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

        Activity

          People

            ulherman Ulf Hermann
            jirauser39314 user-45b5b (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes