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. [solved] How to change values of a ListModel from States when it's in another qml file?
QtWS25 Last Chance

[solved] How to change values of a ListModel from States when it's in another qml file?

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 5.5k Views
  • 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.
  • Q Offline
    Q Offline
    qgil
    wrote on last edited by
    #1

    Hi, I'm trying to change values in a ListModel from States. The starting point is http://doc.qt.nokia.com/4.7-snapshot/qml-listmodel.html#set-method

    @fruitModel.set(3, {"cost": 5.95, "name":"Pizza"})@

    However, what would be the syntax in the example above if the ListModel is in another qml file e.g. TheFruitModel.qml ?

    And how can you set these changes from a State?

    I'm attempting to do it following this syntax, but somehow it doesn't work:

    @StateChangeScript { script: TheFruitModel.set(3, {"cost": 5.95, "name":"Pizza"}) }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      aalpert
      wrote on last edited by
      #2

      fruitModel is an id, not a type. TheFruitModel is a type. When you create an instance of the type, e.g. TheFruitModel{ id: fruitModel }, you can use that id to reference it like in the example.

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qgil
        wrote on last edited by
        #3

        Thanks aalpert, but at least in the example above running in a StateChangeScript I get this error:

        @StateChangeScript { script: fruitModel.set(3, {"cost": 5.95, "name":"Pizza"}) }

        @ReferenceError: Can't find variable: ...@

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          Where is your fruitModel declared? You can't access ids that are declared in another qml file (with exception of accessing ids that are declared in qml file which agregates current qml file).

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qgil
            wrote on last edited by
            #5

            Do you mean that I would need to 'import The FruitModel.qml'?

            The "GridView documentation":http://doc.qt.nokia.com/4.7-snapshot/qml-gridview.html says:

            bq. This model can be referenced as ContactModel in other QML files. See "QML Modules":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativemodules.html#qml-modules for more information about creating reusable components like this.

            But a simple example for newbies would be nice since the QML Modules page touches many things and at least for me it's not evident to see what I'm supposed to do.

            In the example of this thread fruitModel would be declared in TheFruitModel.qml, while

            @fruitModel.set(3, {"cost": 5.95, "name":"Pizza"})@

            would be in a different document, say MainPage.qml

            fwiw the real example I'm facing is that I want to place the set() method at "OnlineBoard.qml":https://gitorious.org/testdef/testdef/blobs/master/testdef/qml/OnlineBoard.qml , where GridView and delegate are defined, while the ListModel itself is placed in "BoardModel.qml":https://gitorious.org/testdef/testdef/blobs/master/testdef/qml/BoardModel.qml

            Thank you for your help!

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DenisKormalev
              wrote on last edited by
              #6

              So in your chessboardGrid you have model BoardModel {}.
              You can use
              @
              chessboardGrid.model.set(/.../);
              @

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qgil
                wrote on last edited by
                #7

                Yes! This works. Thanks a lot Denis (and aalpert).

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fgrs
                  wrote on last edited by
                  #8

                  would be nice to look at that example again but files have been removed

                  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