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. Use same class name in different namespaces for QObject derived classes
Forum Updated to NodeBB v4.3 + New Features

Use same class name in different namespaces for QObject derived classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.5k 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.
  • C Offline
    C Offline
    convexbytes
    wrote on 29 Apr 2019, 22:04 last edited by convexbytes
    #1

    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.

    1 Reply Last reply
    1
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 30 Apr 2019, 05:52 last edited by
      #2

      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.

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 30 Apr 2019, 19:53 last edited by
        #3

        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

        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
        5
        • B Offline
          B Offline
          Buckwheat
          wrote on 3 May 2019, 15:57 last edited by
          #4

          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

          Dave Fileccia

          1 Reply Last reply
          1

          1/4

          29 Apr 2019, 22:04

          • Login

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