Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVariant and Enums: different behavior in Qt 5.7 and Qt 5.6.1

QVariant and Enums: different behavior in Qt 5.7 and Qt 5.6.1

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tga2450
    wrote on last edited by
    #1

    Hi all,

    I am trying to use QVariant with enum types. My issue is that I have different behaviors when building with Qt 5.7 (run on computer) and building with Qt 5.6.1 (run on embedded target).

    Here is an example code :

    #include <QCoreApplication>
    #include <QMetaType>
    #include <QMetaEnum>
    #include <QVariant>
    #include <QDebug>
    
    enum Test
    {
        Test1,
        Test2,
        Test3
    };
    Q_DECLARE_METATYPE(Test)
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        Test t = Test2;
        QVariant v(t);
    
        qDebug()<<"Can convert: "+QString("%1").arg(v.canConvert<Test>());
        Test t2 = v.value<Test>();
        qDebug()<<"Value of t2: "+QString("%1").arg(t2);
    
    
        return a.exec();
    }
    

    Built under Qt 5.7 and running on computer it returns

    Can convert: 1
    Value of t2: 1
    

    Built under Qt 5.6.1 and running on my embedded target it returns

    Can convert: 0
    Value of t2: 0
    

    I cannot figure out why QVariant::canConvert always return false with all enums on my target.

    Thank you for your help.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      can you try using Q_ENUM?

      globalenums.h

      class GlobalEnums{
      Q_GADGET
      Q_DISABLE_COPY(GlobalEnums)
      GlobalEnums() = delete;
      ~GlobalEnums() = delete;
      public:
      enum Test
      {
          Test1,
          Test2,
          Test3
      };
      Q_ENUM(Test)
      };
      

      main.cpp

      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          GlobalEnums::Test t = GlobalEnums::Test2;
          QVariant v(t);
      
          qDebug()<<"Can convert: " << v.canConvert<GlobalEnums::Test>();
          GlobalEnums::Test t2 = v.value<GlobalEnums::Test>();
          qDebug()<<"Value of t2: "<< t2;
      
      
          return a.exec();
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • T Offline
        T Offline
        tga2450
        wrote on last edited by
        #3

        Thank you VRonin for your help.

        I tried your suggestion but it didn't change the results on my target. Conversion from QVariant to Test enum is still refused.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Could be related: QTBUG-53384.
          Can you check with Qt 5.6.2?

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved