artoon
artoon

Reputation: 739

QVariantList append

I tri to append a QVariant to another QVariant (which must be a QVariantList).

QList<int> listInt;
listInt.append(1);    
QVariant v;
v.setValue(listInt);
if(v.canConvert(QVariant::List))
{
    QVariant v1;
    v1.setValue(5);
    qvariant_cast<QVariantList>(v).append(v1);
}
objC.setProperty("_list", v);

But in the obC my _list contains only the integer 1. What is the problem?

Upvotes: 0

Views: 5105

Answers (1)

Steffen
Steffen

Reputation: 2948

The problem is, that the qvariant_cast<> returns by-value. So with your call to append() you are not changing the original object but only the copy. To make the changes stick, you will have to reset the QVariant, e.g.

    ...
    QVariantList toChange = qvariant_cast<QVariantList>(v);
    toChange.append(v1);
    v.setValue(toChange);
    ...

or using QList<int> instead of QVariantList

    ...
    QList<int> toChange = qvariant_cast< QList<int> >(v);
    toChange.append(47);
    v.setValue(toChange);
    ...

Upvotes: 1

Related Questions