49

I am trying to use the Qt reflection for converting enum to QString.

Here is the part of code:

class ModelApple
{
    Q_GADGET
    Q_ENUMS(AppleType)
public:
    enum AppleType {
      Big,
      Small
    }
}

And here is i trying to do:

convertEnumToQString(ModelApple::Big)

Return "Big"

Is this possible? If you have any idea about convertEnumToQString, please share it

6 Answers 6

80

You need to use Q_ENUM macro, which registers an enum type with the meta-object system.

enum AppleType {
  Big,
  Small
};
Q_ENUM(AppleType)

And now you can use the QMetaEnum class to access meta-data about an enumerator.

QMetaEnum metaEnum = QMetaEnum::fromType<ModelApple::AppleType>();
qDebug() << metaEnum.valueToKey(ModelApple::Big);

Here is a generic template for such utility:

template<typename QEnum>
std::string QtEnumToString (const QEnum value)
{
  return std::string(QMetaEnum::fromType<QEnum>().valueToKey(value));
}
4
  • 8
    QMetaEnum::fromType<T> is available from Qt 5, it doesn't exist in Qt 4. You should add this remark. By the way, I don't recommend to use QMetaEnum::key since it take index as parameter, how come is he declared enum AppleType { Big = 2, Small}
    – Danh
    Dec 15, 2015 at 5:58
  • 4
    From the docs, it looks like this is actually only available from Qt 5.5 thus I still need to use my method in the answer I gave (was about to update my code but have to use Qt 5.4 for now).
    – CJCombrink
    Dec 15, 2015 at 6:27
  • 'keyToValue()' can do the other way around: get the enum value from the string!
    – jaques-sam
    Dec 11, 2018 at 12:58
  • I got this: undefined reference to MyClass::staticMetaObject . I copied the example from official page of Q_ENUM .
    – SuB
    Dec 2, 2020 at 8:01
44

Much more elegant way found (Qt 5.9), just one single line, with the help of mighty QVariant.

turns enum into string:

QString theBig = QVariant::fromValue(ModelApple::Big).toString();

Perhaps you don't need QMetaEnum anymore.

Sample code here:

ModelApple (no need to claim Q_DECLARE_METATYE)

class ModelApple : public QObject
{
    Q_OBJECT
public:
    enum AppleType {
      Big,
      Small
    };
    Q_ENUM(AppleType)
    explicit ModelApple(QObject *parent = nullptr);
};

And I create a widget application, calling QVaraint function there :

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <modelapple.h>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString s = QVariant::fromValue(ModelApple::Big).toString();
    qDebug() << s;

}

MainWindow::~MainWindow()
{
    delete ui;
}

You can see that i try to output the string on console , which really did: enter image description here

And sorry for reverse casting , i tried successfully in some project , but some how this time i met compiling error. So i decide to remove it from my answer.

3
  • 1
    Thanks for answering, looks like the method needs to add Q_DECLARE_METATYPE(ModelApple::AppleType), and even added make it compiled successful, the result is an empty string. Could you please post the example code for this?
    – JustWe
    Nov 20, 2017 at 0:51
  • For anyone struggling with this solution: you need Q_OBJECT and Q_ENUM. Also your enum has to be public.
    – Cheesi
    Feb 20, 2019 at 11:54
  • 1
    @Cheesi you don't need Q_OBJECT: Q_GADGET is sufficient for this (unless you are really making a QObject).
    – Ruslan
    Aug 19, 2019 at 12:21
9

The following should get you going:

QString convertEnumToQString(ModelApple::AppleType type) {
    const QMetaObject metaObject = ModelApple::staticMetaObject;
    int enumIndex = metaObject.indexOfEnumerator("AppleType");
    if(enumIndex == -1) {
        /* The enum does not contain the specified enum */
        return "";
    }
    QMetaEnum en = metaObject.enumerator(enumIndex);
    return QString(en.valueToKey(type));
}
6
  • Why don't you join the declaration of enumString and enumString = en.valueToKey(type); it will call only 1 constructor instead of 1 constructor and 1 operator =. Also, we can eliminate the check with -1 since we can sure that that enum is existed.
    – Danh
    Dec 15, 2015 at 5:54
  • So i have to specify the enumerator "AppleType" ?
    – JustWe
    Dec 15, 2015 at 5:56
  • @Danh Yes you can do that. My actual code is a template function that takes in the enum name and the enum value to return the string. I modified it to answer the question. My usage is string = convertEnumToQString<ModelApple>("AppleType", ModelApple::Big); so I need to check for a valid enum and assert if not valid for debugging. Also I assert before the return thus the reason for first assigning and then returning (purely for debugging).
    – CJCombrink
    Dec 15, 2015 at 6:23
  • 1
    I get the the point for the template. Regarding the declaration, assign, and return, I still think that we shouldn't take an extra step,
    – Danh
    Dec 15, 2015 at 6:28
  • @user3308570 Unfortunately yes unless you have Qt 5.5 (then you can use the other answer) that is why I suggest adding the check for in case you decide to change the name of the enum in future.
    – CJCombrink
    Dec 15, 2015 at 6:41
9

For the global Enum declaring use this in any header file:

namespace YourNamespace {

Q_NAMESPACE

enum YourEnum: int {

    EnumValue1,
    EnumValue2
};
Q_ENUM_NS(YourEnum)

}

and this where you want to get Enum description:

QMetaEnum metaEnum = QMetaEnum::fromType<YourEnum>();
qDebug() << "Enum description: " << metaEnum.name() << "::" << metaEnum.valueToKey(YourEnum::EnumValue2);
2
  • 1
    I had tried to get this to work numerous times over the years, and recently I tried your solution, and was successful! The trick was wrapping my "global" enum (ie: not contained within a class) inside a namespace. Thanks!
    – Doug
    Dec 7, 2020 at 19:18
  • 1
    Such a helpful answer. Thank you!! Oct 7, 2021 at 21:11
2

How about:

QString convertEnumToQString(ModelApple::AppleType type)
{
    const QMetaObject &mo = ModelApple::staticMetaObject;
    int index = mo.indexOfEnumerator("AppleType");
    QMetaEnum metaEnum = mo.enumerator(index);
    return metaEnum.valueToKey(type);
}

UPDATED: For Qt 5.5, see this answer

0
2

I faced the same problem and this is how i solved it. This is especially for Qt 4.8

QString string = enumToString(ModelApple::Big);

QString ModelApple::enumToString(AppleType apple)
{
    int index = metaObject()->indexOfEnumerator("AppleType");
    QMetaEnum metaEnum = metaObject()->enumerator(index);
    return metaEnum.valueToKey(apple);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.