[Interest] Strange QvariantList/QVariant problem

André Somers andre at familiesomers.nl
Mon Nov 25 17:05:24 CET 2013


Guido Seifert schreef op 25-11-2013 17:00:
> Hi,
> I have here a strange effect and I wonder if this is the correct behaviour.
>
> I have a QVariantList x.
> I add a few values
> x << <int>
> x << <QString>
> x << <bool>
>
> Now I create a second QVariantList y and add a few values:
> y << 1.5
> y << 3.5
>
> And add the list y to x:
> x << y
>
> Now I would expect that x has:
> One int, one QString, one bool and one QVariantList with two doubles.
> Actually I get one int, one QString, one bool, and two doubles.
>
> Instead of four my  x list has five values. The second QVariantList gets 'flattened'.
>
> Is this to be expected?
Yes, it is.

Note that QVariantList is simply a QList<QVariant>. You adding a 
QVariantList to another QVariantList perfectly fits the function 
signature of QList::append(const QList<T>& value). In order to make it 
resolve to QList::append(const T& value), your QVariantList would first 
have to be implicitly casted to a QVariant to fit the signature. AFAIK, 
C++ will always prefer not to do an implicit cast if that isn't needed 
to resolve the function call.

André




More information about the Interest mailing list