June 30, 2011

goli goli
Lab Rat
76 posts

QDeclarativeListProperty add item from qml application

 

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

  1. var i;
  2. for(i = 0 ; i < 100 ; ++i)
  3. {
  4.       listOfItems.append(MyItem {text:"list"+i})
  5. }

and listOfItems is the QDeclarativeListProperty list…
i don’t want to do that:
  1. listOfItems:
  2. [                    
  3.     MyItem{text:"list val1"},  
  4.     MyItem{text:"list val2"},      
  5.      ......    
  6. ]

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

5 replies

July 1, 2011

sigrid sigrid
Lab Rat
138 posts

In order for this to work, you need to implement the AppendFunction [doc.qt.nokia.com] of QDeclarativeListProperty. The example below shows how that can be done. It also implement ClearFunction [doc.qt.nokia.com] so that it cleans up correctly.

  1. #include <QtDeclarative>
  2.  
  3. class MyObject : public QObject
  4. {
  5.     Q_OBJECT
  6.         Q_PROPERTY(QDeclarativeListProperty<MyObject> getInfo READ getInfo CONSTANT)
  7. public:
  8.     MyObject()
  9.     {}
  10.  
  11.     ~MyObject()
  12.     {
  13.     }
  14.     QDeclarativeListProperty<MyObject> getInfo()
  15.     {
  16.         for (int i = 0; i < 10; ++i)
  17.         {
  18.             list << new MyObject();
  19.   }
  20.  
  21.         return QDeclarativeListProperty<MyObject>(this, 0, &MyObject::appendObject, 0, 0, &MyObject::clearObject);
  22.     }
  23.     static void appendObject(QDeclarativeListProperty<MyObject> *l, MyObject *obj)
  24.     {  
  25.         MyObject *object = qobject_cast<MyObject *>(l->object);
  26.         if (object)
  27.             object->list << obj;
  28.     }
  29.     static void clearObject(QDeclarativeListProperty<MyObject> *l)
  30.     {
  31.         qDebug("In clear");
  32.         MyObject *object = qobject_cast<MyObject *>(l->object);
  33.         if (object) {
  34.             foreach (MyObject *o, object->list)
  35.                 delete o;
  36.             object->list.clear();
  37.         }
  38.     }
  39.     QList<MyObject *> list;
  40. };

July 3, 2011

goli goli
Lab Rat
76 posts

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

  1. MyObject
  2. {
  3. ...
  4.  
  5. getInfo :[
  6. MyObject {...},
  7. MyObject {...},
  8. MyObject {...}
  9. ]
  10. }

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.

July 14, 2011

jonathanz jonathanz
Lab Rat
4 posts

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:

  1. MyElement {
  2.     id: myElement
  3.  
  4.     listOfItems: [                  
  5.         MyItem {text: "list val1"},
  6.         MyItem {text: "list val2"}    
  7.    ]
  8. }

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

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

or:

  1. MyItem {
  2.      id: myItem3
  3.  
  4.      text: "list val3"
  5. }
  6.  
  7. 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?

September 3, 2011

moldovan.catalin moldovan.cat..
Lab Rat
3 posts

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?

October 4, 2011

jonathanz jonathanz
Lab Rat
4 posts

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.

 
  ‹‹ Quad Splitter Windows, an implementation?      QSqlQuery and Umlauts ››

You must log in to post a reply. Not a member yet? Register here!