<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Get all enum value names from C++ enum definition]]></title><description><![CDATA[<p dir="auto">Hi,<br />
System spec: Windows 10, Qt 6.9, mingw64 13.10, Qt Creator 17.0.1<br />
Classic C++ enum side declaration.</p>
<pre><code>#include &lt;QObject&gt;
#include &lt;QtQml/qqml.h&gt;

class BackgroundType : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    QML_UNCREATABLE("BackgroundType is not creatable - use enum values only")

public:
    explicit BackgroundType(QObject *parent = nullptr) : QObject(parent) {}

    enum Value {
        Basic,
        Upper,
        VIP
    };
    Q_ENUM(Value)
};
</code></pre>
<p dir="auto">This class is NOT compiled with qt_add_qml_module. So I used</p>
<pre><code>    qmlRegisterUncreatableType&lt;BackgroundType&gt;("InfModule", 1, 0,
                                          "BackgroundType",
                                          "BackgroundTypeis not creatable - use enum values only");
</code></pre>
<p dir="auto">Now, In QML side I would like to get list of all value names. For test purpose to scroll through them in combobox and check if all is fine.<br />
Q_PROPERTY with static QStringList, Q_INVOKABLE returning QStringList - none of them work.<br />
How to achive enum availabilty of uncreatable type with ability to print all enum values names?</p>
]]></description><link>https://forum.qt.io/topic/163180/get-all-enum-value-names-from-c-enum-definition</link><generator>RSS for Node</generator><lastBuildDate>Fri, 14 Aug 2026 06:55:20 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/163180.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 08 Sep 2025 11:49:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Get all enum value names from C++ enum definition on Tue, 09 Sep 2025 08:29:11 GMT]]></title><description><![CDATA[<p dir="auto">Partial solution which I found is to register this enum C++ class as <code>creatable</code>.<br />
Then in point where I need value names - is to call is JS section</p>
<pre><code>var obj = Qt.createQmlObject('import InfModule1.0; BackgroundType {}', parent); 
obj.names();
</code></pre>
]]></description><link>https://forum.qt.io/post/831355</link><guid isPermaLink="true">https://forum.qt.io/post/831355</guid><dc:creator><![CDATA[SebastianM]]></dc:creator><pubDate>Tue, 09 Sep 2025 08:29:11 GMT</pubDate></item><item><title><![CDATA[Reply to Get all enum value names from C++ enum definition on Tue, 21 Oct 2025 07:47:19 GMT]]></title><description><![CDATA[<p dir="auto">Other solution is ... to create attached  properties</p>
<pre><code>namespace MessageTypes {
    Q_NAMESPACE
    QML_ELEMENT

    enum class Type {
        None,
        SessionOpened,
        ConfigurationExternal,
        ConfigurationGet,
    };
    Q_ENUM_NS(Type)

    inline static const QMap&lt;Type, QString&gt;&amp; typeToString() {
        static const QMap&lt;Type, QString&gt; map = {
            {Type::None, "None"},
            {Type::SessionOpened, "SessionOpened"},
            {Type::ConfigurationExternal, "configuration/external"},
            {Type::ConfigurationGet, "configuration/get"},
        };
        return map;
    }

    inline static const QMap&lt;QString, Type&gt;&amp; stringToType() {
        static QMap&lt;QString, Type&gt; reverseMap = []() {
            QMap&lt;QString, Type&gt; map;
            for (auto it = MessageTypes::typeToString().cbegin();
                 it != MessageTypes::typeToString().cend(); ++it) {
                map.insert(it.value(), it.key());
            }
            return map;
        }();
        return reverseMap;
    }
}

// Attached object type - contains the utility methods
class MessageTypesAttached : public QObject
{
    Q_OBJECT
    QML_ANONYMOUS  // Not directly instantiable

public:
    explicit MessageTypesAttached(QObject *parent = nullptr)
        : QObject(parent)
    {}

    // Helper functions
    Q_INVOKABLE inline static QString toString(MessageTypes::Type type) {
        return MessageTypes::typeToString().value(type, QString());
    }

    Q_INVOKABLE inline static MessageTypes::Type fromString(const QString&amp; str, MessageTypes::Type defaultValue = MessageTypes::Type::None) {
        return MessageTypes::stringToType().value(str, defaultValue);
    }

    Q_INVOKABLE inline static QStringList getAllTypesName() {    
            return MessageTypes::typeToString().values();    
    }
}  
    

// Attaching type - provides access to the attached object
class MessageTypesUtils : public QObject
{
    Q_OBJECT
    QML_ELEMENT
    QML_UNCREATABLE("MessageTypesUtils is only for attached properties")
    QML_ATTACHED(MessageTypesAttached)

public:
    explicit MessageTypesUtils(QObject *parent = nullptr)
        : QObject(parent)
    {}

    // Required static method for attached properties
    static MessageTypesAttached *qmlAttachedProperties(QObject *object)
    {
        return new MessageTypesAttached(object);
    }
};
</code></pre>
]]></description><link>https://forum.qt.io/post/833006</link><guid isPermaLink="true">https://forum.qt.io/post/833006</guid><dc:creator><![CDATA[SebastianM]]></dc:creator><pubDate>Tue, 21 Oct 2025 07:47:19 GMT</pubDate></item><item><title><![CDATA[Reply to Get all enum value names from C++ enum definition on Tue, 09 Sep 2025 08:29:11 GMT]]></title><description><![CDATA[<p dir="auto">Partial solution which I found is to register this enum C++ class as <code>creatable</code>.<br />
Then in point where I need value names - is to call is JS section</p>
<pre><code>var obj = Qt.createQmlObject('import InfModule1.0; BackgroundType {}', parent); 
obj.names();
</code></pre>
]]></description><link>https://forum.qt.io/post/831355</link><guid isPermaLink="true">https://forum.qt.io/post/831355</guid><dc:creator><![CDATA[SebastianM]]></dc:creator><pubDate>Tue, 09 Sep 2025 08:29:11 GMT</pubDate></item><item><title><![CDATA[Reply to Get all enum value names from C++ enum definition on Mon, 08 Sep 2025 17:42:15 GMT]]></title><description><![CDATA[<p dir="auto">To be clear - I would like to call from QML side  <code>BackgroundType.getValueNames();</code> and receive <code>QStringList("Basic", "Upper", "Vip")</code>.<br />
Or my own QStrings mapping - like <code>QStringList("Basic Background", "Upper Level Background", "Vip Ultimate  Background")</code>.</p>
<p dir="auto">PS:</p>
<pre><code>    Q_INVOKABLE QStringList fun() { return {"Basic"), "Upper", "VIP"}; }
</code></pre>
<p dir="auto">it doesn't matter if it's registered with <code>qmlRegisterType</code> or <code>qmlRegisterUncreatableType</code> or if I remove <code>QML_UNCREATABLE</code> from dla  in results - QML engine warns with<br />
<code>TypeError: Property 'names' of object InfModule/BackgroundType is not a function</code></p>
]]></description><link>https://forum.qt.io/post/831323</link><guid isPermaLink="true">https://forum.qt.io/post/831323</guid><dc:creator><![CDATA[SebastianM]]></dc:creator><pubDate>Mon, 08 Sep 2025 17:42:15 GMT</pubDate></item><item><title><![CDATA[Reply to Get all enum value names from C++ enum definition on Mon, 08 Sep 2025 14:42:03 GMT]]></title><description><![CDATA[<p dir="auto">I don't know if there is a simpler way that is already built in, but you could look into using <code>QMetaEnum</code> on the C++ side and expose it to QML.</p>
]]></description><link>https://forum.qt.io/post/831316</link><guid isPermaLink="true">https://forum.qt.io/post/831316</guid><dc:creator><![CDATA[Bob64]]></dc:creator><pubDate>Mon, 08 Sep 2025 14:42:03 GMT</pubDate></item></channel></rss>