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

QML - object list data type

    XMLWordPrintable

Details

    Description

      I know that the general recommendation is to create a model in C++ and export it to QML, but sometimes it's more convenient to create a list of objects directly in QML:

      // Product.qml
      QtObject {
          property string name
          property real price
          property url photoUrl
          property color color
      }
      
      // main.qml
      Item {
          property <list of Product> products
          
          TableView {
              model: products
              ...
          }
          
          Component {
              id: productComponent
              Product {}
          }
          
          onSomething: {
              products.append(productComponent.createObject(...))
          }
      }
      

      The question is, what type should the 'products' property have? I believe that all types available in QML now are not suitable.

      • ListModel
        Its elements are not real objects, they can't have signals, functions and property bindings, property types are limited (string, number, bool).
      • js array
        Not strongly typed (can contain anything, not only the Product).
        Does not notify a view about adding/removing elements.
      • list<T>
        "Objects cannot be individually added to or removed from the list once created; to modify the contents of a list, it must be reassigned to a new list."

      I propose to implement a new QML data type (or adapt list<T>), which will not have the mentioned drawbacks, it should:

      • Store objects of a certain type (like list<T>).
      • Has a rich method set for content manipulating (QList is a good source for inspiration).
      • Works with views properly (notify about changes).

      Attachments

        Issue Links

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

          Activity

            People

              Unassigned Unassigned
              t.artikov Timur Artikov
              Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes