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. When is time to include "moc_XXX.cpp" at bottom of a cpp file
Forum Updated to NodeBB v4.3 + New Features

When is time to include "moc_XXX.cpp" at bottom of a cpp file

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.7k Views 2 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.
  • jiancaiyangJ Offline
    jiancaiyangJ Offline
    jiancaiyang
    wrote on last edited by
    #1

    When developing Qt in depth, We always encounter problems on moc and virtual table. C++ provide us such complicated infrastructures: multi-inheritance, polymorphic, virtual inheritance, etc. And Qt provide us a bunch of more key words: signal/slots, moc, which confuses us a lot.

    In my practice, some bug may be hidden, the program crashes with meaningless debug informations. I followed the article on "Debugging Techniques", the last topic, I added "moc_XXXX.cpp" at bottom of each cpp file, the problem disappears. But who can give use a summary in which scenarios do we need to include the line there? Thanks in advance.

    我们自己的论坛:http://qtdream.com
    擅长三维角色仿真动画。

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      short answer: if you have Q_OBJECT in a .cpp file then you have to #include the moc_ file

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      jiancaiyangJ 1 Reply Last reply
      3
      • VRoninV VRonin

        short answer: if you have Q_OBJECT in a .cpp file then you have to #include the moc_ file

        jiancaiyangJ Offline
        jiancaiyangJ Offline
        jiancaiyang
        wrote on last edited by
        #3

        @VRonin Oh, maybe in my practice, some class does follow Qt's role that Q_OBJECT only exist in header file.

        我们自己的论坛:http://qtdream.com
        擅长三维角色仿真动画。

        JKSHJ 1 Reply Last reply
        0
        • jiancaiyangJ Offline
          jiancaiyangJ Offline
          jiancaiyang
          wrote on last edited by
          #4

          But I have some experience that when missing "moc_XXX.cpp", even though no Q_OBJECT is present in header file, the compiler is missing essential information that could produce incorrect program at the time. adding this line could prevent the confusing problems from happending, this is especially for Qt plugins.

          我们自己的论坛:http://qtdream.com
          擅长三维角色仿真动画。

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            Hi
            the #include "moc_XXXX.cpp" at bottom of cpp file is to get around the fact
            that moc tools only runs on .h files.
            So often seen in main.cpp examples where no main.h was created.

            I have made a few Designer plugins for Creator and never had a issue where i had to include the
            moc_ (in .cpp) to get it to link.

            Do you have a sample where even with a .h file but not Q_OBJECT in class, you had
            to include it ?

            1 Reply Last reply
            2
            • jiancaiyangJ jiancaiyang

              @VRonin Oh, maybe in my practice, some class does follow Qt's role that Q_OBJECT only exist in header file.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              In my projects, I follow these rules:

              1. If I create a new class that inherits QObject (directly or indirectly), I always put the definition in a .h file and not a .cpp file.
              2. If I create a new class that inherits QObject (directly or indirectly), I always give it the Q_OBJECT macro even if it doesn't use any special moc-related features.

              As a result, I never need to include moc_XXX.cpp

              Also, when I make major changes to my code, I also delete my build folder to remove stale compiler outputs.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              4

              • Login

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