MOC and Qt Creator?



  • Hi All,
    In the most simple application below, in order to use metaObject I derived QObject and I used Q_OBJECT macro
    But Qt Creator does not invoked moc, and as a result the application does not compile .
    The test application has 2 files in Qt Creator

    main().cpp
    @
    #include <QtCore/QCoreApplication>
    #include <QDebug>
    class QObj1 : public QObject {
    Q_OBJECT
    public:
    int i;
    };
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QObj1 o1;
    qDebug() << o1.metaObject()->className();
    qDebug() << o1.objectName();
    }

    @

    qobject2.pro
    @
    QT += core
    QT -= gui
    TARGET = qobject2
    CONFIG += console
    TEMPLATE = app
    SOURCES += main.cpp
    @

    Qt Creator handles moc very well in other applications, but here , moc does not create a moc file and I don’t understand why.
    Thanks in advance for your help
    Gilles

    PS : The compile errors are :
    @
    23:47:28: Running build steps for project qobject2...
    23:47:28: Configuration unchanged, skipping qmake step.
    23:47:28: Starting: "C:\dev\Qt\mingw\bin\mingw32-make.exe"
    C:/dev/Qt/mingw/bin/mingw32-make.exe -f Makefile.Debug
    mingw32-make.exe[1]: Entering directory D:/Source/Cpp/qt5/qobject2-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug' g++ -Wl,-subsystem,console -mthreads -o debug\qobject2.exe debug/main.o -L"c:\dev\Qt\Desktop\Qt\4.8.1\mingw\lib" -lQtCored4 mingw32-make.exe[1]: Leaving directoryD:/Source/Cpp/qt5/qobject2-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug'
    debug/main.o: In function main': D:\Source\Cpp\qt5\qobject2-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug/../qobject2/main.cpp:15: undefined reference toQObj1::metaObject() const'
    debug/main.o: In function QObj1': D:\Source\Cpp\qt5\qobject2-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug/../qobject2/main.cpp:4: undefined reference tovtable for QObj1'
    debug/main.o: In function ~QObj1': D:\Source\Cpp\qt5\qobject2-build-desktop-Qt_4_8_1_for_Desktop_-_MinGW__Qt_SDK__Debug/../qobject2/main.cpp:4: undefined reference tovtable for QObj1'
    collect2: ld returned 1 exit status
    mingw32-make.exe[1]: *** [debug\qobject2.exe] Error 1
    mingw32-make.exe: *** [debug] Error 2
    23:47:29: The process "C:\dev\Qt\mingw\bin\mingw32-make.exe" exited with code 2.
    Error while building project qobject2 (target: Desktop)
    When executing build step 'Make'
    @



  • Qmake ensures Moc is run on:

    • Files listed in the HEADERS variable. Qmake arranges to compile and link the resulting C++ implementation file by adding it to SOURCES.
    • Files listed in the SOURCES or OBJECTIVE_SOURCES variables are processed by moc. Qmake does not link any potential output, so the resulting moc output must be #included.

    main.cpp
    @
    #include <QDebug>
    class QObj1 : public QObject {
    Q_OBJECT
    public:
    int i;
    };
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QObj1 o1;
    qDebug() << o1.metaObject()->className();
    qDebug() << o1.objectName();
    }
    #include "main.moc"
    @

    As always, if you add or remove Q_OBJECT macros you must rerun qmake.

    BTW: Nothing to do with Qt Creator except that it is running qmake on your behalf



  • Thanks Chris. It worked.
    I am not 100% it's related to metaObject, but in the corrected code below I can’t display objectName() (which seems to be a dynamic property) and I remember being able to display it in other applications, though not each time. So far, I have never figured out what is missing when it does not display. I hope you can help me to pinpoint it on this simple example.
    QObject spec says that "By default, this property contains an empty string" but here i do have metaObject up and running.
    qobject2.pro
    @
    QT += core
    QT -= gui
    TARGET = qobject2
    CONFIG += console
    TEMPLATE = app
    SOURCES += main.cpp
    QObj1.cpp
    HEADERS +=
    @
    QObj1.h
    @
    #ifndef QOBJ1_H
    #define QOBJ1_H
    #include <QtCore/QCoreApplication>
    class QObj1 : public QObject {
    Q_OBJECT
    public:
    QObj1();
    int i;
    };
    #endif // QOBJ1_H
    @

    QObj1.cpp
    @
    #include "QObj1.h"
    QObj1::QObj1()
    {
    }
    @

    main.cpp
    @
    #include <QtCore/QCoreApplication>
    #include <QDebug>
    #include "QObj1.h"
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    QObj1 o1;
    QObj1 *p1 = new QObj1;
    qDebug() << o1.metaObject()->className();
    qDebug() << o1.objectName();
    qDebug() << p1->objectName();
    }
    @

    output
    @
    QObj1
    “”
    “”
    @


  • Moderators

    You need to set the objectName property if you want to read it later. This is not done automatically.



  • You are right, it works much better with :
    @
    o1.setObjectName("op1");
    @
    I guess it's time for me to do deeper reading on the priority system. Here i mixed up the object name and the instance name.
    Thanks again Sierdzio and Chris.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.