I am following this example in order to understand how to work with a QList as a ListModel for QML.
I would like to modify the list from C++. Therefore I put the list into a new QObject to be able to use a timer. In the timer callback I modify an existing element's color and append a new element to the list.
void MyObject::setList(QList<QObject *>* l)
{
list = l;
QTimer* timer = new QTimer;
connect(timer, SIGNAL(timeout()), this, SLOT(addElement()));
timer->start(2000);
}
void MyObject::addElement()
{
list->append(new DataObject("Item 1", "red"));
((DataObject *) list->at(0))->setColor("blue");
}
The color change is shown in QML, however the length of the list in QMLs ListView does not change. What am I missing? What steps are neccessary to make QML aware of the list's size change?
I understand the color and name properties are registered through the Q_PROPERTY makro
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
How do I translate this to the list's length?