简体   繁体   中英

How to convert QVariant to QJsonValue?

How to convert QVariant to QJsonValue ? I know QVariant provide the toJsonValue function, but it did not perform as expected.

For example:

qDebug()<<QVariant(1.0).toJsonValue();
qDebug()<<QVariant("test").toJsonValue();

Both return:

QJsonValue(null)
QJsonValue(null)

Expect output:

QJsonValue(double, 1)
QJsonValue(string, "test")

You might do the following:

QVariant dblVariant(1.0);
QVariant strVariant("test");

QJsonValue dblJs(dblVariant.toDouble());
QJsonValue strJs(strVariant.toString());

Your approach doesn't work because variant object should have user type QJsonValue , but it doesn't. Therefore it returns default constructed QJsonValue object.

You can use this static function too:

QJsonValue::fromVariant( myVariant )

Check this link for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM