Home > database >  Convert custom type to QVariant
Convert custom type to QVariant

Time:12-02

I have my custom type:

enum class MyType : int {
     TYPENAME1 = 0,
     TYPENAME2 = 1,
     TYPENAME3 = 2
};

I need to convert MyType to QVariant. I tried qDebug() << QVariant::fromValue(value) but I received " " instead of property value.

CodePudding user response:

For QVariant to store a custom type, you need the type to be registered with the qt meta object system.

  1. Q_ENUM or Q_ENUM_NS in the header of the type
  2. qRegisterMetaType<MyType>() called sometime before you try to use the type with QVariant (usually setup somewhere that is called when your app starts)
  • Related