简体   繁体   中英

Inheritance of serial port class Qt C++

I want to add some functionality to the serialport class and therefore want to inherit it and add som functions. However, I got problems. I put the class in a header file like this:

class mySerialport : public QSerialPort
{

    public:

    void mySerialport(): QSerialPort(QObject*)
    {


    }


};

I'm modifying the Terminal example: http://qt-project.org/doc/qt-5.1/qtserialport/terminal-mainwindow-cpp.html

Here a serialport object is created in the MainWindow constructor by

serial = new QSerialPort(this);

However, after declaring mySerialport and trying

serial = new MySerialport(this); 

I get nothing but a myriad of error messages regarding the constructor.

Questions:
1. What could the error be? I guess it's basic. 2. Why is the serialport ineheriting the MainWindow? Is it the Qt thing that the serialport than will be deleted when the MainWindow destructor is called?

  1. What could the error be? I guess it's basic.

Replace this line:

void mySerialport(): QSerialPort(QObject*)

with this:

explicit mySerialport(QObject *parent): QSerialPort(parent)

You could also consider composition instead of inheritance based on your exact use case. You would also need to have the Q_OBJECT macro, as well as the source and header files added in your qmake project file.

You would be writing something like this:

myserialport.h

class MySerialport : public QSerialPort
{
    Q_OBJECT
    public:
        explicit MySerialPort(QObject *parent);
        ~MySerialPort();
    ...
};

myserialport.cpp

...
MySerialPort::MySerialPort(QObject *parent)
    : QSerialPort(parent)
{
    ...
}

MySerialPort::~MySerialPort()
{
    ...
}

...

main.pro

...

HEADERS += \
    myserialport.h \
    ...

SOURCES += \
    myserialport.cpp \
    ...

...

You would also need to have the Q_OBJECT macro, as well as the source and header files added in your qmake project file.

  1. Why is the serialport ineheriting the MainWindow?

Our example does not seem to have constructed that way.

Also, do not take the terminal as a good example. I am currently working on a QML terminal example which will be cleaner.

I expect it to be pushed soon against gerrit, and I will share the url later in this post when that is ready.

Is it the Qt thing that the serialport than will be deleted when the MainWindow destructor is called?

No, in fact, QtSerialPort is a core functionality, or you could say "headless". I have written several command line based examples , like sync and async reader and writers. You can check it out in the examples folder of the project.

  1. You should definitely learn the C++ basics like pointers, references, inheritance. You should pass the parent QObject to the mySerialport constructor and redirect it to the QSerialPort constructor.
  2. The serialport is not inheriting the MainWindow, it's just a "Qt way" child of the MainWindow. Qt parent-child relationships have nothing to do with inheritance. Yes, it seems you got it: serial will be deleted with its parent — MainWindow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM