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_NAMESPACE and CMake

Q_NAMESPACE and CMake

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.1k 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.
  • L Offline
    L Offline
    ligabre
    wrote on last edited by ligabre
    #1

    Hello all,

    I am trying to use an enum defined inside a namespace in a QML context. The following code works well when I use QMake :

    // In file ns.h
    namespace NameSpace
    {
        Q_NAMESPACE
    
        enum MyEnum
        {
            ValOne,
            ValTwo
        };
        Q_ENUM_NS(MyEnum)
    }
    

    And in main.cpp :

        qmlRegisterUncreatableMetaObject(NameSpace::staticMetaObject,
                                         "com.test", 1, 0,
                                         "Namespace", "Error: only for enums & flags");
    

    But when I use CMake with a default generated CMakeLists.txt from Qt Creator, I have the following error :

    main.cpp.obj:-1: erreur : LNK2001: symbole externe non résolu "struct QMetaObject const NameSpace::staticMetaObject" (?staticMetaObject@NameSpace@@3UQMetaObject@@B)
    

    I think I've forgotten to set some variable in CMake in order to be able to use either the Q_NAMESPACE or the qmlRegisterUncreatableMetaType.

    Could someone please help me to identify what I'm missing here ?

    Thank you very much for your help in advance !

    Ligabre

    S 1 Reply Last reply
    0
    • L ligabre

      Hello all,

      I am trying to use an enum defined inside a namespace in a QML context. The following code works well when I use QMake :

      // In file ns.h
      namespace NameSpace
      {
          Q_NAMESPACE
      
          enum MyEnum
          {
              ValOne,
              ValTwo
          };
          Q_ENUM_NS(MyEnum)
      }
      

      And in main.cpp :

          qmlRegisterUncreatableMetaObject(NameSpace::staticMetaObject,
                                           "com.test", 1, 0,
                                           "Namespace", "Error: only for enums & flags");
      

      But when I use CMake with a default generated CMakeLists.txt from Qt Creator, I have the following error :

      main.cpp.obj:-1: erreur : LNK2001: symbole externe non résolu "struct QMetaObject const NameSpace::staticMetaObject" (?staticMetaObject@NameSpace@@3UQMetaObject@@B)
      

      I think I've forgotten to set some variable in CMake in order to be able to use either the Q_NAMESPACE or the qmlRegisterUncreatableMetaType.

      Could someone please help me to identify what I'm missing here ?

      Thank you very much for your help in advance !

      Ligabre

      S Offline
      S Offline
      simonschmeisser
      wrote on last edited by
      #2

      @ligabre this is because CMakes automoc does not recognize the Q_NAMESPACE keyword and therefore does not run moc on the header. It was added in CMake 3.10. Until then you have to use qt5_wrap_cpp(moc_file ns.h) and then include ${moc_file} with your target

      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