SIGNAL / SLOT in QCoreApplication

  • Hi,
    i did a single file (main() + a class) QCoreApplication,
    i m not able to fix below error .. i feel it is about moc.

    QObject::connect(task,&Task::end,// error here

    please help me to understand and fix this

    C:\..\main.cpp:30: erreur : undefined reference to `Task::end()'
    C:\...\debug\main.o:-1: In function `ZN4TaskC1EP7QObject':
    C:\...\main.cpp:10: erreur : undefined reference to `vtable for Task'
    C:\...\main.o:-1: In function `ZN7QObject7connectIM4TaskFvvEPFvvEEENSt9enable_ifIXaagecvisrN9QtPrivate15FunctionPointerIT0_EE13ArgumentCountLi0EntsrSA_25IsPointerToMemberFunctionEN11QMetaObject10ConnectionEE4typeEPKNS8_IT_E6ObjectESF_PKS_S9_N2Qt14ConnectionTypeE':
    C:\Qt\5.10.0\mingw53_32\include\QtCore\qobject.h:299: erreur : undefined reference to `Task::staticMetaObject'
    #include <QCoreApplication>
    #include <QtCore>
    #include <QTcpSocket>
    #include <QHostAddress>
    class Task : public QObject
        Task(QObject *parent = 0) : QObject(parent) {}
    signals :
        void end();
    public slots:
        void run()
            emit end();
    int main(int argc, char *argv[])
        QCoreApplication a(argc, argv);
        Task *task = new Task(&a);
            qDebug()<< "done.";
        QTimer::singleShot(0, task, SLOT(run()));
        return a.exec();

  • Moderators

    Adding a Q_OBJECT macro to a class means it needs to go through the moc to generate the meta information for that class (the Task::staticMetaObject compiler complains about missing). The problem here is that by default moc is not run on cpp files, only on headers.
    The easiest way to fix your problem is to put the Task class in a proper header file, as it should be anyway.

  • Lifetime Qt Champion


    To add to @Chris-Kawa, the other technic here would be to add

    #include "main.moc"

    At the bottom of your main.cpp file.

  • Thank you very much

Log in to reply