Problem when inheriting from Qwt classes
-
I'm encountering a strange issue with Qwt classes. I'm getting a linker error specifically when inheriting from qwt classes(QwtPlot, QwtDial, maybe more..) like this:
#pragma once #include <qwt_plot.h> #include <qwt_dial.h> #include <qwt_text.h> #include <QWidget> #include <QObject> #include <QLayout> #include <QWidget> class SimpleQwtPlot : public QwtPlot { Q_OBJECT public: SimpleQwtPlot(){ QWidget* centralWidget = new QWidget(); QVBoxLayout* layout = new QVBoxLayout(centralWidget); QwtText plotTitle("QwtPlot Test with QwtText"); plotTitle.setFont(QFont("Arial", 12, QFont::Bold)); plotTitle.setColor(Qt::blue); QwtPlot* plot = new QwtPlot(plotTitle, centralWidget); QwtPlot* plot2 = new QwtPlot(centralWidget); if (plot) { layout->addWidget(plot); plot->setCanvasBackground(Qt::white); plot->setAxisTitle(QwtPlot::xBottom, "X Axis"); plot->setAxisTitle(QwtPlot::yLeft, "Y Axis"); } } };
This throws error :
mocs_compilation_Release.obj : error LNK2019: unresolved external symbol "public: static st
ruct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject@QwtPlot@@2UQMetaObject
@@B) referenced in function "public: static struct QMetaObject const * __cdecl QMetaObject:
:staticMetaObject<&public: static struct QMetaObject const QwtPlot::staticMetaObject>(void)
" (??$staticMetaObject@$1?staticMetaObject@QwtPlot@@2UQMetaObject@@B@QMetaObject@@SAPEBU0@X
Z) [C:\dev\aero\testApp\build\QwtTest.vcxproj]
C:\dev\aero\testApp\build\bin\Release\QwtTest.exe : fatal error LNK1120: 1 unresolved exter
nals [C:\dev\aero\testApp\build\QwtTest.vcxproj]Ok, now you would say, that its propably linking problem, library just not getting linked. But what if i tell you, that this code compiles?:
#pragma once #include <qwt_plot.h> #include <qwt_dial.h> #include <qwt_text.h> #include <QWidget> #include <QObject> #include <QLayout> #include <QWidget> class SimpleQwtPlot : public QWidget { Q_OBJECT public: SimpleQwtPlot(){ QWidget* centralWidget = new QWidget(); QVBoxLayout* layout = new QVBoxLayout(centralWidget); QwtText plotTitle("QwtPlot Test with QwtText"); plotTitle.setFont(QFont("Arial", 12, QFont::Bold)); plotTitle.setColor(Qt::blue); QwtPlot* plot = new QwtPlot(plotTitle, centralWidget); QwtPlot* plot2 = new QwtPlot(centralWidget); if (plot) { layout->addWidget(plot); plot->setCanvasBackground(Qt::white); plot->setAxisTitle(QwtPlot::xBottom, "X Axis"); plot->setAxisTitle(QwtPlot::yLeft, "Y Axis"); } } };
Where is the change? I changed inheriting class from QwtPlot to QWidget. I just dont understand where is the problem. Why the hell it just dont compile with inheriting from QwtPlot, but compiles fine when it inherits from QWidget? Iam compiling in using MSVC, on linux, it worked fine. Iam using Qt 6.7.3 and Qwt 6.3.0. Iam helpless, would really appreciate any help. Thanks.
-
Ok, so if someone enounter similiar problem:
I resolved it removing Q_OBJECT macro.But could someone explain why is that? Why it didnt compile on windows using MSVC, but it did compile on linux using gcc? Why is Q_OBJECT causing the issue?
-
@patrik777 I guess you did not include the generated moc file in your cpp file? See https://doc.qt.io/qt-6/moc.html
1/4