2

I have QThread inherited class and when i add a Q_OBJECT macro, it gives unresolved external error. (I've included QObject and QThread).

class TCustomThread: public QThread { Q_OBJECT public: explicit TCustomThread(QObject *parent=0); public slots: void testSlot(); } 

How can i solve this issue?

7
  • 1
    Are you planning on re-writing how Qt manages threads? If not, and you're inheriting from QThread, you're doing it wrong: blog.qt.digia.com/blog/2010/06/17/youre-doing-it-wrong Commented Dec 4, 2013 at 16:07
  • If you're going to use QThread, you should read this: mayaposch.wordpress.com/2011/11/01/… Commented Dec 4, 2013 at 16:08
  • I've clean and rebuilt it but it doesn't works :( . It's too weird. Commented Dec 4, 2013 at 16:12
  • I've re-implemented QThread and overrided its "run()" method and it worked like a charm but when i add Q_OBJECT macro it gives me error Commented Dec 4, 2013 at 16:14
  • 1
    Are you using QtCreator? Clean and Rebuild do not run qmake again. Commented Dec 4, 2013 at 16:17

1 Answer 1

3

In this case this error may accrue only if moc tool didn't create meta data for this class. This can happen in two cases:

  • file dependencies are broken so you have to run qmake and/or clean project (make clean).
  • moc tool is not instructed to generate those data, for example you are missing entry in pro file: HEADERS += tcustomthread.h

I'm assuming that you are using pro file, if you are using other build manager you have to just add missing entry respective to this HEADERS += tcustomthread.h.

Sign up to request clarification or add additional context in comments.

2 Comments

if you change the pro file you need to run qmake again
For others encountering this issue, this was caused when I created a new class (.h and .cpp files) outside my project directory and forgot to check "add to project" during creation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.