[Interest] QVariant conversion (QString->QChar)

Ch'Gans chgans at gna.org
Wed Jan 20 04:47:29 CET 2016


Hi there,

According to the documentation of "bool QVariant::canConvert(int
targetTypeId) const" [1] a QVariant holding a QString should be able
to convert to QChar as "QMetaType::QString converts automatically to
QMetaType::QChar"

but the following code:
    QVariant from = QVariant::fromValue<QString>(QString("a"));
    QVariant to = from;
    bool success = to.convert(QMetaType::QChar);
    qDebug() << from << success << to;
yields:
    QVariant(QString, "a") false QVariant(Invalid)

and this code:
    QVariant from = QVariant::fromValue<QString>(QString("1"));
    QVariant to = from;
    bool success = to.convert(QMetaType::Int);
    qDebug() << from << success << to;
yields:
    QVariant(QString, "1") true QVariant(int, 1)

Is the documentation correct or is it me missing something trivial?

Thanks,
Chris

[1] http://doc.qt.io/qt-5/qvariant.html#canConvert



More information about the Interest mailing list