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

How to use enum declared in another class as Q_Enum

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 7 Posters 4.5k Views
  • 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.
  • I Offline
    I Offline
    IknowQT
    wrote on 17 Mar 2022, 04:00 last edited by
    #1
    namespace CollectEnums
    {
       	enum class EpicModeType
    	{
    		Photometric = 0,
    		Spectrum,
    		Kinetics,
    		Quatitation,
    		Method,
    		Hach,
    		Merck,
    	};
    
    	enum class CELL_TYPE
    	{
    		Single = 0,
    		Multi,
    		Nanoliter,
    		Sipper,
    		Temperature,
    		Max
    	};
    	//Q_ENUM(CELL_TYPE);
    }
    
    class cModelCommon : public cSerializer
    {
    	Q_ENUM(CollectEnums::CELL_TYPE);      // error 
    }
    
    error C2039 'staticMetaObject': not a member of 'CollectEnums' MainWidget D:\ProgramSrc\POP\MainWidget\x64\Debug\moc\moc_cModelStructure.cpp 154
    error C2065 'staticMetaObject': undeclared identifier. MainWidget D:\ProgramSrc\POP\MainWidget\x64\Debug\moc\moc_cModelStructure.cpp 154
    error C2672 'QMetaObject::staticMetaObject': no matching overloaded function (x64\Debug\moc\moc_cModelStructure.cpp while compiling source file) MainWidget C:\Qt\5.15.2\msvc2019_64\include\QtCore\qobjectdefs.h 600
    error C2975 'MO': invalid template argument for 'QMetaObject::staticMetaObject' A compile-time constant expression is required. (x64\Debug\moc\moc_cModelStructure.cpp while compiling source file) MainWidget C:\Qt\5.15.2\msvc2019_64\include\QtCore\qobjectdefs.h 600
    
    A 1 Reply Last reply 17 Mar 2022, 09:36
    0
    • I IknowQT
      17 Mar 2022, 04:00
      namespace CollectEnums
      {
         	enum class EpicModeType
      	{
      		Photometric = 0,
      		Spectrum,
      		Kinetics,
      		Quatitation,
      		Method,
      		Hach,
      		Merck,
      	};
      
      	enum class CELL_TYPE
      	{
      		Single = 0,
      		Multi,
      		Nanoliter,
      		Sipper,
      		Temperature,
      		Max
      	};
      	//Q_ENUM(CELL_TYPE);
      }
      
      class cModelCommon : public cSerializer
      {
      	Q_ENUM(CollectEnums::CELL_TYPE);      // error 
      }
      
      error C2039 'staticMetaObject': not a member of 'CollectEnums' MainWidget D:\ProgramSrc\POP\MainWidget\x64\Debug\moc\moc_cModelStructure.cpp 154
      error C2065 'staticMetaObject': undeclared identifier. MainWidget D:\ProgramSrc\POP\MainWidget\x64\Debug\moc\moc_cModelStructure.cpp 154
      error C2672 'QMetaObject::staticMetaObject': no matching overloaded function (x64\Debug\moc\moc_cModelStructure.cpp while compiling source file) MainWidget C:\Qt\5.15.2\msvc2019_64\include\QtCore\qobjectdefs.h 600
      error C2975 'MO': invalid template argument for 'QMetaObject::staticMetaObject' A compile-time constant expression is required. (x64\Debug\moc\moc_cModelStructure.cpp while compiling source file) MainWidget C:\Qt\5.15.2\msvc2019_64\include\QtCore\qobjectdefs.h 600
      
      A Offline
      A Offline
      Asperamanca
      wrote on 17 Mar 2022, 09:36 last edited by
      #2

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

      I 1 Reply Last reply 18 Mar 2022, 08:37
      1
      • A Asperamanca
        17 Mar 2022, 09:36

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

        I Offline
        I Offline
        IknowQT
        wrote on 18 Mar 2022, 08:37 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.

        J 1 Reply Last reply 18 Mar 2022, 08:41
        0
        • I IknowQT
          18 Mar 2022, 08:37

          @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.

          J Online
          J Online
          jsulm
          Lifetime Qt Champion
          wrote on 18 Mar 2022, 08:41 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 20 Mar 2022, 23:41
          0
          • J jsulm
            18 Mar 2022, 08:41

            @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 20 Mar 2022, 23:41 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 21 Mar 2022, 07:32
            0
            • J jsulm
              18 Mar 2022, 08:41

              @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 21 Mar 2022, 07:31 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
                20 Mar 2022, 23:41

                @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 21 Mar 2022, 07:32 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 22 Mar 2022, 01:15
                0
                • A Asperamanca
                  21 Mar 2022, 07:32

                  @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 22 Mar 2022, 01:15 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 22 Mar 2022, 06:56
                  0
                  • I IknowQT
                    22 Mar 2022, 01:15

                    @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 22 Mar 2022, 06:56 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 22 Mar 2022, 14:25 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 22 Mar 2022, 16:30 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 22 Mar 2022, 23:43
                        1
                        • J jadelisle
                          22 Mar 2022, 16:30

                          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 22 Mar 2022, 23:43 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 15 Apr 2022, 13:56
                          0
                          • I IknowQT
                            22 Mar 2022, 23:43

                            @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 15 Apr 2022, 13:56 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 17 Apr 2022, 23:33
                            0
                            • J jadelisle
                              15 Apr 2022, 13:56

                              @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 17 Apr 2022, 23:33 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 18 Apr 2022, 00:46
                              0
                              • I IknowQT
                                17 Apr 2022, 23:33

                                @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 18 Apr 2022, 00:46 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 18 Apr 2022, 02:10
                                0
                                • Pl45m4P Pl45m4
                                  18 Apr 2022, 00:46

                                  @IknowQT

                                  QMetaEnum myEnum = QMetaEnum::fromType<CellType>();
                                  QString s = myEnum.valueToKey(CellType::Single);
                                  
                                  I Offline
                                  I Offline
                                  IknowQT
                                  wrote on 18 Apr 2022, 02:10 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