Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:01:23+00:00 2026-06-03T08:01:23+00:00

Given an enum: enum AnEnum { Foo, Bar, Bash, Baz }; Can you iterate

  • 0

Given an enum:

enum AnEnum { Foo, Bar, Bash, Baz };

Can you iterate over each of these enums using Qt’s foreach loop?

This code doesn’t compile (not that I expected it to…)

foreach(AnEnum enum, AnEnum)
{
// do nothing
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:01:25+00:00Added an answer on June 3, 2026 at 8:01 am

    If it is moced into a QMetaEnum than you can iterate over it like this:

    QMetaEnum e = ...;
    for (int i = 0; i < e.keyCount(); i++)
    {
        const char* s = e.key(i); // enum name as string
        int v = e.value(i); // enum index
        ...
    }
    

    http://qt-project.org/doc/qt-4.8/qmetaenum.html

    Example using QNetworkReply which is a QMetaEnum:

    QNetworkReply::NetworkError error;
    error = fetchStuff();
    if (error != QNetworkReply::NoError) {
        QString errorValue;
        QMetaObject meta = QNetworkReply::staticMetaObject;
        for (int i=0; i < meta.enumeratorCount(); ++i) {
            QMetaEnum m = meta.enumerator(i);
            if (m.name() == QLatin1String("NetworkError")) {
                errorValue = QLatin1String(m.valueToKey(error));
                break;
            }
        }
        QMessageBox box(QMessageBox::Information, "Failed to fetch",
                    "Fetching stuff failed with error '%1`").arg(errorValue),
                    QMessageBox::Ok);
        box.exec();
        return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an enum where each instance is associated with some value: public enum SQLState
Given a class containing an enum: public class MyClass { public enum NestedEnum {
So I have function that formats a date to coerce to given enum DateType{CURRENT,
Given the following information Public Enum Request As Byte None = 0 Identity =
Given the following flags, [Flags] public enum Operations { add = 1, subtract =
I have read the documentation that states that "given the type of the enum,
Since enumeration uses integers, what other structure can I use to give me enum-like
Given a package, how can I automatically find all its sub-packages?
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
I'm extending an Enum and, given the following code, selectListItems is a generic List

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.