Convert QList to QVariant

The class contains the following:

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 following is shown:

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

but now my data type is QVariantListinstead QList<double>.

How can I replace QList QVariant now?

This does not work:

setSwitch1(QVariantList::fromVector(QVector<QVariant>::fromStdVector(data->switch1)));
+4
source share
3 answers

Just use this constructor:

QVariant :: QVariant (const QList and val)

Creates a new variant with a list value, val.

those. when saving a QList as a QVariant, the type of the QList template must be the type that is suitable for QVariant. There is no other constructor or conversion method for this.

You should write this:

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

setSwitchVariant(variant);
+2
source

If a needs constructor is not required:

QList<QUrl> params;
QVariant varParams;
varParams.setValue<QList<QUrl>>( params );
0
source

, . , , - .

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

All Articles