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. Q_ENUM and QVariant in a class in a namespace
Forum Updated to NodeBB v4.3 + New Features

Q_ENUM and QVariant in a class in a namespace

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 538 Views 3 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.
  • K Offline
    K Offline
    kshots
    wrote on last edited by kshots
    #1

    I have a class with the structure:

    namespace NS
    {
       //Q_NAMESPACE   // Apparently I can't have multiple Q_NAMESPACE definitions if multiple headers have the same namespace declaration
       class A : public QObject
       {
          Q_OBJECT
       public:
          enum class MyEnum:quint8 {A,B,C};
          Q_ENUM(NS::A::MyEnum)
       };
    }
    

    I then want to create a QVariant from this enum:

    void SomeFunc()
    {
       QVariant V(NS::A::MyEnum::A);
    }
    

    ... this fails, stating that there is no valid conversion from the enum to QVariant. Am I doing something wrong with the Q_ENUM macro above? I'm having difficulty deciphering when I should use Q_NAMESPACE, Q_ENUM_NS, Q_ENUM, etc. Apparently if an enum is in a class, you don't use Q_ENUM_NS even if the class is in a namespace. Any pointers?

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

      According to the docs it must be Q_ENUM(MyEnum). And I'm not quite sure it is working with enum class

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

      SGaistS 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        According to the docs it must be Q_ENUM(MyEnum). And I'm not quite sure it is working with enum class

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @Christian-Ehrlicher if memory serves well, enum class should already work.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • Axel SpoerlA Online
          Axel SpoerlA Online
          Axel Spoerl
          Moderators
          wrote on last edited by
          #4

          It works, I am using it every day!

          Software Engineer
          The Qt Company, Oslo

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kshots
            wrote on last edited by
            #5

            Evidently it doesn't work when used as above...

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Can you provide a minimal project that triggers your issue ?
              That will allow for everybody to work on the same base.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              K 1 Reply Last reply
              1
              • SGaistS SGaist

                Can you provide a minimal project that triggers your issue ?
                That will allow for everybody to work on the same base.

                K Offline
                K Offline
                kshots
                wrote on last edited by
                #7

                Sure, just add the code below to the post above (using the class in A.h and filling in a basic main() function for SomFunc() for a test project:

                CMakeLists.txt:

                cmake_minimum_required(VERSION 3.5)
                
                project(project LANGUAGES CXX)
                
                set(CMAKE_AUTOMOC ON)
                
                find_package(Qt6 COMPONENTS Core REQUIRED)
                
                set(sources main.cpp)
                set(headers A.h)
                
                add_executable(test ${sources} ${headers})
                target_link_libraries(test PRIVATE Qt6::Core)
                
                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kshots
                  wrote on last edited by
                  #8

                  I couldn't include the code above because apparently the same code above is flagged as spam by the forum and refuses to post.

                  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