Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDeclarativeListProperty add item from qml application
Forum Updated to NodeBB v4.3 + New Features

QDeclarativeListProperty add item from qml application

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 6.6k Views 1 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.
  • G Offline
    G Offline
    goli
    wrote on last edited by
    #1

    down vote
    favorite is there any why to add item to QDeclarativeListProperty from qml file at run time? in a loop, for example:

    @var i;
    for(i = 0 ; i < 100 ; ++i)
    {
    listOfItems.append(MyItem {text:"list"+i})
    }@
    and listOfItems is the QDeclarativeListProperty list...
    i don't want to do that:
    @listOfItems:
    [
    MyItem{text:"list val1"},
    MyItem{text:"list val2"},
    ......
    ] @

    i have a lot of item that should be in this list

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sigrid
      wrote on last edited by
      #2

      In order for this to work, you need to implement the "AppendFunction":http://doc.qt.nokia.com/latest/qdeclarativelistproperty.html#AppendFunction-typedef of QDeclarativeListProperty. The example below shows how that can be done. It also implement "ClearFunction":http://doc.qt.nokia.com/4.7/qdeclarativelistproperty.html#ClearFunction-typedef so that it cleans up correctly.

      @
      #include <QtDeclarative>

      class MyObject : public QObject
      {
      Q_OBJECT
      Q_PROPERTY(QDeclarativeListProperty<MyObject> getInfo READ getInfo CONSTANT)
      public:
      MyObject()
      {}

      ~MyObject() 
      { 
      } 
      QDeclarativeListProperty<MyObject> getInfo() 
      { 
          for (int i = 0; i < 10; ++i) 
          { 
              list << new MyObject(); 
      

      }

          return QDeclarativeListProperty<MyObject>(this, 0, &MyObject::appendObject, 0, 0, &MyObject::clearObject); 
      }
      static void appendObject(QDeclarativeListProperty<MyObject> *l, MyObject *obj)
      {   
          MyObject *object = qobject_cast<MyObject *>(l->object);
          if (object)
              object->list << obj;
      }
      static void clearObject(QDeclarativeListProperty<MyObject> *l)
      {
          qDebug("In clear");
          MyObject *object = qobject_cast<MyObject *>(l->object);
          if (object) {
              foreach (MyObject *o, object->list)
                  delete o;
              object->list.clear();
          }
      }
      QList<MyObject *> list;
      

      };
      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goli
        wrote on last edited by
        #3

        but this way you can only add items to the list in the qml file like this:
        @MyObject
        {
        ...

        getInfo :[
        MyObject {...},
        MyObject {...},
        MyObject {...}
        ]
        }@

        i wan't to know if there is any way to add items to this list in a loop, because i have a lot of items to append into the list.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jonathanz
          wrote on last edited by
          #4

          I have a similar requirement: Append item to an initialized list property from QML application at run-time.

          This is what we can do right now, at the initialization stage:

          @MyElement {
          id: myElement

          listOfItems: [                   
              MyItem {text: "list val1"}, 
              MyItem {text: "list val2"}     
          

          ]
          }@

          This is what I would like to do additionally, at run time:

          @myElement.listOfItems.append(MyItem {text: "list val3" });@

          or:

          @MyItem {
          id: myItem3

           text: "list val3"
          

          }

          myElement.listOfItems.append(myItem3);
          @
          But neither "append()" works. This means that QDeclarativeListProperty is static once initialized in QML. This is a big limitation. Refer to the "Birthday Party" example. We have an initial list of guests. Later a new guest wants to join the fun. Will this be possible?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            moldovan.catalin
            wrote on last edited by
            #5

            This seems to be a very big limit to QDeclarativeListProperty<T*> properties. I couldn't find a way to dynamically append items to a QDeclarativeListProperty list from QML nor to dynamically create a new list and replace the old list with a new one. I can't believe that this was overlooked by Trolltech guys, it's a serious limitation if you want to do application logic with QML and Javascript, or am I missing a point here?

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jonathanz
              wrote on last edited by
              #6

              As an alternative solution, this is what I do when a dynamic list is needed: Derive a new class from QAbstractListModel and expose it to QML, or somehow make use of ListModel.

              When only a static list is needed, QDeclarativeListProperty appears to be a very good choice.

              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