> ## Content Index
> Fetch the complete content index at: https://www.smallstepsystems.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Passing Enum Properties between C++ and QML
- URL: https://www.smallstepsystems.com/passing-enum-properties-between-c-and-qml/
- Published: 2017-09-15T16:24:33.000Z
- Updated: 2026-01-25T15:51:17.000Z
- Author: Burkhard Stubert

We have defined a Qt property `warningLevel` in the C++ class `MainModel`:

Q\_PROPERTY(WarningLevel::Enum warningLevel READ warningLevel WRITE setWarningLevel NOTIFY warningLevelChanged)

We want to use this property in QML. For example, we want to colour a rectangle according to the `warningLevel`:

import com.embeddeduse.models 1.0 // ... property MainModel mainModel : MainModel {} Rectangle { color: toColor(mainModel.warningLevel) // ... } function toColor(level) { switch (level) { case WarningLevel.Error: return "red" case WarningLevel.Warning: return "orange" case WarningLevel.Info: return "green" case WarningLevel.Debug: return "purple" default: return "magenta" } }

Note how we access the C++ property `mainModel.warningLevel` from QML to set the `color` of the rectangle and how we use symbolic enum constants like `WarningLevel.Info` in the function `toColor()`.

It is similarly easy to use a list of the symbolic enum constants as the model of a `Repeater` and to assign the warning level by the user to the property `mainModel.warningLevel` in the `onReleased` handler of a `MouseArea`.

Repeater { model: \[WarningLevel.Error, WarningLevel.Warning, WarningLevel.Info, WarningLevel.Debug\] Rectangle { color: toColor(modelData) // ... MouseArea { anchors.fill: parent onReleased: mainModel.warningLevel = modelData } } }

I'll show you in the rest of this post how to write your C++ code so that you can use a C++ property of enum type easily in QML.

Our enum represents the four levels of warnings that we want to show in an application. The QML code `toColor()` maps each warning level to a colour. A red message is an error, an orange message a warning and so on. Here is the plain enum, which I declared in a separate file *qmlenums.h*.

enum class Enum : quint8 { Error, Warning, Info, Debug };

We must register the enum with Qt's meta object system. This allows us to use the Enum as a custom-type of a `QVariant`, to pass it between QML and Qt, to use it in synchronous signal-slot connections, and to print the symbolic enums (instead of a magic number) with `qDebug()`. Our first stop for registrations always is the macro `Q_DECLARE_METATYPE`. The [documenation of this macro](http://doc.qt.io/qt-5/qmetatype.html?ref=smallstepsystems.com#Q%5FDECLARE%5FMETATYPE) gives us a good hint.

- ...
- Enumerations registered with Q\_ENUM or Q\_FLAG

The [documentation of Q\_ENUM](http://doc.qt.io/qt-5/qobject.html?ref=smallstepsystems.com#Q%5FENUM) gives us the necessary details.

> This macro registers an enum type with the meta-object system. It must be placed after the enum declaration in a class that has the Q\_OBJECT or the Q\_GADGET macro.

This tells us to do two things:

- Place the macro `Q_ENUM(Enum)` after the enum declaration.
- Create a class `WarningLevel` derived from `QObject` containig the `Q_OBJECT` macro.

The code for the enum looks as follows: class WarningLevel : public QObject { Q\_OBJECT public: enum class Enum : quint8 { Error, Warning, Info, Debug }; Q\_ENUM(Enum) };

The above class is the template for other enums that we want to expose to QML. If, for example, we wanted to use an enum `OperatingMode` in QML, we would write another small class. We could access the enum constants in QML as `OperatingMode.Field`. If we added the enum `OperatingMode` to the existing class `WarningLevel`, we would have to access the enum constants in QML as `WarningLevel.Field`. This is most likely not what we want.

There are two more steps to make the enum known in QML. First, we must register the class `WarningLevel` with the QML engine. This is done with the following command in the file *main.cpp*.

qmlRegisterUncreatableType<WarningLevel>("com.embeddeduse.models", 1, 0, "WarningLevel", "Cannot create WarningLevel in QML");

This command registers the C++ class `WarningLevel` under the QML name `WarningLevel` in version "1.0" of the QML module "com.embeddeduse.models". We use the [function qmlRegisterUncreatableType](http://doc.qt.io/qt-5/qqmlengine.html?ref=smallstepsystems.com#qmlRegisterUncreatableType) instead of `qmlRegisterType`, because we don't need to call the constructor of `WarningLevel` in QML and because the documentation says so.

> This is useful where the type is only intended for providing attached properties or enum values.

When we want to access the enum constants `WarningLevel` in a QML file, we must add the following `import` command at the beginning of the QML file. This is the second step.

import com.embeddeduse.models 1.0

You can find the complete source code on [github](https://github.com/bstubert/embeddeduse/tree/master/BlogPosts/EnumsQmlCpp?ref=smallstepsystems.com).