Results 1 to 6 of 6

Thread: how to make my Producer n Consumer class inherits two classes: QObject & QThread

  1. #1
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default how to make my Producer n Consumer class inherits two classes: QObject & QThread

    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:
    Qt Code:
    1. extern const int DataSize;
    2. extern const int BufferSize;
    3. extern char buffer[100];
    4.  
    5. extern QWaitCondition bufferNotEmpty;
    6. extern QWaitCondition bufferNotFull;
    7. extern QMutex mutex;
    8.  
    9. extern int numUsedBytes;
    10.  
    11. class Producer : public QThread
    12. {
    13. public:
    14. Producer();
    15. void run();
    16.  
    17. private:
    18.  
    19. rs485 *rs_485;
    20. };
    To copy to clipboard, switch view to plain text mode 

    I tried adding QObject as well, but it prompted errrors.
    i did this
    Qt Code:
    1. #ifndef PRODUCER_H
    2. #define PRODUCER_H
    3.  
    4. #include <QWaitCondition>
    5. #include <QThread>
    6. #include <QMutex>
    7. #include <QTime>
    8.  
    9. #include <QSemaphore>
    10. #include "rs485.h"
    11.  
    12. extern const int DataSize;
    13. extern const int BufferSize;
    14. extern char buffer[100];
    15.  
    16. extern QWaitCondition bufferNotEmpty;
    17. extern QWaitCondition bufferNotFull;
    18. extern QMutex mutex;
    19.  
    20. extern int numUsedBytes;
    21.  
    22. class Producer : public QObject, public QThread
    23. {
    24. Q_OBJECT
    25. public:
    26. explicit Producer(QObject *parent = 0);
    27. void run();
    28.  
    29. public slots:
    30. void ProducerWritePacket(QByteArray);
    31. private:
    32.  
    33. rs485 *rs_485;
    34. };
    35.  
    36.  
    37.  
    38.  
    39. #endif // PRODUCER_H
    To copy to clipboard, switch view to plain text mode 

    and here's the error:

    ‘QObject’ is an ambiguous base of ‘Producer’

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to make my Producer n Consumer class inherits two classes: QObject & QThread

    Hi,

    Have you readed the QThread doc? QThread inherits QObject so you don't need to inherit from it.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to make my Producer n Consumer class inherits two classes: QObject & QThread

    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.

  4. #4
    Join Date
    Feb 2012
    Posts
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to make my Producer n Consumer class inherits two classes: QObject & QThread

    I'm not sure, I've catched the idea but if it is about synchronizing several objects then the right solution is QSemaphore.

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to make my Producer n Consumer class inherits two classes: QObject & QThread

    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.
    Òscar Llarch i Galán

  6. #6
    Join Date
    Feb 2012
    Location
    Armenia/Yerevan
    Posts
    400
    Thanks
    15
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to make my Producer n Consumer class inherits two classes: QObject & QThread

    thanks anyway
    Last edited by saman_artorious; 14th June 2012 at 08:36. Reason: changed [code] to [qtclass]

Similar Threads

  1. Any Qt code/example for producer consumer pattern?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 23rd February 2009, 21:11
  2. Speed up Producer Consumer Threads
    By ^NyAw^ in forum Qt Programming
    Replies: 7
    Last Post: 29th February 2008, 18:38
  3. create a Class inherits from two QObject subclasses
    By sabeesh in forum Qt Programming
    Replies: 17
    Last Post: 31st December 2007, 12:04
  4. Producer Consumer...
    By Shuchi Agrawal in forum Newbie
    Replies: 1
    Last Post: 16th February 2007, 09:45
  5. Producer Consumer
    By ^NyAw^ in forum Qt Programming
    Replies: 16
    Last Post: 17th November 2006, 19:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.