Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

New in Qt 5.8: meta-object support for namespaces

Using namespaces in Qt for enumeration introspection


namespace MyNamespace
{
    Q_NAMESPACE

    // ...
}

namespace MyNamespace
{
    Q_NAMESPACE
    Q_CLASSINFO("author", "Uncle Scrooge")
    Q_CLASSINFO("revision", "3.14")
}

for (int i = 0; i < MyNamespace::staticMetaObject.classInfoCount(); ++i) {
    const auto &metaClassInfo = MyNamespace::staticMetaObject.classInfo(i);
    qDebug() << metaClassInfo.name() << metaClassInfo.value();
}

namespace MyNamespace
{
    Q_NAMESPACE

    enum class SomeEnum {
        Foo,
        Bar
    };
    Q_ENUM_NS(SomeEnum)
}

class ColoredObject : public QObject {
    Q_OBJECT
    Q_PROPERTY(Color color MEMBER m_color)
public:
    enum class Color {
        Red, Blue, Green
    };
    Q_ENUM(Color)

private:
    Color m_color;
};

qmlRegisterType<ColoredObject>(    // C++ datatype
    "com.kdab.components",         // import statement
    42, 0,                         // major and minor version of the import
    "ColorThing");                 // name in QML

import QtQuick 2.0
import com.kdab.components 42.0

Item {
    ColorThing {
        color: ColorThing.Red    // enum defined in C++
    }
}

namespace MyNamespace
{
    Q_NAMESPACE

    enum class SomeEnum {
        Foo,
        Bar
    };
    Q_ENUM_NS(SomeEnum)
}

qmlRegisterUncreatableMetaObject(MyNamespace::staticMetaObject, // static meta object
                                 "com.kdab.namespace",          // import statement
                                 3, 14,                         // major and minor version of the import
                                 "MyNamespace",                 // name in QML
                                 "Error: only enums");          // error in case someone tries to create a MyNamespace object

import QtQuick 2.0
import com.kdab.namespace 3.14

Item {
    Component.onCompleted: console.log(MyNamespace.Foo)
}

About KDAB


22 Comments

1 - Mar - 2017

Eric Lemanissier

2 - Mar - 2017

Giuseppe D'Angelo

5 - Mar - 2017

Tim

11 - Mar - 2017

Giuseppe D'Angelo

6 - Mar - 2017

Alan Sambol

11 - Mar - 2017

Giuseppe D'Angelo

6 - Mar - 2017

Ceyk

11 - Mar - 2017

Giuseppe D'Angelo

17 - Jul - 2017

Karl Hansen

// SysEnums.h
namespace SysEnums
{
   Q_NAMESPACE
   namespace MyEnum1
   {
       Q_NAMESPACE
       enum class MyEnum1
       {
           V1,
           V2,
       }
       Q_ENUM_NS(MyEnum1)
   } // ns MyEnum1
   namespace MyEnum2
   {
       Q_NAMESPACE
       enum class MyEnum2
       {
           V1,
           V2,
           V3,
       }
       Q_ENUM_NS(MyEnum2)
   } // ns MyEnum2
} // ns SysEnums

#include "enums.h"

// somewhere in startup code

qmlRegisterUncreatableMetaObject(Enums::staticMetaObject, // static meta object
                                 "com.medtronic.enums",          // import statement
                                 1,0,                         // major and minor version of the import
                                 "Enums",                 // name in QML
                                 "Error: only enums");  
qmlRegisterUncreatableMetaObject(Enums::MyEnum1::staticMetaObject, // static meta object
                                 "com.medtronic.enums.MyEnum1",          // import statement
                                 1,0,                         // major and minor version of the import
                                 "MyEnum1",                 // name in QML
                                 "Error: only enums");  
qmlRegisterUncreatableMetaObject(Enums::MyEnum2::staticMetaObject, // static meta object
                                 "com.medtronic.enums.MyEnum2",          // import statement
                                 1,0,                         // major and minor version of the import
                                 "MyEnum2",                 // name in QML
                                 "Error: only enums");  

17 - Jul - 2017

Karl Hansen

22 - Aug - 2017

Matthias Wallnöfer

namespace MyNamespace
{
    Q_NAMESPACE

    enum class SomeEnum {
        Foo,
        Bar
    };
    Q_ENUM_NS(SomeEnum)

    class SomeQObject : public QObject
    {
         Q_OBJECT
         Q_PROPERTY(SomeEnum value MEMBER value)

         SomeEnum value;
    public:
         explicit SomeQObject() { }
    };
}

31 - Aug - 2017

Orient

23 - Jan - 2018

Karsten Sperling Opdal

23 - Jan - 2018

Giuseppe D'Angelo

3 - Feb - 2018

Bernando

5 - Feb - 2018

Giuseppe D'Angelo

5 - Feb - 2018

Bernando

19 - Feb - 2020

Ivan

19 - Feb - 2020

Giuseppe D'Angelo

21 - Feb - 2020

Ivan D

21 - Feb - 2020

Giuseppe D'Angelo

8 - Apr - 2022

matzze54

GiuseppeD'Angelo

Giuseppe D’Angelo

Senior Software Engineer

Learn Modern C++

Learn more