#include#include enum Fruit { Apple, Banana, Cherry }; int main() { Fruit fruit = Banana; QMetaEnum metaEnum = QMetaEnum::fromType (); const char* fruitName = metaEnum.valueToKey(fruit); qDebug() << "The fruit is" << fruitName; // Output: The fruit is Banana }
#includeThis example shows how to iterate over all enumeration values using the `keyCount` and `key` functions of QMetaEnum. The QMetaEnum class is part of the Qt Core module, which is a part of the Qt C++ library.#include enum Animal { Cat, Dog, Elephant }; int main() { QMetaEnum metaEnum = QMetaEnum::fromType (); int count = metaEnum.keyCount(); for (int i = 0; i < count; ++i) { const char* key = metaEnum.key(i); int value = metaEnum.value(i); qDebug() << "Animal" << key << "has value" << value; } // Output: // Animal Cat has value 0 // Animal Dog has value 1 // Animal Elephant has value 2 }