简体   繁体   English

串口类Qt C ++的继承

[英]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. 我想为serialport类添加一些功能,因此希望继承它并添加som函数。 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 我正在修改终端示例: http//qt-project.org/doc/qt-5.1/qtserialport/terminal-mainwindow-cpp.html

Here a serialport object is created in the MainWindow constructor by 这里在MainWindow构造函数中创建一个serialport对象

serial = new QSerialPort(this);

However, after declaring mySerialport and trying 然而,在声明mySerialport并尝试之后

serial = new MySerialport(this); 

I get nothing but a myriad of error messages regarding the constructor. 我得到的只是关于构造函数的无数错误消息。

Questions: 问题:
1. What could the error be? 1.错误是什么? I guess it's basic. 我想这是基本的。 2. Why is the serialport ineheriting the MainWindow? 2.为什么序列端口不能使用MainWindow? Is it the Qt thing that the serialport than will be deleted when the MainWindow destructor is called? 在调用MainWindow析构函数时,串口是否会被删除?

  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. 您还需要具有Q_OBJECT宏,以及qmake项目文件中添加的源文件和头文件。

You would be writing something like this: 你会写这样的东西:

myserialport.h myserialport.h

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

myserialport.cpp myserialport.cpp

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

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

...

main.pro 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. 您还需要具有Q_OBJECT宏,以及qmake项目文件中添加的源文件和头文件。

  1. Why is the serialport ineheriting the MainWindow? 为什么序列端口不能使用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. 我目前正在研究一个更清洁的QML终端示例。

I expect it to be pushed soon against gerrit, and I will share the url later in this post when that is ready. 我希望很快就能推出针对gerrit的内容,我会在这篇文章的后期分享这个网址。

Is it the Qt thing that the serialport than will be deleted when the MainWindow destructor is called? 在调用MainWindow析构函数时,串口是否会被删除?

No, in fact, QtSerialPort is a core functionality, or you could say "headless". 不,实际上,QtSerialPort是核心功能,或者你可以说“无头”。 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. 您可以在项目的examples文件夹中查看它。

  1. You should definitely learn the C++ basics like pointers, references, inheritance. 你一定要学习像指针,引用,继承这样的C ++基础知识。 You should pass the parent QObject to the mySerialport constructor and redirect it to the QSerialPort constructor. 您应该将父QObject传递给mySerialport构造函数并将其重定向到QSerialPort构造函数。
  2. The serialport is not inheriting the MainWindow, it's just a "Qt way" child of the MainWindow. serialport不是继承MainWindow,它只是MainWindow的“Qt way”子节点。 Qt parent-child relationships have nothing to do with inheritance. Qt父子关系与继承无关。 Yes, it seems you got it: serial will be deleted with its parent — MainWindow. 是的,似乎你得到了它:serial将被删除它的父 - MainWindow。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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