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. [Solved] Compilation error when using a flagable enumeration in a Q_PROPERTY
QtWS25 Last Chance

[Solved] Compilation error when using a flagable enumeration in a Q_PROPERTY

Scheduled Pinned Locked Moved General and Desktop
qt 5.5.0qflagqpropertymoc
6 Posts 3 Posters 4.3k 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.
  • K Offline
    K Offline
    kylek
    wrote on last edited by kylek
    #1

    I want to create a Q_PROPERTY with my flaggable enumeration.
    The code is the following :

    class Car : public QObject
    {
        Q_OBJECT
        Q_PROPERTY (CarOptions options READ options WRITE setOptions NOTIFY optionsChanged)
    public:
        enum CarOptions
        {
            CO_None = 0x0,
            CO_AirBag = 0x1,
            CO_NavigationSystem = 0x10,
            CO_MusicPlayer = 0x100
        };
    
        Car(QObject *parent = 0);
    
        CarOptions options() const;
        void setOptions(CarOptions options);
    
    Q_SIGNALS:
        void optionsChanged();
    
    private:
        CarOptions _options;
    
    public:
        Q_FLAG(CarOptions)
    };
    

    But when I compile I got the following error :

    debug\moc_car.cpp: In static member function 'static void Car::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)':
    debug\moc_car.cpp:119:66: error: conversion from 'QFlag' to 'Car::CarOptions' is ambiguous
         case 0: _t->setOptions(QFlag(*reinterpret_cast<int*>(_v))); break;
                                                                  ^
    debug\moc_car.cpp:119:66: note: candidates are:
    In file included from C:\Qt\Qt5.5.0\5.5\mingw492_32\include/QtCore/qglobal.h:1102:0,
                 from C:\Qt\Qt5.5.0\5.5\mingw492_32\include/QtCore/qnamespace.h:37,
                 from C:\Qt\Qt5.5.0\5.5\mingw492_32\include/QtCore/qobjectdefs.h:41,
                 from C:\Qt\Qt5.5.0\5.5\mingw492_32\include\QtCore/qobject.h:40,
                 from C:\Qt\Qt5.5.0\5.5\mingw492_32\include\QtCore/QObject:1,
                 from debug\../../test_flag_enum/car.h:4,
                 from debug\moc_car.cpp:9:
    C:\Qt\Qt5.5.0\5.5\mingw492_32\include/QtCore/qflags.h:66:29: note: QFlag::operator uint() const <near match>
     Q_DECL_CONSTEXPR inline operator uint() const Q_DECL_NOTHROW { return uint(i); }
                             ^
    C:\Qt\Qt5.5.0\5.5\mingw492_32\include/QtCore/qflags.h:66:29: note:   no known conversion from 'uint {aka unsigned int}' to 'Car::CarOptions'
    C:\Qt\Qt5.5.0\5.5\mingw492_32\include/QtCore/qflags.h:53:29: note: QFlag::operator int() const <near match>
     Q_DECL_CONSTEXPR inline operator int() const Q_DECL_NOTHROW { return i; }
                             ^
    C:\Qt\Qt5.5.0\5.5\mingw492_32\include/QtCore/qflags.h:53:29: note:   no known conversion from 'int' to 'Car::CarOptions'
    In file included from debug\moc_car.cpp:9:0:
    debug\../../test_flag_enum/car.h:21:14: note: initializing argument 1 of 'void Car::setOptions(Car::CarOptions)'
         void setOptions(CarOptions options);
    

    Am I missing something or is this a bug ?
    Thanks.

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      case 0: _t->setOptions(QFlag(*reinterpret_cast<int*>(_v))); break;
      You should convert to CarOptions not to QFlags

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kylek
        wrote on last edited by
        #3

        Thanks for the answer,

        The code you quote is auto generate by MOC (it's in the file 'moc_car.cpp'), can't do much here.

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

          You're declaring your enum wrong. It should be CarOption (without the s) and then you should use Q_DECLARE_FLAGS to define the CarOptions type i.e.

          ...
          public:
              enum CarOption
              {
                  CO_None = 0x0,
                  CO_AirBag = 0x1,
                  CO_NavigationSystem = 0x10,
                  CO_MusicPlayer = 0x100
              };
              Q_DECLARE_FLAGS(CarOptions, CarOption)
              Q_FLAG(CarOptions)
          ...
          

          Btw. Not really an issue here, but flags usually use consecutive bits i.e. 0x01, 0x02, 0x04, 0x08, 0x10 and so on. You're wasting "enum space" ;)

          K 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            You're declaring your enum wrong. It should be CarOption (without the s) and then you should use Q_DECLARE_FLAGS to define the CarOptions type i.e.

            ...
            public:
                enum CarOption
                {
                    CO_None = 0x0,
                    CO_AirBag = 0x1,
                    CO_NavigationSystem = 0x10,
                    CO_MusicPlayer = 0x100
                };
                Q_DECLARE_FLAGS(CarOptions, CarOption)
                Q_FLAG(CarOptions)
            ...
            

            Btw. Not really an issue here, but flags usually use consecutive bits i.e. 0x01, 0x02, 0x04, 0x08, 0x10 and so on. You're wasting "enum space" ;)

            K Offline
            K Offline
            kylek
            wrote on last edited by
            #5

            @Chris-Kawa Tested it successfully, thanks for the info.
            As for the enum, i don't know how I could mixed hexadecimal and binary format -_-

            1 Reply Last reply
            0
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You don't have to mix anything.
              What you use in your enum are hexadecimal values not binary.
              That means (left side hex, right side binary): 0x0 == 0, 0x1 == 1, 0x10 == 10000, 0x100 == 100000000!
              I guess what you actually want is what Chris Kawa said: 0x01 == 1, 0x02 == 10, 0x04 == 100, 0x08 == 1000, 0x10 == 10000

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

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