[英]Inheritance of serial port class Qt C++
我想為serialport類添加一些功能,因此希望繼承它並添加som函數。 但是,我遇到了問題。 我把這個類放在這樣的頭文件中:
class mySerialport : public QSerialPort
{
public:
void mySerialport(): QSerialPort(QObject*)
{
}
};
我正在修改終端示例: http : //qt-project.org/doc/qt-5.1/qtserialport/terminal-mainwindow-cpp.html
這里在MainWindow構造函數中創建一個serialport對象
serial = new QSerialPort(this);
然而,在聲明mySerialport並嘗試之后
serial = new MySerialport(this);
我得到的只是關於構造函數的無數錯誤消息。
問題:
1.錯誤是什么? 我想這是基本的。 2.為什么序列端口不能使用MainWindow? 在調用MainWindow析構函數時,串口是否會被刪除?
- 錯誤是什么? 我想這是基本的。
替換此行:
void mySerialport(): QSerialPort(QObject*)
有了這個:
explicit mySerialport(QObject *parent): QSerialPort(parent)
您還可以根據您的確切用例考慮組合而不是繼承。 您還需要具有Q_OBJECT宏,以及qmake項目文件中添加的源文件和頭文件。
你會寫這樣的東西:
class MySerialport : public QSerialPort
{
Q_OBJECT
public:
explicit MySerialPort(QObject *parent);
~MySerialPort();
...
};
...
MySerialPort::MySerialPort(QObject *parent)
: QSerialPort(parent)
{
...
}
MySerialPort::~MySerialPort()
{
...
}
...
...
HEADERS += \
myserialport.h \
...
SOURCES += \
myserialport.cpp \
...
...
您還需要具有Q_OBJECT宏,以及qmake項目文件中添加的源文件和頭文件。
- 為什么序列端口不能使用MainWindow?
我們的例子似乎沒有這樣構建。
另外,不要把終端作為一個很好的例子。 我目前正在研究一個更清潔的QML終端示例。
我希望很快就能推出針對gerrit的內容,我會在這篇文章的后期分享這個網址。
在調用MainWindow析構函數時,串口是否會被刪除?
不,實際上,QtSerialPort是核心功能,或者你可以說“無頭”。 我已經編寫了幾個基於命令行的示例 ,如同步和異步讀取器和編寫器。 您可以在項目的examples文件夾中查看它。
QObject
傳遞給mySerialport
構造函數並將其重定向到QSerialPort
構造函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.