Solved Unresolved external symbol when connecting with new syntax
-
Hello,
I have a class that inherits from QLineEdit to implement clicked signal :
#if defined(LD_LIBRARY) # define LD_DLL Q_DECL_EXPORT #else # define LD_DLL Q_DECL_IMPORT #endif class LdLineEdit: public QLineEdit { Q_OBJECT public: LD_DLL explicit LdLineEdit( QWidget *aParent = nullptr ); signals: LD_DLL void LdClicked(); protected: void mouseReleaseEvent(QMouseEvent *){emit LdClicked();} }
And when I try to connect to this signal with the new syntax :
connect(&mLESerial, &LdLineEdit::LdClicked,this,&PrReportViewer::LESerialTextEdited);
I have this error :
PrReportViewer.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const LdLineEdit::staticMetaObject" (?staticMetaObject@LdLineEdit@@2UQMetaObject@@B)
However, if i use the old syntax :
connect( &mLESerial, SIGNAL( LdClicked() ), this, SLOT( LESerialTextEdited() ) ) ;
it works (it compiles, and its actually connected)
I need the new syntax to plug a Lambda instead of that slot, and it would be nice to understand anyway.
-
What type is
mLESerial
?LdLineEdit
orLdLineEdit*
? -
LdLineEdit
-
@Mwoua
And What is LdLineEdit ? -
Strange error. replace
private slots:
withprotected:
btw, the event is not a slot. Also add a constructor or you'll potentially break parent/child dependency.Last question, what is
LD_DLL
?class LdLineEdit: public QLineEdit { Q_OBJECT Q_DISABLE_COPY(LdLineEdit) public: LdLineEdit(QWidget* parent = Q_NULLPTR) :QLineEdit(parent) {} signals: LD_DLL void LdClicked(); protected: void mouseReleaseEvent(QMouseEvent *){emit LdClicked();} }
-
@Taz742
See first post@VRonin
The constructor is there in my class, I forgot it because I wanted to simplify (I'll edit my first post)The macro is :
#if defined(LD_LIBRARY) # define LD_DLL Q_DECL_EXPORT #else # define LD_DLL Q_DECL_IMPORT #endif
LdLineEdit is in a dll that I use in my other function
Same issue with the function in protected.
EDIT : it might be linked to the dll, if I do this inside a function of LdLineEdit :
connect( this, &LdLineEdit::LdClicked, this, &LdLineEdit::ThisValueChanged);
It compiles (ThisValueChanged is private slot of LdLineEdit)
-
@Mwoua said in Unresolved external symbol when connecting with new syntax:
LdLineEdit is in a dll
BINGO!
you should export the whole classclass LD_DLL LdLineEdit: public QLineEdit { Q_OBJECT Q_DISABLE_COPY(LdLineEdit) public: LdLineEdit(QWidget* parent = Q_NULLPTR) :QLineEdit(parent) {} signals: void LdClicked(); protected: void mouseReleaseEvent(QMouseEvent *){emit LdClicked();} }
-
Export the whole class, not only a handful of methods.
Edit: Crossposted with our Ronin, but it happens. ;)
-
Ok, is there a rule to export a whole class or just a part of it?
-
The rule is you need to export Qt's methods and variables from your class - the ones
Q_OBJECT
introduces (in this case thestaticMetaObject
, see your linker error in the OP), so when working withQObject
s always export the whole class. -
Thanks for the help !
-
No problem. As to why it works with the old syntax - it works because there's no compile time checking and all is done in runtime.