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

Emitting enum class in namespace not working

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 2 Posters 1.7k 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.
  • P Offline
    P Offline
    Palli
    wrote on 18 Feb 2021, 15:31 last edited by Palli
    #1

    I ran into something weird today, maybe someone can explain what is going on.

    This is more or less my settup:

    my_class.h:

    namespace my_namespace {
        enum class my_enum : uint64_t { one, two three};
    
        class my_class : public QObject
        {
            Q_OBJECT
    
        signals:
            void EmittMyEnum(my_enum);
        }
    }
    

    Then there is a function registerAllOfTheThings that is called from main:

    meta_register.h:

    #include "my_class.h"
    Q_DECLARE_METATYPE(my_namespace ::BathyHint)
    
    void registerAllOfTheThings()
    {
        qRegisterMetaType<my_namespace ::BathyHint>();
    }
    

    Now when I emit EmittMyEnum it doesn't get emitted between threads. I get this message to the console:

    QObject::connect: Cannot queue arguments of type 'my_enum'
    (Make sure 'my_enum' is registered using qRegisterMetaType().)
    

    But if I change the Q_DECLARE_METATYPE call to:

    namespace my_namespace {
        Q_DECLARE_METATYPE(BathyHint)
    }
    

    everything workes fine.

    But the thing is, I have done this before:

    Q_DECLARE_METATYPE(somenamespace::sometype)
    

    Where sometype is a class or a struct and it workes fine, just doesn't seem to work for enum.

    BTW, running on Windows 10, compiling with Visual Studio 2019 and Qt 5.14.1

    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 18 Feb 2021, 15:40 last edited by
      #2

      Please show your connect statement. Looks like you're using the old style connect and forgot to add the namespace to your signal - otherwise there would have been a warning about my_namespace ::my_enum and not my_enum. Fix your signal and use the new style connect.

      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 18 Feb 2021, 16:06
      0
      • C Christian Ehrlicher
        18 Feb 2021, 15:40

        Please show your connect statement. Looks like you're using the old style connect and forgot to add the namespace to your signal - otherwise there would have been a warning about my_namespace ::my_enum and not my_enum. Fix your signal and use the new style connect.

        P Offline
        P Offline
        Palli
        wrote on 18 Feb 2021, 16:06 last edited by Palli
        #3

        @Christian-Ehrlicher
        The connection is something like this

        QObject::connet(my_class_ptr, &my_class::EmittMyEnum,
        some_other_class_ptr, &some_other_class::other_class_slot);
        

        But this connection is made in my_namespace , but some_other_class is not in my_namespace.

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 18 Feb 2021, 16:09 last edited by
          #4

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

          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 18 Feb 2021, 20:23
          0
          • C Christian Ehrlicher
            18 Feb 2021, 16:09

            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 18 Feb 2021, 20:23 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
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 19 Feb 2021, 06:11 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 19 Feb 2021, 09:58
              1
              • C Christian Ehrlicher
                19 Feb 2021, 06:11

                @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 19 Feb 2021, 09:58 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
                • C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 19 Feb 2021, 10:35 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 19 Feb 2021, 10:40
                  0
                  • C Christian Ehrlicher
                    19 Feb 2021, 10:35

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

                    P Offline
                    P Offline
                    Palli
                    wrote on 19 Feb 2021, 10:40 last edited by
                    #9

                    @Christian-Ehrlicher Done

                    1 Reply Last reply
                    0
                    • C Online
                      C Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 19 Feb 2021, 10:43 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 19 Feb 2021, 10:52
                      1
                      • C Christian Ehrlicher
                        19 Feb 2021, 10:43

                        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 19 Feb 2021, 10:52 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
                        • C Christian Ehrlicher
                          19 Feb 2021, 10:43

                          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 19 Feb 2021, 11:02 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.

                          C 1 Reply Last reply 19 Feb 2021, 11:04
                          0
                          • P Palli
                            19 Feb 2021, 11:02

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

                            C Online
                            C Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 19 Feb 2021, 11:04 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 19 Feb 2021, 11:07
                            0
                            • C Christian Ehrlicher
                              19 Feb 2021, 11:04

                              @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 19 Feb 2021, 11:07 last edited by
                              #14

                              @Christian-Ehrlicher Done, thanks for you help

                              1 Reply Last reply
                              0

                              3/14

                              18 Feb 2021, 16:06

                              topic:navigator.unread, 11
                              • Login

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