PDA

View Full Version : connecting signal from thread to slot in a cpp file



prkhr4u
17th February 2014, 05:09
I am trying to connect a signal from a thread to a slot in a cpp file.
I am able to successfully connect it to Main Window slot,but not through a cpp file.
Here is what I am doing
//abc.h

#ifndef ABC_H
#define ABC_H
#include <QObject>

class abc : public QObject
{
Q_OBJECT
public:
explicit abc(QObject *parent = 0);
public slots:
void AbcInvokeSlot();
};

#endif // ABC_H
//abc.cpp


#include "abc.h"
abc::abc(QObject *parent) :
QObject(parent)
{
}
void abc :: AbcInvokeSlot()
{
//some code
}

//main.cpp


QApplication app(argc, argv);
MainWindow win;
abc a;
win.show();
SThread sthread;
QObject::connect( & sthread, SIGNAL( SInSignal() ),& win, SLOT( SOutSlot() ) ); //correctly working
QObject::connect( & sthread, SIGNAL( SInSignal() ),& a, SLOT( AbcInvokeSlot() ) ); //not working
sthread.start();
return app.exec();

I am getting the error :

undefined reference to 'vtable for abc'

I tried changing

abc a;
to

abc a=new abc;

then I am getting this error:

no matching function for call to 'QObject::connect(SThread*, const char*, abc**, const char*)'

May I know what I am doing wrong and what is the correct procedure??

ChrisW67
17th February 2014, 08:32
Rerun qmake and rebuild your program.

anda_skoa
17th February 2014, 08:49
As ChrisW67 said, run qmake again.

Your code is fine, the error is caused by adding Q_OBJECT to a header after the header has been added to the project file and the related qmake run.

Classes with Q_OBJECT need to be processed by MOC and these processing instructions are created by qmake.
If you add Q_OBJECT after a header file has been "seen" by qmake, then certain bit will be missing at link time.

Rerunning qmake (and completing the hew build) solves that.

Cheers,
_

prkhr4u
17th February 2014, 10:19
Thank you for the solution and explanation.It exactly solved my problem!!