Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Q_NAMESPACE and CMake



  • 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



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


Log in to reply