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

Use same class name in different namespaces for QObject derived classes



  • Hi,

    I want to know how I can write two QObject derived classes (Q_OBJECT macro defined) with the same class name and file name but different namespaces.

    I have a project structure where each folder represents a namespace and the file name is the name of the class. Example: a/myclass.h and b/myclass.h for classes A::MyClass and B::MyClass respectively. Normally this isn't a problem for c++ projects, but MOC creates .obj files using only the file name, so it attempts to create moc_myclass.obj twice.

    Is there a way to tell MOC to name the object file according to its full type (namespace + class)?

    Thank you.



  • On the surface, it looks like the answer is no. A workaround might be to create subprojects for each namespace, and have each subproject create a dll/so/a file based on the namespace name. then the master project can just reference the subproject code by its library.


  • Lifetime Qt Champion

    Hi,

    IIRC, there was the object_parallel_to_source qmake config option for that kind of cases (best avoided). However I haven't used it so I can't comment on whether it's working.

    One alternative could be to use the technique described in qmake's Advanced Usage and do the file name processing yourself and feed the result to moc.

    Hope it helps



  • Hi @convexbytes !

    This is something I do... a lot. However, I break my project into libraries (both shared and static) and use common names all the time. I have not had problems so far.

    Now, what I do have problems with is QtCreate can get confused sometime on the correct file to reference in the debugger and code completion. This is much better in 4.9 if I am patient and wait a moment.

    My typical usage is in plugins. I am a creature of habit. My plugin main interface is typically called PluginMain. Resides in PluginMain.cpp/PluginMain.h. May or may not reside in different namespaces. I have never had a problem building my projects.

    The MOC generator typically uses the namespace in the name mangling. Here is a small sample (my namespace in this case is 'i3k'):
    /****************************************************************************
    ** Meta object code from reading C++ file 'PositionView.h'
    **
    ** Created by: The Qt Meta Object Compiler version 67 (Qt 5.12.3)
    **
    ** WARNING! All changes made in this file will be lost!
    *****************************************************************************/

    #include "../../../../../src/display/PositionView.h"
    #include <QtCore/qbytearray.h>
    #include <QtCore/qmetatype.h>
    #if !defined(Q_MOC_OUTPUT_REVISION)
    #error "The header file 'PositionView.h' doesn't include <QObject>."
    #elif Q_MOC_OUTPUT_REVISION != 67
    #error "This file was generated using the moc from 5.12.3. It"
    #error "cannot be used with the include files from this version of Qt."
    #error "(The moc has changed too much.)"
    #endif

    QT_BEGIN_MOC_NAMESPACE
    QT_WARNING_PUSH
    QT_WARNING_DISABLE_DEPRECATED
    struct qt_meta_stringdata_i3k__PositionView_t {
    QByteArrayData data[8];
    char stringdata0[82];
    };
    #define QT_MOC_LITERAL(idx, ofs, len)
    Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len,
    qptrdiff(offsetof(qt_meta_stringdata_i3k__PositionView_t, stringdata0) + ofs
    - idx * sizeof(QByteArrayData))
    )
    static const qt_meta_stringdata_i3k__PositionView_t qt_meta_stringdata_i3k__PositionView = {
    {
    QT_MOC_LITERAL(0, 0, 17), // "i3k::PositionView"
    QT_MOC_LITERAL(1, 18, 11), // "on_postData"
    QT_MOC_LITERAL(2, 30, 0), // ""
    QT_MOC_LITERAL(3, 31, 8), // "PRawGNSS"
    QT_MOC_LITERAL(4, 40, 3), // "gps"
    QT_MOC_LITERAL(5, 44, 9), // "PPosition"
    QT_MOC_LITERAL(6, 54, 8), // "position"
    QT_MOC_LITERAL(7, 63, 18) // "on_settingsChanged"

    },
    "i3k::PositionView\0on_postData\0\0PRawGNSS\0"
    "gps\0PPosition\0position\0on_settingsChanged"
    

    };
    #undef QT_MOC_LITERAL


Log in to reply