4

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")
  )
)
zopieux
  • 2,854
  • 2
  • 25
  • 32
Victor Aurélio
  • 2,355
  • 2
  • 24
  • 48

1 Answers1

7

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());
Victor Aurélio
  • 2,355
  • 2
  • 24
  • 48
  • 6
    You can also use the [`push_back`](http://doc.qt.io/qt-5/qlist.html#push_back) alias that has no special overload for `QList`: `l.push_back(other_list);` This relies on a "API design hack" from Qt but at least it does not need the size. – zopieux Aug 25 '16 at 16:24