What does the Q_OBJECT macro do ? Why do all Qt objects need this macro ?

What does the Q_OBJECT macro do ? Why do all Qt objects need this macro ?

Asked on December 16, 2018 in Qt.
Add Comment


  • 3 Answer(s)

    In this link Qt documentation:

                 The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.

    Answered on December 16, 2018.
    Add Comment

    Here the simple solutions:

                     It simply tells the pre-compiler that  class has gui elements and needs to be run through the ‘moc’ only need to add this to classes that use the signal mechanism.
    But it will be quietly ignored in any other classes.

    Answered on December 16, 2018.
    Add Comment

    Good solution:

                   The MOC (meta object compiler) converts the Q_OBJECT macro included header files in to C++ equivalent source code. It basically controls the signal-slot mechanism, and makes it understandable to the C++ compiler

    Answered on December 16, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.