c++qtqtcoreqlistqvariant

Converting QList to QVariant


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)));

Solution

  • 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);