1
votes

I want to create SerialPort class, and it can receive msg automatic and then emit a signal.

but when I compile it show the error message:

error: 'QObject' is an ambiguous base of 'SerialPort' QObject::connect(&uartObj, SIGNAL(readDone(QByteArray)), this, SLOT(hdlRxDone(QByteArray)));

have someone can help me to solve it ? thanks.

#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <QObject>
#include <QSerialPort>
#include <QThread>

class SerialPort : public QSerialPort, public QThread
{
    Q_OBJECT
public:
    explicit SerialPort(QObject *parent = 0);
    ~SerialPort();
    void stop();

signals:
    void readDone(QByteArray data);

public slots:

private:
    void run();
};

#endif // SERIALPORT_H
1

1 Answers

0
votes

Both QSerialPort and QThread derived from QObject, so in your code you have multiple inheritance from QObject, which is forbidden in Qt. You should derive only from QThread, but it is not very good way. The best way is to create simple Worker class derived from QObject, which HAS QSerialPort, and move this class to some thread with moveToThread().

You can find more information about correct usage here or in the documentation.

Full example about serial port in separate thread you can find here, it is in russian language, but you need only code which is not very complex.