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



  • 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.


  • Moderators

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



  • 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.


  • Moderators

    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" ;)



  • @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 -_-


  • Moderators

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.