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. Problem dynamically adding items in a ListModel
Forum Updated to NodeBB v4.3 + New Features

Problem dynamically adding items in a ListModel

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 3.1k Views 2 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.
  • C Offline
    C Offline
    Cyrille de Brebisson
    wrote on 25 Oct 2018, 10:10 last edited by
    #1

    Hello,

    I have a list model with a number of elements:

        items: ListModel {
            id: pagesModel
            ListElement {
                pageTitle: qsTr ("Settings")
                pageIcon: "qrc:/Resources/settings.svg"
                action: function() {
                    stackView.push(Qt.resolvedUrl("/qml/Settings.qml"))
                }
            }
            ...
        }
    

    I want to dynamically add elements to the model. So I am doing:

                        pagesModel.append({"pageTitle": qsTr ("Title"),
                                           "pageIcon": "qrc:/Resources/stuff.svg",
                                           "action": function() { stackView.push(Qt.resolvedUrl("/qml/Stuff.qml"), {"deviceId":linkId}) },
                                          });
    

    However, at execution time, I get the following error:
    <Unknown File>: Can't assign to existing role 'action' of different type [VariantMap -> Function]

    Any idea what I might be doing wrong there?

    Thanks,
    Cyrille

    T 1 Reply Last reply 25 Oct 2018, 22:29
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 25 Oct 2018, 14:15 last edited by
      #2

      You can use the dynamicRoles:true. In the beginning don't have any elements. Dynamically add all of them. It should work. Not sure why this error though.

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

      1 Reply Last reply
      0
      • C Cyrille de Brebisson
        25 Oct 2018, 10:10

        Hello,

        I have a list model with a number of elements:

            items: ListModel {
                id: pagesModel
                ListElement {
                    pageTitle: qsTr ("Settings")
                    pageIcon: "qrc:/Resources/settings.svg"
                    action: function() {
                        stackView.push(Qt.resolvedUrl("/qml/Settings.qml"))
                    }
                }
                ...
            }
        

        I want to dynamically add elements to the model. So I am doing:

                            pagesModel.append({"pageTitle": qsTr ("Title"),
                                               "pageIcon": "qrc:/Resources/stuff.svg",
                                               "action": function() { stackView.push(Qt.resolvedUrl("/qml/Stuff.qml"), {"deviceId":linkId}) },
                                              });
        

        However, at execution time, I get the following error:
        <Unknown File>: Can't assign to existing role 'action' of different type [VariantMap -> Function]

        Any idea what I might be doing wrong there?

        Thanks,
        Cyrille

        T Offline
        T Offline
        Tom_H
        wrote on 25 Oct 2018, 22:29 last edited by Tom_H
        #3

        @Cyrille-de-Brebisson I don't think you can assign a function as a value. ListElement role values must be simple constants, i.e. strings, numbers, or booleans.

        Edit: Deleted wrong information as pointed out by @dheerendra

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cyrille de Brebisson
          wrote on 26 Oct 2018, 06:32 last edited by
          #4

          Hello,
          One solution that I found was to setup the "action" AFTER the append. This seemed to work...
          Why, I do not know. Another issue is that since append does not seem to return the position of insertion, what happends if another thread does an insert just after. my count-1 will refer to what at this point?

                          pagesModel.append({"pageTitle": qsTr (ttle),
                                             "pageIcon": icon,
                                             "model": m2
                                            });
                          pagesModel.setProperty(pagesModel.count-1, "action", function() { console.log("My Action"); stackView.push(Qt.resolvedUrl(url), {"deviceId":linkId}) }); // for some reason I can no do this in the object creation!
          

          Cyrille

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dheerendra
            Qt Champions 2022
            wrote on 26 Oct 2018, 07:59 last edited by
            #5

            @Tom_H Starting from 2.11 you can assign the functions.

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

            1 Reply Last reply
            1

            1/5

            25 Oct 2018, 10:10

            • Login

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