unresolved external symbol "public: virtual struct QMetaObject const *
-
I added this:
class DSSImageToolBar : public QToolBar { Q_OBJECT public: explicit DSSImageToolBar(QWidget* parent) : QToolBar(parent), opacityEffect(this) { } ~DSSImageToolBar() {}; void setOpacity(qreal opacity) { opacityEffect.setOpacity(opacity); setGraphicsEffect(&opacityEffect); update(); }; protected: void enterEvent(QEvent* e) override { setOpacity(1.0); }; void leaveEvent(QEvent* e) override { setOpacity(0.5); }; QGraphicsOpacityEffect opacityEffect; };
to the front of my main.cpp file after the includes.
For my pains I got:
1>main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl DSSImageToolBar::metaObject(void)const " (?metaObject@DSSImageToolBar@@UEBAPEBUQMetaObject@@XZ) 1>main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl DSSImageToolBar::qt_metacast(char const *)" (?qt_metacast@DSSImageToolBar@@UEAAPEAXPEBD@Z) 1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl DSSImageToolBar::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@DSSImageToolBar@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
If I put into a separate header it all works fine, but I didn't want to externalise that class definition.
So why does it fail in the cpp but work in a header?
-
@J-Hilk said in unresolved external symbol "public: virtual struct QMetaObject const *:
@Perdrix this is inside main.cpp?
have you added #include "main.moc" at the end of the file?
At the end of main.cpp? Looking at the generated main.moc file, it seems probable that this would help. What part of the docs would tell me that I needed to do that? I failed to spot anything in the "Using the Meta-Object Compiler" part
@Perdrix said in unresolved external symbol "public: virtual struct QMetaObject const *:
At the end of main.cpp?
yes,
The problem is, that your class definition is located inside a source file, (main.cpp in this case) and that is messing up qmake
take a look at this stack overflow answer, a very good one
https://stackoverflow.com/a/34929627I'm not sure if its explicitly featured in the Meta-Object-Compiler documentation 🤷♂️
edit:
just checked, it is mentioned
https://doc.qt.io/qt-5/moc.html#writing-make-rules-for-invoking-moc
-
When subclassing a QObject, you'd better write .h and .cpp separately.
Or there maybe some "mocing" problems... -
I added this:
class DSSImageToolBar : public QToolBar { Q_OBJECT public: explicit DSSImageToolBar(QWidget* parent) : QToolBar(parent), opacityEffect(this) { } ~DSSImageToolBar() {}; void setOpacity(qreal opacity) { opacityEffect.setOpacity(opacity); setGraphicsEffect(&opacityEffect); update(); }; protected: void enterEvent(QEvent* e) override { setOpacity(1.0); }; void leaveEvent(QEvent* e) override { setOpacity(0.5); }; QGraphicsOpacityEffect opacityEffect; };
to the front of my main.cpp file after the includes.
For my pains I got:
1>main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl DSSImageToolBar::metaObject(void)const " (?metaObject@DSSImageToolBar@@UEBAPEBUQMetaObject@@XZ) 1>main.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl DSSImageToolBar::qt_metacast(char const *)" (?qt_metacast@DSSImageToolBar@@UEAAPEAXPEBD@Z) 1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl DSSImageToolBar::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@DSSImageToolBar@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
If I put into a separate header it all works fine, but I didn't want to externalise that class definition.
So why does it fail in the cpp but work in a header?
-
@Perdrix this is inside main.cpp?
have you added #include "main.moc" at the end of the file?
@J-Hilk said in unresolved external symbol "public: virtual struct QMetaObject const *:
@Perdrix this is inside main.cpp?
have you added #include "main.moc" at the end of the file?
At the end of main.cpp? Looking at the generated main.moc file, it seems probable that this would help. What part of the docs would tell me that I needed to do that? I failed to spot anything in the "Using the Meta-Object Compiler" part
-
@J-Hilk said in unresolved external symbol "public: virtual struct QMetaObject const *:
@Perdrix this is inside main.cpp?
have you added #include "main.moc" at the end of the file?
At the end of main.cpp? Looking at the generated main.moc file, it seems probable that this would help. What part of the docs would tell me that I needed to do that? I failed to spot anything in the "Using the Meta-Object Compiler" part
@Perdrix said in unresolved external symbol "public: virtual struct QMetaObject const *:
At the end of main.cpp?
yes,
The problem is, that your class definition is located inside a source file, (main.cpp in this case) and that is messing up qmake
take a look at this stack overflow answer, a very good one
https://stackoverflow.com/a/34929627I'm not sure if its explicitly featured in the Meta-Object-Compiler documentation 🤷♂️
edit:
just checked, it is mentioned
https://doc.qt.io/qt-5/moc.html#writing-make-rules-for-invoking-moc