PDA

View Full Version : QThread class does emit signal



saman_artorious
14th June 2012, 13:55
I get error when I want to emit a signal from inside of my consumer class, here the code:



#ifndef CONSUMER_H
#define CONSUMER_H

#include <QSemaphore>
#include <QQueue>

extern QSemaphore freeBytes;
extern QSemaphore usedBytes;
extern QQueue<QByteArray> queue;

class Consumer : public QThread
{
public:
void run();
signals:
void sendPackToAllComponents(QByteArray );
};

void Consumer::run()
{
for(;;)
{
if(queue.size() > 0)
{
usedBytes.acquire();
QByteArray pack = queue.dequeue();
emit sendPackToAllComponents(pack);
freeBytes.release();

qDebug() << "pack=" <<pack;

}
}
}
#endif // CONSUMER_H


And here's how I connected the signal to the slot of another object inside main()



QCoreApplication a(argc, argv);
Consumer *consumer = new Consumer();
rs485 *rs_485 = new rs485();
exhaustSystem *MyExhaust = new exhaustSystem();
QByteArray packet;
packet.append(0x04);
consumer->start();
QObject::connect(MyExhaust, SIGNAL(EXHAUST_SendToRS485(QByteArray)), rs_485, SLOT(rs485DataAquisition(QByteArray)));
QObject::connect(consumer, SIGNAL(sendPackToAllComponents(QByteArray)),MyExha ust, SLOT(EXHAUST_DataReceive(QByteArray)));

rs_485->rs485DataAquisition(packet);


This is the exhaust class where it's SLOT is connected to the consumer thread SendTOALLOBJECT SIGNAL:


class exhaustSystem : public QObject
{
Q_OBJECT
public:
explicit exhaustSystem(QObject *parent = 0);

signals:
void EXHAUST_SendToRS485(QByteArray );
void stop_diesel();
public slots:

void EXHAUST_DataReceive(QByteArray);
.....

wysota
14th June 2012, 14:21
You forgot to tell us what the error is.

^NyAw^
14th June 2012, 15:10
Hi,

Your Consumer class needs the Q_OBJECT macro:



class Consumer : public QThread
{
Q_OBJECT
...
}