Nominate our 2022 Qt Champions!

Returning and Enum to QML using a Q_INVOKABLE method

  • Is it possible to pass enums from a C++ Q_OBJECT class using Q_INVOKABLE to a QML implementation. There is an error that I am getting with it:

    @main.qml:185: Error: Unknown method return type: FaultWarningItem::ItemType@

    This happens when I attempt to return a enum as part of the Q_INVOKABLE:
    @Q_INVOKABLE FaultWarningItem::ItemType getItemErrorType(void);@

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you register your enum ?

    See this "thread": for an example

  • there is some limits on using enums from qml.
    you can't use another classes enums for Q_INVOKABLE functions.
    your enums only can be used for primary classes functions.

  • Indeed this is the case there is a problem with this. I was hoping to use this to explicitly do apple-to-apple comparison: (1) make the *.qml file easily readable by replacing hard coded numbers, (2) force errors when interpreting *.qml files when enum changes, and (3) make the code easily searchable to enum item.

    However, there is a limitation here that I will not be able to get around.

    My code is the following:

    #include <QObject>

    // Fault Warning Container Class
    class FaultWarningItem

    enum ItemType
        TypeUnspecified = -1,
        TypeNoError = 0,




    #include <QObject>
    #include "FaultWarningItem.h"

    class FaultWarningModel: public QObject {
    FaultWarningModel(QObject *parent=0);

    Q_INVOKABLE FaultWarningItem::ItemType getItemErrorType(void) const;
    Q_INVOKABLE int getItemErrorTypeAsInt(void) const;
    Q_INVOKABLE void setNextFaultWarning();
    int getCount() const;

    int m_count;
    FaultWarningItem::ItemType m_FaultWarningItem;
    void countChanged(int);


    @#include "FaultWarningModel.h"
    #include <QQml.h>
    #include <QDebug>

    FaultWarningModel::FaultWarningModel(QObject *parent) :

    m_FaultWarningItem = FaultWarningItem::TypeUnspecified;
    int statusType = qmlRegisterUncreatableType<FaultWarningItem>
                         "ItemType",                                          // const char * uri,
                         1,                                                   // int versionMajor,
                         0,                                                   // int versionMinor,
                         "ItemType",                                          // const char * qmlName,
                         "ItemType type cannot be created." // const QString & message
    qDebug() << "statusType: " << statusType;


    FaultWarningItem::ItemType FaultWarningModel::getItemErrorType(void) const
    return m_FaultWarningItem;

    int FaultWarningModel::getItemErrorTypeAsInt() const
    int iValue = (int) m_FaultWarningItem;
    return iValue;


    @import QtQuick 2.0

    // The enum types need to be imported before they can be used.
    import ItemType 1.0

    Rectangle {
    width: 200
    height: 300

    property int currentFaultWarningType: ItemType.TypeUnspecified
    Column {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter
        spacing: 10
        // Toggles the printer type
        Rectangle {
            id: buttonFaultWaring
            width: 100
            height: 50
            color: "gainsboro"
            border.color: "black"
            border.width: 1
            Text {
                text: "TOGGLE Fault/Warning Type"
                width: parent.width
                anchors.centerIn: parent
                wrapMode: Text.WordWrap
                horizontalAlignment: Text.AlignHCenter
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    currentFaultWarningType = FaultWarningModel.getItemErrorTypeAsInt();
        // Shows the type of the printer
        Rectangle {
            id: statusFaultWarning
            width: 100
            height: 50
            Text {
               text: {
                   // Causes - "Error: Unknown method return type: FaultWarningItem::ItemType"
                   //currentFaultWarningType = FaultWarningModel.getItemErrorType()
                   // Causes - "Error: Unknown method return type: FaultWarningItem::ItemType"
                   /*if (ItemType.TypeUnspecified === FaultWarningModel.getItemErrorType())
                       "Yay, it works"
                   currentFaultWarningType = FaultWarningModel.getItemErrorTypeAsInt()
                   if (ItemType.TypeUnspecified === currentFaultWarningType)
                       "TypeUnspecified (-1)"
                   else if (ItemType.TypeNoError === currentFaultWarningType)
                       "TypeNoError (0)"
                   else if (ItemType.TypeWarning === currentFaultWarningType)
                       "TypeWarning (1)"
                   else if (ItemType.TypeFault === currentFaultWarningType)
                       "TypeFault (2)"
               anchors.centerIn: parent


    Is there any future plans in Qt to have this changed so that it can be used without getting "Error: Unknown method return type"?

  • see,73812
    it seems that something changed at 5.3.0
    try it.

  • It is already old topic but I would like to reply for documentation purposes for further questions,
    If you like to use an enum on a Q_INVOKABLE function, then this enum should be registered.

Log in to reply