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. How to use enum declared in another class as Q_Enum
Forum Updated to NodeBB v4.3 + New Features

How to use enum declared in another class as Q_Enum

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 7 Posters 4.6k Views 4 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.
  • A Asperamanca

    @IknowQT
    What are you trying to achieve?
    Why not use Q_ENUM_NS instead in the namespace?

    I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #3

    @Asperamanca

    I want to define all enum classes in a place called CollectEnums.
    And we will declare and use CollectEnums for each required class. And I want to use enumtoString or Stringtoenum.
    If I define an enum class in one class, it works normally, but when I separate only the enum class, an error like that occurs.

    jsulmJ 1 Reply Last reply
    0
    • I IknowQT

      @Asperamanca

      I want to define all enum classes in a place called CollectEnums.
      And we will declare and use CollectEnums for each required class. And I want to use enumtoString or Stringtoenum.
      If I define an enum class in one class, it works normally, but when I separate only the enum class, an error like that occurs.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @IknowQT said in How to use enum declared in another class as Q_Enum:

      I want to define all enum classes in a place called CollectEnums

      From design point of view it is better do declare enums where they belong to instead of putting all eneums into one place.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      I A 2 Replies Last reply
      0
      • jsulmJ jsulm

        @IknowQT said in How to use enum declared in another class as Q_Enum:

        I want to define all enum classes in a place called CollectEnums

        From design point of view it is better do declare enums where they belong to instead of putting all eneums into one place.

        I Offline
        I Offline
        IknowQT
        wrote on last edited by
        #5

        @jsulm

        If there is an enum class that is commonly used, it would be more efficient to use it in one place.

        So can you answer my question?
        Is there any way to fix the error?

        A 1 Reply Last reply
        0
        • jsulmJ jsulm

          @IknowQT said in How to use enum declared in another class as Q_Enum:

          I want to define all enum classes in a place called CollectEnums

          From design point of view it is better do declare enums where they belong to instead of putting all eneums into one place.

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #6

          @jsulm said in How to use enum declared in another class as Q_Enum:

          From design point of view it is better do declare enums where they belong to instead of putting all eneums into one place.

          I beg to differ. Classes with included enums are almost impossible to forward-declare. So if you have a large project and want to use forward declarations extensively, you are better off having enums in separate header files.

          1 Reply Last reply
          0
          • I IknowQT

            @jsulm

            If there is an enum class that is commonly used, it would be more efficient to use it in one place.

            So can you answer my question?
            Is there any way to fix the error?

            A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #7

            @IknowQT said in How to use enum declared in another class as Q_Enum:

            @jsulm

            If there is an enum class that is commonly used, it would be more efficient to use it in one place.

            So can you answer my question?
            Is there any way to fix the error?

            Have you looked at Q_ENUM_NS?

            I 1 Reply Last reply
            0
            • A Asperamanca

              @IknowQT said in How to use enum declared in another class as Q_Enum:

              @jsulm

              If there is an enum class that is commonly used, it would be more efficient to use it in one place.

              So can you answer my question?
              Is there any way to fix the error?

              Have you looked at Q_ENUM_NS?

              I Offline
              I Offline
              IknowQT
              wrote on last edited by IknowQT
              #8

              @Asperamanca said in How to use enum declared in another class as Q_Enum:

              Q_ENUM_NS

              I tried it, but the same error occurs.

              Can you give me one example?

              CP71C 1 Reply Last reply
              0
              • I IknowQT

                @Asperamanca said in How to use enum declared in another class as Q_Enum:

                Q_ENUM_NS

                I tried it, but the same error occurs.

                Can you give me one example?

                CP71C Offline
                CP71C Offline
                CP71
                wrote on last edited by CP71
                #9

                @IknowQT
                Hi,
                The second answer in this link works well to me

                https://stackoverflow.com/questions/20089196/how-to-access-c-enum-from-qml

                Only issue, the qml editor doesn’t show the tips when start tip text ( Ex MyNamespace. after point the editor doesn’t show the possible keys, I’m working on

                1 Reply Last reply
                0
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #10

                  @Asperamanca said in How to use enum declared in another class as Q_Enum:

                  Q_ENUM_NS

                  https://blog.birost.com/a?ID=01500-a7ae7381-dc1f-424d-8847-220e31d68484

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    jadelisle
                    wrote on last edited by
                    #11

                    If you look closely at the compiler errors, you'll notice that the complaints are directed toward the namespace, not the enumeration. The namespace does not have a static QMetaObject to which the Qt MetaObject system can relate the enumeration. To correct this, you must enable the namespace to participate in the Qt MetaObject system[1]. For example:

                    namespace CollectEnums 
                    {
                        Q_NAMESPACE
                    
                        enum class CELL_TYPE {
                            Single = 0,
                            Multi,
                            Nanoliter
                        };
                        Q_ENUM_NS(CELL_TYPE)
                    }
                    

                    The net effect of adding 'Q_NAMESPACE' to a namespace is similar to adding 'Q_OBJECT' in that it creates a static MetaObject for the type.

                    After doing this, you can refer to 'CollectEnums::CELL_TYPE' as a type and 'CollectEnums::CELL_TYPE::Single' as a value for that type.

                    Also, in my experience, using 'enum class' (instead of just plain 'enum') also requires that the type be registered with the QMetaType system if the type is going to be used in a QVariant. This is due to how plain enums are treated (like a synonym for 'int'), and 'enum class' (which is defining a new type) [2]. To do this, add this line in the same header file, but outside of the namespace declaration:

                    namespace CollectEnums {
                        Q_NAMESPACE
                        .
                        .
                        .
                    }
                    Q_DECLARE_METATYPE(CollectEnums::CELL_TYPE)
                    

                    Doing this is absolutely necessary if the type will be referred to in QML.

                    Cheers.

                    [1] QObject documentation
                    [2] QMetaType documentation

                    I 1 Reply Last reply
                    1
                    • J jadelisle

                      If you look closely at the compiler errors, you'll notice that the complaints are directed toward the namespace, not the enumeration. The namespace does not have a static QMetaObject to which the Qt MetaObject system can relate the enumeration. To correct this, you must enable the namespace to participate in the Qt MetaObject system[1]. For example:

                      namespace CollectEnums 
                      {
                          Q_NAMESPACE
                      
                          enum class CELL_TYPE {
                              Single = 0,
                              Multi,
                              Nanoliter
                          };
                          Q_ENUM_NS(CELL_TYPE)
                      }
                      

                      The net effect of adding 'Q_NAMESPACE' to a namespace is similar to adding 'Q_OBJECT' in that it creates a static MetaObject for the type.

                      After doing this, you can refer to 'CollectEnums::CELL_TYPE' as a type and 'CollectEnums::CELL_TYPE::Single' as a value for that type.

                      Also, in my experience, using 'enum class' (instead of just plain 'enum') also requires that the type be registered with the QMetaType system if the type is going to be used in a QVariant. This is due to how plain enums are treated (like a synonym for 'int'), and 'enum class' (which is defining a new type) [2]. To do this, add this line in the same header file, but outside of the namespace declaration:

                      namespace CollectEnums {
                          Q_NAMESPACE
                          .
                          .
                          .
                      }
                      Q_DECLARE_METATYPE(CollectEnums::CELL_TYPE)
                      

                      Doing this is absolutely necessary if the type will be referred to in QML.

                      Cheers.

                      [1] QObject documentation
                      [2] QMetaType documentation

                      I Offline
                      I Offline
                      IknowQT
                      wrote on last edited by IknowQT
                      #12

                      @jadelisle said in How to use enum declared in another class as Q_Enum:

                      If you look closely at the compiler errors, you'll notice that the complaints are directed toward the namespace, not the enumeration. The namespace does not have a static QMetaObject to which the Qt MetaObject system can relate the enumeration. To correct this, you must enable the namespace to participate in the Qt MetaObject system[1]. For example:

                      error	LNK2001	"struct QMetaObject const CollectEnums::staticMetaObject" (?staticMetaObject@CollectEnums@@3UQMetaObject@@B)	
                      

                      I tried it, but the same error occurs.
                      For reference, we are developing in the QWidget method in the vs2019 environment.

                      namespace CollectEnums
                      {
                      	Q_NAMESPACE
                      	enum class CELL_TYPE
                      	{
                      		Single = 0,
                      		Multi,
                      		Nanoliter,
                      		Sipper,
                      		Temperature,
                      		Max
                      	};
                      	Q_ENUM_NS(CELL_TYPE);
                      }
                      Q_DECLARE_METATYPE(CollectEnums::CELL_TYPE)
                      
                      QString cModelCommon::EnumtoStringCellType(const CollectEnums::CELL_TYPE e)
                      {
                      	return QVariant::fromValue(e).toString();
                      }
                      
                      CollectEnums::CELL_TYPE cModelCommon::StringtoEnumCellType(QString str)
                      {
                      	CollectEnums::CELL_TYPE e = CollectEnums::CELL_TYPE::Single;
                      
                      	QMetaEnum map = QMetaEnum::fromType<CollectEnums::CELL_TYPE>();
                      	for (qint32 i = (qint32)CollectEnums::CELL_TYPE::Single; i < map.keyCount(); i++)
                      	{
                      		if (map.key(i) == str)
                      		{
                      			e = CollectEnums::CELL_TYPE(i);
                      			break;
                      		}
                      	}
                      
                      	return e;
                      }
                      

                      The peculiar thing is that if you comment out the code inside the EnumtoStringCellType and StringtoEnumCellType functions, it will be built.
                      I want to convert an enum to a string and vice versa, convert a string to an enum.

                      J 1 Reply Last reply
                      0
                      • I IknowQT

                        @jadelisle said in How to use enum declared in another class as Q_Enum:

                        If you look closely at the compiler errors, you'll notice that the complaints are directed toward the namespace, not the enumeration. The namespace does not have a static QMetaObject to which the Qt MetaObject system can relate the enumeration. To correct this, you must enable the namespace to participate in the Qt MetaObject system[1]. For example:

                        error	LNK2001	"struct QMetaObject const CollectEnums::staticMetaObject" (?staticMetaObject@CollectEnums@@3UQMetaObject@@B)	
                        

                        I tried it, but the same error occurs.
                        For reference, we are developing in the QWidget method in the vs2019 environment.

                        namespace CollectEnums
                        {
                        	Q_NAMESPACE
                        	enum class CELL_TYPE
                        	{
                        		Single = 0,
                        		Multi,
                        		Nanoliter,
                        		Sipper,
                        		Temperature,
                        		Max
                        	};
                        	Q_ENUM_NS(CELL_TYPE);
                        }
                        Q_DECLARE_METATYPE(CollectEnums::CELL_TYPE)
                        
                        QString cModelCommon::EnumtoStringCellType(const CollectEnums::CELL_TYPE e)
                        {
                        	return QVariant::fromValue(e).toString();
                        }
                        
                        CollectEnums::CELL_TYPE cModelCommon::StringtoEnumCellType(QString str)
                        {
                        	CollectEnums::CELL_TYPE e = CollectEnums::CELL_TYPE::Single;
                        
                        	QMetaEnum map = QMetaEnum::fromType<CollectEnums::CELL_TYPE>();
                        	for (qint32 i = (qint32)CollectEnums::CELL_TYPE::Single; i < map.keyCount(); i++)
                        	{
                        		if (map.key(i) == str)
                        		{
                        			e = CollectEnums::CELL_TYPE(i);
                        			break;
                        		}
                        	}
                        
                        	return e;
                        }
                        

                        The peculiar thing is that if you comment out the code inside the EnumtoStringCellType and StringtoEnumCellType functions, it will be built.
                        I want to convert an enum to a string and vice versa, convert a string to an enum.

                        J Offline
                        J Offline
                        jadelisle
                        wrote on last edited by
                        #13

                        @IknowQT
                        Have you tried using the facilities in QMetaEnum to convert between the value and name?
                        https://doc.qt.io/qt-5/qmetaenum.html

                        I 1 Reply Last reply
                        0
                        • J jadelisle

                          @IknowQT
                          Have you tried using the facilities in QMetaEnum to convert between the value and name?
                          https://doc.qt.io/qt-5/qmetaenum.html

                          I Offline
                          I Offline
                          IknowQT
                          wrote on last edited by
                          #14

                          @jadelisle

                          I don't know how to use qmetaEnum.
                          Do you have any examples I can refer to?

                          Pl45m4P 1 Reply Last reply
                          0
                          • I IknowQT

                            @jadelisle

                            I don't know how to use qmetaEnum.
                            Do you have any examples I can refer to?

                            Pl45m4P Offline
                            Pl45m4P Offline
                            Pl45m4
                            wrote on last edited by
                            #15

                            @IknowQT

                            QMetaEnum myEnum = QMetaEnum::fromType<CellType>();
                            QString s = myEnum.valueToKey(CellType::Single);
                            

                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            I 1 Reply Last reply
                            0
                            • Pl45m4P Pl45m4

                              @IknowQT

                              QMetaEnum myEnum = QMetaEnum::fromType<CellType>();
                              QString s = myEnum.valueToKey(CellType::Single);
                              
                              I Offline
                              I Offline
                              IknowQT
                              wrote on last edited by IknowQT
                              #16

                              @Pl45m4

                              I tested it, but the result is the same error.

                              It doesn't seem to be a problem with the code, but it seems to be because of the structure of my source code.
                              cModelCommon inherits cSerializer as its parent
                              If you look at the structure of cSerializer, there are xml parsing codes.
                              It was used by declaring Q_GADGET and QS_SERIALIZABLE instead of qobject.

                              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