qfedu-qt-level/Project02/serialportdebuggingwindow.cpp

122 lines
3.8 KiB
C++
Raw Permalink Normal View History

2023-08-13 11:47:00 +08:00
#include "serialportdebuggingwindow.h"
#include "ui_serialportdebuggingwindow.h"
#include <QDebug>
SerialPortDebuggingWindow::SerialPortDebuggingWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::SerialPortDebuggingWindow)
{
ui->setupUi(this);
// 初始化串口调试窗
initSPDW();
}
SerialPortDebuggingWindow::~SerialPortDebuggingWindow()
{
delete ui;
}
void SerialPortDebuggingWindow::initSPDW()
{
initMenuBar(); // 初始化菜单栏
initToolBar(); // 初始化工具栏
initPortShow(); // 初始化串口设置栏
}
void SerialPortDebuggingWindow::initMenuBar()
{
// 关于
connect(ui->actionabout,&QAction::triggered,[&](){
QMessageBox::about(this,"关于","这是liubo写的Qt的练习项目");
});
// 退出
connect(ui->actionexitAction,&QAction::triggered,[&](){
qDebug() <<"退出";
// emit close_app();
QApplication::quit();
});
}
void SerialPortDebuggingWindow::initToolBar()
{
open_action = new QAction(this);
stop_action = new QAction(this);
add_action = new QAction(this);
time_action = new QAction(this);
loadfile_action = new QAction(this);
write2file_action = new QAction(this);
save_action = new QAction(this);
clearbytes_action = new QAction(this);
cleanport_action = new QAction(this);
msg_action = new QAction(this);
open_action->setIcon(QIcon(":open"));
stop_action->setIcon(QIcon(":stop"));
add_action->setIcon(QIcon(":add"));
time_action->setIcon(QIcon(":time"));
loadfile_action->setIcon(QIcon(":loadfile"));
write2file_action->setIcon(QIcon(":write2file"));
save_action->setIcon(QIcon(":save"));
clearbytes_action->setIcon(QIcon(":clearbytes"));
cleanport_action->setIcon(QIcon(":cleanport"));
msg_action->setIcon(QIcon(":msg"));
tool_bar = new QToolBar(this);
tool_bar->addAction(open_action);
tool_bar->addAction(stop_action);
tool_bar->addSeparator(); // 分隔线
tool_bar->addAction(add_action);
tool_bar->addAction(time_action);
tool_bar->addAction(loadfile_action);
tool_bar->addAction(write2file_action);
tool_bar->addAction(save_action);
tool_bar->addSeparator();
tool_bar->addAction(clearbytes_action);
tool_bar->addAction(cleanport_action);
tool_bar->addSeparator();
tool_bar->addAction(msg_action);
tool_bar->setMovable(false); // 禁止移动
// tool_bar->setFloatable(true);
tool_bar->setAllowedAreas(Qt::TopToolBarArea);
tool_bar->setFixedHeight(40);
this->addToolBar(tool_bar); // 将工具栏添加到当前 Window
// 绑定工具行为
connect(msg_action,&QAction::triggered,[&](){
QMessageBox::about(this,"关于","这是liubo写的Qt的练习项目");
});
}
void SerialPortDebuggingWindow::initPortShow()
{
// 添加串口并显示当前串口
qDebug() <<"可用串口信息"<<endl;
ui->port_select_combo_box->clear();
QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
for(int i = 0; i<ports.size();i++){
QSerialPortInfo info = ports.at(i);
qDebug() << info.portName();
ui->port_select_combo_box->addItem(info.portName());
}
// if(ports.size()!=0){ // 当串口不为空时,至少存在一个串口
// QSerialPortInfo portid = ports.at(0); // 获取第一个串口
// ui->port_select_combo_box->setCurrentText(portid.portName());
// }
// 添加波特率
qDebug() <<"可用波特率信息"<<endl;
ui->rates_check_box->clear(); // 清空可选下拉框
QList<qint32> rates = QSerialPortInfo::standardBaudRates();
for(int i=0;i<rates.size();i++){
qint32 rate_item = rates.at(i);
qDebug()<<rate_item;
ui->rates_check_box->addItem(QString::number(rate_item));
}
}