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. Emitting enum class in namespace not working
Qt 6.11 is out! See what's new in the release blog

Emitting enum class in namespace not working

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 2.3k Views 2 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.
  • Christian EhrlicherC Christian Ehrlicher

    Your code above is not consistent with the enum naming and others. Please provide a minimal, compilable example so we can reproduce the issue.

    P Offline
    P Offline
    Palli
    wrote on last edited by
    #5

    @Christian-Ehrlicher

    Code: https://github.com/Drllap/EmitEnumFromNamspaceTest

    a8109696-e6d2-446d-8b09-f681ea3b238d-image.png

    Looks like I was wrong about this working with structs:
    MyEnum and MyStruct are metatype declared with:

    namespace MyNamespace {
        Q_DECLARE_METATYPE(MyEnum)
        Q_DECLARE_METATYPE(MyStruct)
    }
    

    and are successfully sent between threads but MyEnum2, MyStruct2

    Q_DECLARE_METATYPE(MyNamespace::MyEnum2)
    Q_DECLARE_METATYPE(MyNamespace::MyStruct2)
    

    are not sent between threads.

    Then there is another problem, this:

    namespace MyNamespace {
        Q_DECLARE_METATYPE(MyEnum)
        Q_DECLARE_METATYPE(MyStruct)
    }
    

    doesn't compile on Ubuntu18 with GCC

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @Palli said in Emitting enum class in namespace not working:

      are not sent between threads.

      They are, please recheck.

      doesn't compile on Ubuntu18 with GCC

      Because you use the macro wrong:

      If MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace.

      This works as expected (and shown in the documentation)

      
      Q_DECLARE_METATYPE(MyNamespace::MyEnum)
      Q_DECLARE_METATYPE(MyNamespace::MyEnum2)
      Q_DECLARE_METATYPE(MyNamespace::MyStruct)
      Q_DECLARE_METATYPE(MyNamespace::MyStruct2)
      
      void MetaRegister::RegisterAllOfTheThings()
      {
          qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyEnum>();
          qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyEnum2>();
          qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyStruct>();
          qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyStruct2>();
      }
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      P 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Palli said in Emitting enum class in namespace not working:

        are not sent between threads.

        They are, please recheck.

        doesn't compile on Ubuntu18 with GCC

        Because you use the macro wrong:

        If MyStruct is in a namespace, the Q_DECLARE_METATYPE() macro has to be outside the namespace.

        This works as expected (and shown in the documentation)

        
        Q_DECLARE_METATYPE(MyNamespace::MyEnum)
        Q_DECLARE_METATYPE(MyNamespace::MyEnum2)
        Q_DECLARE_METATYPE(MyNamespace::MyStruct)
        Q_DECLARE_METATYPE(MyNamespace::MyStruct2)
        
        void MetaRegister::RegisterAllOfTheThings()
        {
            qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyEnum>();
            qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyEnum2>();
            qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyStruct>();
            qDebug() << "Registering meta type: " << qRegisterMetaType<MyNamespace::MyStruct2>();
        }
        
        P Offline
        P Offline
        Palli
        wrote on last edited by
        #7

        @Christian-Ehrlicher

        They are, please recheck.

        I did, and they are not.

        Because you use the macro wrong:

        I know I'm using the macro wrong, but it is the only way I have found to get the signaling between threads to work.

        Q_DECLARE_METATYPE(MyNamespace::MyEnum)
        Q_DECLARE_METATYPE(MyNamespace::MyEnum2)
        Q_DECLARE_METATYPE(MyNamespace::MyStruct)
        Q_DECLARE_METATYPE(MyNamespace::MyStruct2)

        void MetaRegister::RegisterAllOfTheThings()
        {
        qDebug() << "Registering meta type: " << qRegisterMetaTypeMyNamespace::MyEnum();
        qDebug() << "Registering meta type: " << qRegisterMetaTypeMyNamespace::MyEnum2();
        qDebug() << "Registering meta type: " << qRegisterMetaTypeMyNamespace::MyStruct();
        qDebug() << "Registering meta type: " << qRegisterMetaTypeMyNamespace::MyStruct2();
        }

        If I use this code above, I get this:
        alt text

        1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #8

          Please push the code with my changes so others can test it

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          P 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Please push the code with my changes so others can test it

            P Offline
            P Offline
            Palli
            wrote on last edited by
            #9

            @Christian-Ehrlicher Done

            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #10

              Ah, you still forget to fully qualify the enums in the slots as I said in my first post. I did this locally.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              P 2 Replies Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                Ah, you still forget to fully qualify the enums in the slots as I said in my first post. I did this locally.

                P Offline
                P Offline
                Palli
                wrote on last edited by
                #11

                @Christian-Ehrlicher

                I don't quite follow, are you talking about in the Receiver class?

                class Receiver : public QObject
                {
                    Q_OBJECT
                
                    public slots:
                        void OnGettingEnum(MyNamespace::MyEnum e) {
                            qDebug() << "Received on thread: " << QThread::currentThread();
                            qDebug() << "Got the MyEnum: " << static_cast<int>(e);
                        }
                        void OnGettingEnum2(MyNamespace::MyEnum2 e) {
                            qDebug() << "Received on thread: " << QThread::currentThread();
                            qDebug() << "Got the MyEnum2: " << static_cast<int>(e);
                        }
                        void OnGettingStruct(MyNamespace::MyStruct s) {
                            qDebug() << "Received on thread: " << QThread::currentThread();
                            qDebug() << "Got the MyStruct";
                        }
                        void OnGettingStruct2(MyNamespace::MyStruct2 s) {
                            qDebug() << "Received on thread: " << QThread::currentThread();
                            qDebug() << "Got the MyStruct2";
                        }
                };
                
                1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  Ah, you still forget to fully qualify the enums in the slots as I said in my first post. I did this locally.

                  P Offline
                  P Offline
                  Palli
                  wrote on last edited by
                  #12

                  @Christian-Ehrlicher

                  Ok, you are talking about the signals in the Sender class?

                  Changing this

                      class Sender : public QObject
                      {
                          Q_OBJECT
                      signals:
                          void EmitEnum(MyEnum);
                          void EmitEnum2(MyEnum2);
                          void EmitStruct(MyStruct);
                          void EmitStruct2(MyStruct2);
                      };
                  
                  

                  to this

                      class Sender : public QObject
                      {
                          Q_OBJECT
                      signals:
                          void EmitEnum(MyNamespace::MyEnum);
                          void EmitEnum2(MyNamespace::MyEnum2);
                          void EmitStruct(MyNamespace::MyStruct);
                          void EmitStruct2(MyNamespace::MyStruct2);
                      };
                  

                  fixes it.

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • P Palli

                    @Christian-Ehrlicher

                    Ok, you are talking about the signals in the Sender class?

                    Changing this

                        class Sender : public QObject
                        {
                            Q_OBJECT
                        signals:
                            void EmitEnum(MyEnum);
                            void EmitEnum2(MyEnum2);
                            void EmitStruct(MyStruct);
                            void EmitStruct2(MyStruct2);
                        };
                    
                    

                    to this

                        class Sender : public QObject
                        {
                            Q_OBJECT
                        signals:
                            void EmitEnum(MyNamespace::MyEnum);
                            void EmitEnum2(MyNamespace::MyEnum2);
                            void EmitStruct(MyNamespace::MyStruct);
                            void EmitStruct2(MyNamespace::MyStruct2);
                        };
                    

                    fixes it.

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    @Palli said in Emitting enum class in namespace not working:

                    Ok, you are talking about the signals in the Sender class?

                    Yes:

                    forgot to add the namespace to your signal - otherwise there would have been a warning about my_namespace ::my_enum and not my_enum.

                    Please mark the topic as solved then.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    P 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Palli said in Emitting enum class in namespace not working:

                      Ok, you are talking about the signals in the Sender class?

                      Yes:

                      forgot to add the namespace to your signal - otherwise there would have been a warning about my_namespace ::my_enum and not my_enum.

                      Please mark the topic as solved then.

                      P Offline
                      P Offline
                      Palli
                      wrote on last edited by
                      #14

                      @Christian-Ehrlicher Done, thanks for you help

                      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