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

when should Generating "moc" files in qt project,and what is the "moc" files's function?



  • I create a new qt project in Mac qt for test the example in :http://doc.qt.io/archives/qt-4.8/signalsandslots.html#signals-and-slots
    for test the 'Signals & Slots', it seem it will generated moc_counter.cpp.o,moc_predefs.h,moc_counter.o "moc" files in the "-Deubg" directory after build,I delete the "moc" files,and I run the project again,everything is work right,
    in my main.cpp,here is the code copy from the example in :http://doc.qt.io/archives/qt-4.8/signalsandslots.html#signals-and-slots
    Counter a,b;
    QObject::connect(&a, SIGNAL(valueChanged(int)),
    &b, SLOT(setValue(int)));
    a.setValue(12);
    cout << "1" + string("a的值") + to_string(a.value()) + " b的值" + to_string(b.value()) << endl;
    b.setValue(48);
    cout << "2" + string("a的值") + to_string(a.value()) + " b的值" + to_string(b.value()) << endl;
    after delete the "moc" files in "-Deubg" directory,I run the project again,I can still get right answer,

    what dose the "moc" files do,and when I need Generating "moc" files,and when should I add the Generating "moc" files to my project to use them?

    in my another qt project,when I run the project,it shows me error"QMetaObject::connectSlotsByName: No matching signal for "
    is it because I lack the "moc" files?

    thanks a lot!


  • Moderators

    moc files are generated by moc tool (Meta Object Compiler) - they are always generated for all QObject classes. They are absolutely required for meta stuff to work, so things like:

    • signals
    • slots
    • Q_INVOKABLE
    • QMeta* classes

    However, these files are generated at build time. You don't need to keep them in your project, same as you don't need to store Makefiles, object files etc.
    Actually, I strongly recommend building your code outside of your source code directory. Qt Creator does this automatically (it's called a "shadow build").

    Other code generators in Qt include:

    • uic (generates UI headers from .ui files)
    • rcc (generates cpp resource files from .qrc files)
    • lupdate/lrelease (for translations)


  • @Princein
    the moc files are generated automatically here are a couple of documents links so you can read about them.
    http://doc.qt.io/archives/qt-4.8/moc.html
    http://doc.qt.io/qt-5/why-moc.html



  • @sierdzio said in when should Generating "moc" files in qt project,and what is the "moc" files's function?:

    moc files are generated by moc tool (Meta Object Compiler) - they are always generated for all QObject classes. They are absolutely required for meta stuff to work, so things like:

    signals
    slots
    Q_INVOKABLE
    QMeta* classes

    However, these files are generated at build time. You don't need to keep them in your project, same as you don't need to store Makefiles, object files etc.
    Actually, I strongly recommend building your code outside of your source code directory. Qt Creator does this automatically (it's called a "shadow build").
    Other code generators in Qt include:

    uic (generates UI headers from .ui files)
    rcc (generates cpp resource files from .qrc files)
    lupdate/lrelease (for translations)

    thank you very much!


  • Banned

    This post is deleted!

Log in to reply