Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. multiple Q_ENUM_NS in one namespace OK?

multiple Q_ENUM_NS in one namespace OK?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by mzimmers
    #1

    Hi all -

    This is what I'm trying to do:

    // equipmentitem.h
    namespace EquipmentNS {
    Q_NAMESPACE
        enum Category {
            ...
        };
        Q_ENUM_NS(Category)
        enum PowerState {
            POWER_OFF,
            POWER_ON
        };
        Q_ENUM_NS(PowerState)
    }
    ...
    // main.cpp
        // register the Equipment state enums so they can be used in QML.
        qmlRegisterUncreatableMetaObject(
            EquipmentNS::staticMetaObject, // meta object created by Q_NAMESPACE macro
            "equipmentNS",                // import statement (can be any string)
            1, 0,                           // major and minor version of the import
            "EquipmentNS",                    // name used in QML (does not have to match C++ name)
            "Error: only enums"             // error in case someone tries to create an object
            );
    
    
    

    A few questions:

    1. is this legal? dumb question; disregard.
    2. is this correct?
    3. is this complete?
    4. is this a bad idea?
    5. how do I reference the respective enums in QML? I'd have expected it to be something like this:
    EquipmentNS.POWER_ON
    

    But that gives me an undefined error.

    Thanks...

    mzimmersM 1 Reply Last reply
    0
    • mzimmersM mzimmers

      Hi all -

      This is what I'm trying to do:

      // equipmentitem.h
      namespace EquipmentNS {
      Q_NAMESPACE
          enum Category {
              ...
          };
          Q_ENUM_NS(Category)
          enum PowerState {
              POWER_OFF,
              POWER_ON
          };
          Q_ENUM_NS(PowerState)
      }
      ...
      // main.cpp
          // register the Equipment state enums so they can be used in QML.
          qmlRegisterUncreatableMetaObject(
              EquipmentNS::staticMetaObject, // meta object created by Q_NAMESPACE macro
              "equipmentNS",                // import statement (can be any string)
              1, 0,                           // major and minor version of the import
              "EquipmentNS",                    // name used in QML (does not have to match C++ name)
              "Error: only enums"             // error in case someone tries to create an object
              );
      
      
      

      A few questions:

      1. is this legal? dumb question; disregard.
      2. is this correct?
      3. is this complete?
      4. is this a bad idea?
      5. how do I reference the respective enums in QML? I'd have expected it to be something like this:
      EquipmentNS.POWER_ON
      

      But that gives me an undefined error.

      Thanks...

      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #2

      The answer is "yes," it works. I had some mistakes in my attempt which caused me to question it, but it works fine when you do it right.

      In simplest terms (credit to this post):

      // header file of your choice
      namespace EquipmentNS {
      Q_NAMESPACE
          enum Category {
              ...
          };
          Q_ENUM_NS(Category)
          enum PowerState {
              POWER_OFF,
              POWER_ON
          };
          Q_ENUM_NS(PowerState)
      }
      
      // main.cpp
      qmlRegisterUncreatableMetaObject(
          EquipmentNS::staticMetaObject, // meta object created by Q_NAMESPACE macro
          "equipment.enums",                // used in qml import statement
          1, 0,                           // major and minor version of the import
          "EquipmentNS",                    // name used in QML (does not have to match C++ name)
          "Error: only enums"             // error in case someone tries to create an object
          );
      
      // your qml file
      import equipment.enums // same as 2nd argument in call to qmlRegisterUncreatableMetaObject().
      ...
      console.log(EquipmentNS.POWER_ON)
      
      1 Reply Last reply
      2
      • mzimmersM mzimmers has marked this topic as solved on
      • M Offline
        M Offline
        MikePooh
        wrote on last edited by
        #3

        Yes. It works. However you won't be able to have same enum names since one will shadows another one.

        namespace EquipmentNS {
        Q_NAMESPACE
            enum Category {
                POWER_OFF = 100,
                ...
            };
            Q_ENUM_NS(Category)
            enum PowerState {
                POWER_OFF, #Here value 0 will shadow value 100
                POWER_ON
            };
            Q_ENUM_NS(PowerState)
        }
        
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved