0

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?

Herr von Wurst
  • 2,571
  • 5
  • 32
  • 53

1 Answers1

1

A QList<QObject*> is the simplest but also the dumbest type of data model you could use. It has no means to signal for internal changes so the view can update itself.

You could force updates by exposing the list as a property that has the NOTIFY signal. However, that is very inefficient, and will force the recreation of every list view delegate each and every time. This can get ugly very fast as the number of elements grows.

You should consider implementing a proper QAbstractListModel with all its bells and whistles. Then changes in the list will be reflected on the qml side in the most efficient and adequate manner possible. Or perhaps use the generic model I've outlined here, it is quite flexible because it also allows population with declarative qml code in additions to using the functions. You can also define the actual object types in qml without the need to recompile the C++ stuff for each and every new type.

Community
  • 1
  • 1
dtech
  • 47,916
  • 17
  • 112
  • 190
  • And there I was thinking the simplest way would be the best way to start learning... Thank you! – Herr von Wurst Dec 17 '16 at 21:03
  • Simple is good as in "needless complexity is very bad", but "the simplest" tends to be also "very limited". – dtech Dec 17 '16 at 21:15
  • 1
    @Gege a custom list model is actually not that much more complicated than exposing a QList as a property. I'd say actually easier since you don't have to do two QObject based classes. WIth a custom model you data can be in any data structure you like, e..g a simple class or struct, an array of tuples, and so on However, since you said you are learning, I would still recommend to fix the original problem first, by emitting a notify signal when property exposing the list changes. You will need this technique in other occasions – Kevin Krammer Dec 18 '16 at 12:13