Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-141609

QVariant::convert() does not do round-trip conversion between QMetaType::(U)Char and QMetaType::QString

XMLWordPrintable

      Code

      #include <QVariant>
      #include <QMetaType>
      #include <QDebug>
      
      static void checkAndConvert(QVariant &var, QMetaType toType)
      {
          qDebug("");
          qDebug() << "Can convert to" << toType << "?" << var.canConvert(toType);
          qDebug() << "Conversion successful?" << var.convert(toType);
          qDebug() << "New value:" << var;
      }
      
      int main()
      {
          QVariant var = QVariant::fromValue(quint8(55)); // 55 == 0x37 == ASCII representation for '7'
          QMetaType origType = var.metaType();
          qDebug() << "Original value:" << var;
      
          checkAndConvert(var, QMetaType(QMetaType::QString));
          checkAndConvert(var, origType);
      }
      

       

      Outcomes

      Original value: QVariant(uchar, 55)
      
      Can convert to QMetaType(QString) ? true
      Conversion successful? true
      New value: QVariant(QString, "7")
      
      Can convert to QMetaType(uchar) ? true
      Conversion successful? true
      New value: QVariant(uchar, 7)
      

       

      Questions

      1. Do we want to support conversion from QMetaType::(U)Char to QMetaType::QString? (Note: In Qt 5.15, QVariant::fromValue(quint8(55)).canConvert(QVariant::String) == false)
      2. If so, shouldn't the conversion behave in such a way that the final value equals the original value in the test above? (e.g. the first conversion to QString yields "55" instead of "7")

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            thiago Thiago Macieira
            skoh-qt Sze Howe Koh
            Vladimir Minenko Vladimir Minenko
            Alex Blasche Alex Blasche
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes