PDA

View Full Version : how to make my Producer n Consumer class inherits two classes: QObject & QThread



saman_artorious
13th June 2012, 13:14
I would thank if you tell me how to inherit two classes in qt.
I have several objects, each of which may connect to serial port via producer and consumer.
So, for emission n reception I need to connect the serial object inside producer and consumer classes to the other object outside.

Now, my producer n consumer class look like this:


extern const int DataSize;
extern const int BufferSize;
extern char buffer[100];

extern QWaitCondition bufferNotEmpty;
extern QWaitCondition bufferNotFull;
extern QMutex mutex;

extern int numUsedBytes;

class Producer : public QThread
{
public:
Producer();
void run();

private:

rs485 *rs_485;
};


I tried adding QObject as well, but it prompted errrors.
i did this


#ifndef PRODUCER_H
#define PRODUCER_H

#include <QWaitCondition>
#include <QThread>
#include <QMutex>
#include <QTime>

#include <QSemaphore>
#include "rs485.h"

extern const int DataSize;
extern const int BufferSize;
extern char buffer[100];

extern QWaitCondition bufferNotEmpty;
extern QWaitCondition bufferNotFull;
extern QMutex mutex;

extern int numUsedBytes;

class Producer : public QObject, public QThread
{
Q_OBJECT
public:
explicit Producer(QObject *parent = 0);
void run();

public slots:
void ProducerWritePacket(QByteArray);
private:

rs485 *rs_485;
};




#endif // PRODUCER_H



and here's the error:


‘QObject’ is an ambiguous base of ‘Producer’

^NyAw^
13th June 2012, 13:23
Hi,

Have you readed the QThread doc? QThread inherits QObject so you don't need to inherit from it.

saman_artorious
13th June 2012, 14:13
I have a confusion, several objects in my program may send packet to the serial and wait for the serial to send 1 or 2 packet back to them.
I don't know, how to write my producer and consumer thread so that no conflicts occur.

when the threads start running in main, how can one object send a packet to them via signal, so that the first thread can send it to serial while the sender object waits for the reply.

z80
13th June 2012, 15:04
I'm not sure, I've catched the idea but if it is about synchronizing several objects then the right solution is QSemaphore.

^NyAw^
13th June 2012, 17:14
Hi,

Wich kind of object is "rs485"?
If you are using QextSerialPort or QSerialDevice, use SINGAL SLOT mechanism. Connect the SIGNAL "readyRead" to a SLOT and you will get the receiving data in this SLOT.
To send data you can use the main thread. It's not necessary to use threads.
Take a look at QTcpSocket and QUdpSocket examples as the serial port works in the same way.

If it's not a Qt object you can use a timer with a properly set time to catch the baud rate and read the port in the SLOT.

saman_artorious
14th June 2012, 09:35
thanks anyway