4

The class contains this:

Q_PROPERTY(QList<double> switch1 READ switch1 WRITE setSwitch1 NOTIFY switch1Changed)

void setSwitch2(QList<double> arg)
{
    if (m_switch2 != arg)
    {
        m_switch2 = arg;
        emit switch2Changed(arg);
    }
}

The below works:

setSwitch2(QList<double>::fromVector(QVector<double>::fromStdVector(data->switch2)));

but now my datatype is QVariantList instead of QList<double>.

How should I replace QList with QVariant now?

This doesn't work:

setSwitch1(QVariantList::fromVector(QVector<QVariant>::fromStdVector(data->switch1)));
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

4 Answers4

2

Just use this constructor:

QVariant::QVariant(const QList & val)

Constructs a new variant with a list value, val.

I.e. when storing a QList as a QVariant, the template type of the QList has to be a type that is OK for QVariant. There is no other constructor or conversion method for this.

You should be writing this:

QVariant variant(
    QList<double>::fromVector(QVector<double>::fromStdVector(data->switch2))
);

setSwitchVariant(variant);
Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • @user462608: your code does not show the whole context, but this is the only way of the conversion. I am not sure what you mean by "what should I replace with what?". – László Papp Jan 06 '14 at 06:10
  • 1
    Due to QList being a class template it is just not possible to have a generic constructor (as proposed in the accepted answer) for QVariant from a QList. Constructing such QVariant can be achieved with QVariant::fromValue(). Although the result will be a QVariant containing a QList. – StefanQ Apr 06 '18 at 11:23
  • In addition to what StefanQ wrote, there is in fact no `QVariant(const QList &)` constructor (that's not even valid syntax since QList needs a type). There is one specifically for a `QList` but that is not the same thing. Qt cannot determine that the `double` in this case can be converted to QVariant. So the suggested code throws a compile error (at least with Qt 5.9+): "cannot convert argument 1 from 'QList' to 'QVariant::Type'". – Maxim Paperno Oct 01 '19 at 03:49
  • PS. Further details about this/related issue: https://stackoverflow.com/questions/58158034/qlist-in-qvariant-qvarianttype-returns-weird-type/58178040#58178040 – Maxim Paperno Oct 01 '19 at 04:52
1

If no need constructor:

QList<QUrl> params;
QVariant varParams;
varParams.setValue<QList<QUrl>>( params );
ZolotovPavel
  • 138
  • 6
1

I understand that it is not relevant for the author. But maybe someone will come in handy.

qRegisterMetaTypeStreamOperators<QList<double>>("Stuff");
QList<double> lst;
// convert
QVariant varLst = QVariant::fromValue(lst);
// back
lst = varLst.value<QList<double>>();
Serge
  • 41
  • 4
0

There is the QVariant constructor for lists: QVariant(const QList &list) and the typedef QVariantList. Both togheter in a nice template:

template <class T> static QVariant toVariant(const QList<T> &list)
{
    QVariantList variantList;
    variantList.reserve(list.size());
    for (const auto& v : list)
    {
        variantList.append(v);
    }
    return variantList;
}