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

based enum with negative value not handled correctly as Q_PROPERTY

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.2.1
    • Build tools: moc
    • None

    Description

      when using a based enum as a property, negative values are not converted properly.
      when the base type is a type smaller than int (ex. int8 or int16)

      for example

      enum class EnumType : std::int8_t {
        Undefined = -1,
        SomeValue,
        SomeOtherValue,
      }
      

      returning EnumType::Undefined to the property, it will be converted to 255 in qml.
      because the value will be transferred via void* and reinterpret_cast.

      a possible fix would be to change the moc compiler to generate a static_cast to int before transfering the value into the QVariant:

      // some_moc.cpp
      // in void Type::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int  _id, void **_a)
      // ...
      else if (_c == QMetaObject::ReadProperty) {
              Q_ASSERT(staticMetaObject.cast(_o));
              auto *_t = static_cast<Type*>(_o);
              (void)_t;
              void *_v = _a[0];
              switch (_id) {
              case 0: *reinterpret_cast< QObject**>(_v) = _t->GetSomeObj(); break;
              case 1: *reinterpret_cast< QObject**>(_v) = _t->GetOtherObj(); break;
      // case 2: *reinterpret_cast< Namespace::EnumType*>(_v) = _t->GetEnumValue(); break;
              case 2: *reinterpret_cast< int*>(_v) = static_cast<int>(_t->GetEnumValue()); break;
              case 3: *reinterpret_cast< bool*>(_v) = _t->GetSomeBool(); break;
              case 4: *reinterpret_cast< bool*>(_v) = _t->GetSomeBool(); break;
      // same here
              case 5: *reinterpret_cast< Namespace::EnumType2*>(_v) = _t->GetEnumValue2(); break;
              default: break;
              }
          } 
      // ...
      

      Attachments

        For Gerrit Dashboard: QTBUG-98869
        # Subject Branch Project Status CR V

        Activity

          People

            fabiankosmale Fabian Kosmale
            menzels2 Stefan Menzel
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There is 1 open Gerrit change