Linking error with slots containing non-qt types
-
I'm using Qt 5.2.1 on Visual Studio 2010 with Qt Vs Add-in 1.2.3-alpha. Whenever I try to compile a class which have slots/signals referring to non-qt types, I get linking error. I setup my project like this:
mydata.h
@#pragma once
#include <QtCore/QMetaType>
struct MyData
{
int a;
int b;
};Q_DECLARE_METATYPE(MyData);@
testslot.h
@#pragma once
#include <QtCore/QObject>
class MyData;
class TestSlot : public QObject
{
Q_OBJECT
public:
TestSlot(void);
~TestSlot(void);public slots:
void aSlot(const MyData& data);
};@testslot.cpp
@#include "testslot.h"
#include <QtCore/QMetaType>
#include "mydata.h"
TestSlot::TestSlot(void)
{
qRegisterMetaType<MyData>("MyData");
}TestSlot::~TestSlot(void)
{
}void TestSlot::aSlot( const MyData& data )
{
data.a;
}@main.cpp
@
#include <QtCore/QCoreApplication>
#include <QtCore/QMetaType>
#include "testslot.h"int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TestSlot t;
return a.exec();
}@Error from the linker:
@Error 1 error LNK2019: unresolved external symbol "public: void __thiscall TestSlot::aSlot(class MyData const &)" (?aSlot@TestSlot@@QAEXABVMyData@@@Z) referenced in function "private: static void __cdecl TestSlot::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestSlot@@CAXPAVQObject@@W4Call@QMetaObject@@HPAPAX@Z) D:\Visual Studio\Projects\Qt\Test\Test\moc_testslot.obj@ -
Hi,
your code works for me (MinGW)!!.
The only difference is that I not used #pragma once directive but the standard guards@
#ifndef MY_FILE_H
#define MY_FILE_H....
#endif // MY_FILE_H
@Anyway i think it's better to use
@qRegisterMetaType<MyData>("MyData");@outside TestSlot implementation (for example in main)
@
#include <QCoreApplication>#include "TestSlot.h"
#include "MyData.h"int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);qRegisterMetaType<MyData>("MyData"); TestSlot t; return a.exec();
}
@UPDATE: reading from Qt Doc "here":http://qt-project.org/doc/qt-5/qmetatype.html#qRegisterMetaType you don't need to use qRegisterMetaType
-
-This problem does not occur with QtCreator nor VS Add-in 1.2.2. There must be a bug in version 1.2.3-alpha.-
Edited: I made a mistake when forward declaring: MyData is defined as a struct, not a class. VS compiler made a clear distinction between these two keywords, that's why the linking error shows up.
Thanks for your answer.