Search code examples
listqt5.5qvariant

QVariantList.append() merges list instead of nesting


When I try to nest a QVariantList inside another QVariantList, the result is the flat merge of the two lists, instead of a sub-list.

Demo code:

QVariantList container;

QVariantList nested() << "bar" << "baz";

container.append("foo");  // or container << "foo";
container.append(nested); // or container << nested; 

What I obtain (indentations are mine):

QVariant(QVariantList,
  QVariant(QString, "foo"),
  QVariant(QString, "bar"),
  QVariant(QString, "baz"),
)

What I would expect:

QVariant(QVariantList,
  QVariant(QString, "foo"),
  QVariant(QVariantList, 
    QVariant(QString, "bar"),
    QVariant(QString, "baz")
  )
)

Solution

  • Found solution by myself.

    This is due the QList's append overload:

    void QList::append(const QList & value)

    This is an overloaded function.

    Appends the items of the value list to this list.

    The solution is append item using insert method:

    QVariantList l;
    l.insert(l.size(), QVariant());