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. Q_OBJECT in cpp file
Forum Updated to NodeBB v4.3 + New Features

Q_OBJECT in cpp file

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 16.1k Views 1 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.
  • H Offline
    H Offline
    hungnd
    wrote on last edited by
    #1

    Hi

    I have a problem with the macro Q_OBJECT. My source code:
    @
    // myclass.h
    #ifndef MYCLASS_H
    #define MYCLASS_H

    #include <QObject>
    #include <QtCore>

    class MyClassPrivate;

    class MyClass : public QObject
    {
    Q_OBJECT
    Q_DECLARE_PRIVATE(MyClass)
    public:
    explicit MyClass(QObject *parent = 0);

    signals:

    public slots:

    private:
    void* const d_ptr;
    };

    #endif // MYCLASS_H
    @
    @
    // myclass.cpp
    #include "myclass.h"

    class MyClassPrivate: public QObject {
    Q_OBJECT
    public:
    MyClassPrivate():QObject(NULL){}
    signals:
    public slots:
    };

    MyClass::MyClass(QObject *parent) :
    QObject(parent),
    d_ptr(new MyClassPrivate())
    {
    }
    @

    when compiling, I got error "undefined reference to `vtable for MyClassPrivate'". But if I comment the Q_OBJECT declaration inside MyClassPrivate then compilation successful. Could anyone tell me why? And what if I don't use Q_OBJECT in MyClassPrivate?
    Thanks for help!

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      You need to moc that .cpp as well. The usual trick is using a #include "myclass.moc" (or "moc_myclass.cpp") at the end of the .cpp, then rerun qmake. May I ask why do you need a private that inherits from QObject?

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hungnd
        wrote on last edited by
        #3

        Thanks for quick help peppe! It worked. I would want to use signal/slot mechanism whatever it might be unnecessary :D, but I would want to try if sometime I want to use derivations of QObject inside cpp files.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          What I usually do, it not moc the cpp file, but just create a myclass_p.h file.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hungnd
            wrote on last edited by
            #5

            yep, I used to do that

            1 Reply Last reply
            0
            • H Offline
              H Offline
              heiopei
              wrote on last edited by
              #6

              Sry, I have the same problem and I don't get it how to include the moc file. Could you give me a short example how to do it? Would be nice.

              Edit:
              Okay nevermind...I just have to include "moc_myclass.cpp". First I tried modul1g.moc and failed. I was wondering why the compiler didn't find moc_myclass.cpp, so I didn't try this.

              1 Reply Last reply
              0

              • Login

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