#QT(串口助手-界面)

小明 2025-05-05 19:47:47 9

1.IDE:QTCreator


2.实验:编写串口助手


3.记录

���收框:Plain Text Edit

属性选择:Combo Box

发送框:Line Edit

广告:Group Box

(1)仿照现有串口助手设计UI界面

 (2)此时串口助手大体UI完成,但是接收框也能输入数据,应该禁止接收框输入数据,将接收框改为只读属性

(3)在可选择的属性框中设置待选择的属性

(4)加入选项后,发现布局有问题

 (5)新的运行效果

(6)修改可选择属性的默认选择 

(7)在pro文件中加入serialport,如果报错为缺少module,那么可以在qt安装文件中,找到 

根据提示add or remove compent ,加入serial port即可,加入serial bus可以以后编写can modbus等上位机软件

(8)加入搜索串口代码 


4.代码

#include "widget.h"
#include "ui_widget.h"
#include "QSerialPortInfo"   //引入必要的serial文件
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QStringList serialNamePort;       //定义一个数组
    foreach (const QSerialPortInfo &info ,QSerialPortInfo::availablePorts()) {      //自动搜索当前串口
        serialNamePortaddItems(serialNamePort);     //将搜索的串口加入UI界面
}
Widget::~Widget()
{
    delete ui;
}

 

 

The End
微信