ToB企服应用市场:ToB评测及商务社交产业平台
标题:
[QT]qt小项目,使用qcustomplot实时绘制串口波形数据并存储到数据库,可查
[打印本页]
作者:
万有斥力
时间:
2024-7-13 17:28
标题:
[QT]qt小项目,使用qcustomplot实时绘制串口波形数据并存储到数据库,可查
媒介
本项目是基于我已学习到的qt知识,将这些知识整合起来做成的一个练手小项目,作者还是新手处于学习阶段,程序中另有很多能够改进的地方,但我能力不敷有些问题发现不了。或者有些问题在开发中就已经发现了,但是修改代码太麻烦就没有修改(在代码中会指出)。如果你有什么更好的建议可以留在评论区。
界面展示
主界面
设置界面
查询界面
预想中是能显示4路数据,两路ADC数据(其他数据传输方式)和两路串口数据,这里只做了串口通信。图像中的曲线是程序生成的测试数据而非串口数据。
串口数据显示(黄色曲线为串口1接收数据)
这里串口数据显示实在有一个小BUG,之后会提到。
大概框架
说明一下主要成员变量及其所属父对象。
项目团体结构
其中qcustomplot.h和.cpp文件需要去官网下载,请自行百度,或者直接下载我的工程文件即可。
直接上代码
mainwindow.h 主界面头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
#include <QSerialPort>
#include <QSettings>
#include "qcustomplot.h"
#include "setting.h"
#include "search.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QMainWindow>
#define STR(str) #str
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void initMainUi();
void setSerialPort(QSerialPort*);
void initDatabase();
static void setCustomplotStyle(const QString&, QCustomPlot *);
static void setAxisColor(const QString&, QCustomPlot *);
enum FixedAxis{
Graph1FixedAxis = 0x01,
Graph2FixedAxis = 0x02
};
private:
QSerialPort *serialPort1;
QSerialPort *serialPort2;
private slots:
void on_setBtn_clicked();
void on_exitBtn_clicked();
void on_Uart1Enable_clicked(bool checked);
void on_Uart2Enable_clicked(bool checked);
void uart1Recieve();
void uart2Recieve();
void realtimeDataSlot();
void showMainUi();
void on_searchBtn_clicked();
protected:
void showEvent(QShowEvent *event) override;
void closeEvent(QCloseEvent *event) override;
//void paintEvent(QPaintEvent *event) override;
private:
Ui::MainWindow *ui;
QSettings *config;
QCustomPlot *plot_1;
QCustomPlot *plot_2;
QTimer *dataTimer;
double key; //用来记录横坐标的时间刻度
Setting *settingUi;
Search *searchUi;
quint8 isFixedAxisRange;
QSqlDatabase *database;
};
#endif // MAINWINDOW_H
复制代码
mainwindow.cpp 主界面源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
//初始化各成员变量
settingUi = new Setting();
initDatabase(); //必须在实例化Search对象之前初始化数据库
searchUi = new Search(database);
ui->setupUi(this);
plot_1 = ui->customPlot1;
plot_2 = ui->customPlot2;
dataTimer = new QTimer(this);
//打开配置文件
config = new QSettings("config.ini", QSettings::IniFormat);
serialPort1 = new QSerialPort(this);
serialPort2 = new QSerialPort(this);
//连接串口接收槽函数
connect(serialPort1, SIGNAL(readyRead()), this, SLOT(uart1Recieve()));
connect(serialPort2, SIGNAL(readyRead()), this, SLOT(uart2Recieve()));
// 使上下轴、左右轴范围同步
connect(plot_2->xAxis, SIGNAL(rangeChanged(QCPRange)), plot_2->xAxis2, SLOT(setRange(QCPRange)));
connect(plot_2->yAxis, SIGNAL(rangeChanged(QCPRange)), plot_2->yAxis2, SLOT(setRange(QCPRange)));
//定时器连接槽函数realtimeDataSlot
connect(dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
//实现关闭设置界面/查询界面 显示主界面
connect(settingUi, SIGNAL(showMainUi()), this, SLOT(showMainUi()));
connect(searchUi, SIGNAL(showMainUi()), this, SLOT(showMainUi()));
//this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
initMainUi();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initMainUi()
{
//串口数据显示区
//坐标轴使用时间
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
//设置曲线颜色(显示区域2)
plot_2->addGraph();
plot_2->graph(0)->setPen(QPen(QColor("#4fa08b"))); //测试数据
plot_2->addGraph();
plot_2->graph(1)->setPen(QPen(QColor(255, 100, 40))); //串口2
plot_2->xAxis->setTicker(timeTicker);
//四边安上坐标轴
plot_2->axisRect()->setupFullAxesBox();
//设置坐标轴名字
plot_2->xAxis->setLabel("时间/h:m:s");
plot_2->yAxis->setLabel("幅值");
//plot_2->setBackground(QBrush(QColor("#454545")));
//显示区域1
plot_1->addGraph();
plot_1->graph(0)->setPen(QPen(QColor("#4fa08b"))); //测试数据
plot_1->addGraph();
plot_1->graph(1)->setPen(QPen(QColor("#FFFF00"))); //串口1
plot_1->xAxis->setTicker(timeTicker);
plot_1->axisRect()->setupFullAxesBox();
plot_1->xAxis->setLabel("时间/h:m:s");
plot_1->yAxis->setLabel("幅值");
//设置plotstyle,与样式表相匹配
setCustomplotStyle("ManjaroMix", plot_1);
setCustomplotStyle("ManjaroMix", plot_2);
dataTimer->start(20); // 间隔时间 20ms表示刷新率为50hz
}
void MainWindow::setSerialPort(QSerialPort *serialPort)
{
//这一段的作用是根据串口变量名选择不同配置组,配置相应串口参数
QString str = "ConfigData";
if (serialPort == serialPort1)
str = STR(serialPort1) + str;
else if (serialPort == serialPort2)
str = STR(serialPort2) + str;
//qDebug() << str <<endl;
this->config->beginGroup(str);
//设置串口名
serialPort->setPortName(this->config->value("serialName").toString());
//设置波特率
serialPort->setBaudRate(this->config->value("baudrate", 9600).toInt());
//设置校验位
switch(this->config->value("paritybit", 0).toInt())
{
case 0:
serialPort->setParity(QSerialPort::NoParity);
break;
case 1:
serialPort->setParity(QSerialPort::OddParity);
break;
case 2:
serialPort->setParity(QSerialPort::EvenParity);
break;
default:
break;
}
//设置停止位
switch(this->config->value("stopbit", 0).toInt())
{
case 0:
serialPort->setStopBits(QSerialPort::OneStop);
break;
case 1:
serialPort->setStopBits(QSerialPort::OneAndHalfStop);
break;
case 2:
serialPort->setStopBits(QSerialPort::TwoStop);
break;
default:
break;
}
//设置数据位
switch (this->config->value("databit", 8).toInt())
{
case 5:
serialPort->setDataBits(QSerialPort::Data5);
break;
case 6:
serialPort->setDataBits(QSerialPort::Data6);
break;
case 7:
serialPort->setDataBits(QSerialPort::Data7);
break;
case 8:
serialPort->setDataBits(QSerialPort::Data8);
break;
default:
break;
}
this->config->endGroup();
//设置流控为无流控
serialPort->setFlowControl(QSerialPort::NoFlowControl);
}
void MainWindow::initDatabase()
{
database = new QSqlDatabase();
if (QSqlDatabase::contains("qt_sql_default_connection")) //如果目录下已有数据库
{
*database = QSqlDatabase::database("qt_sql_default_connection");
}
else //未有数据库则创建
{
*database = QSqlDatabase::addDatabase("QSQLITE");
database->setDatabaseName("MyDataBase.db");
database->setUserName("L");
database->setPassword("123456");
}
if (!database->open())
{
qDebug() << "Error: Failed to connect database." << database->lastError();
return;
}
QSqlQuery sql_query(*database);
//设置数据库表格式,以横坐标轴数据(X轴:时间s)为主键,包含图像id和Y轴数据,
//这里只演示了存储一条曲线的表格式,可以再添加图像id标识不同曲线,对应不同value值
QString create_sql = "create table graph (timecount double primary key, graphid int, value double);";
//sql_query.prepare(create_sql);
//QString tableExist = "select count(*) from sqlite_master where type='table' and name='graph';";
//创建表
sql_query.exec(create_sql);
sql_query.exec("delete from graph"); //清空表
//qDebug() << "Error: Fail to create table." << sql_query.lastError();
}
void MainWindow::setCustomplotStyle(const QString& style, QCustomPlot *plot)
{
if (style == "ManjaroMix")
{
//坐标轴颜色
MainWindow::setAxisColor("#4fa08b", plot);
//背景颜色
plot->setBackground(QBrush("#353a3e"));
}
else if (style == "") //根据自己需要配置相应主题颜色
{
}
}
void MainWindow::setAxisColor(const QString& color, QCustomPlot *plot)
{
plot->xAxis->setLabelColor(QColor(color));
plot->xAxis->setTickLabelColor(QColor(color));
plot->xAxis->setTickPen(QPen(QColor(color)));
plot->xAxis->setSubTickPen(QPen(QColor(color)));
plot->xAxis->setBasePen(QPen(QColor(color)));
plot->yAxis->setLabelColor(QColor(color));
plot->yAxis->setTickLabelColor(QColor(color));
plot->yAxis->setTickPen(QPen(QColor(color)));
plot->yAxis->setSubTickPen(QPen(QColor(color)));
plot->yAxis->setBasePen(QPen(QColor(color)));
plot->xAxis2->setLabelColor(QColor(color));
plot->xAxis2->setTickLabelColor(QColor(color));
plot->xAxis2->setTickPen(QPen(QColor(color)));
plot->xAxis2->setSubTickPen(QPen(QColor(color)));
plot->xAxis2->setBasePen(QPen(QColor(color)));
plot->yAxis2->setLabelColor(QColor(color));
plot->yAxis2->setTickLabelColor(QColor(color));
plot->yAxis2->setTickPen(QPen(QColor(color)));
plot->yAxis2->setSubTickPen(QPen(QColor(color)));
plot->yAxis2->setBasePen(QPen(QColor(color)));
}
void MainWindow::on_setBtn_clicked()
{
#ifdef QT_DEBUG
settingUi->show();
#else
settingUi->showFullScreen();
#endif
QTime dieTime = QTime::currentTime().addMSecs(300);//延时300毫秒
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
this->hide();
}
void MainWindow::on_exitBtn_clicked()
{
this->close();
}
void MainWindow::showEvent(QShowEvent *event)
{
//更新设置
isFixedAxisRange = 0;
//读取是否固定坐标轴参数
config->beginGroup("customPlot1ConfigData");
if (config->value("fixedRange", false).toBool())
isFixedAxisRange |= MainWindow::Graph1FixedAxis;
config->endGroup();
config->beginGroup("customPlot2ConfigData");
if (config->value("fixedRange", false).toBool())
isFixedAxisRange |= MainWindow::Graph2FixedAxis;
config->endGroup();
QMainWindow::showEvent(event);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
QMainWindow::closeEvent(event);
}
//打开串口1
void MainWindow::on_Uart1Enable_clicked(bool checked)
{
if (checked)
{
setSerialPort(serialPort1);
if(!serialPort1->open(QIODevice::ReadWrite))
{
QMessageBox::warning(this, "串口错误", "串口打开失败或串口被占用");
qDebug() << serialPort1->errorString() << endl;
qDebug() << serialPort1->portName() << endl;
ui->Uart1Enable->setChecked(false);
return;
}
}
else
{
serialPort1->close();
}
}
//打开串口2
void MainWindow::on_Uart2Enable_clicked(bool checked)
{
if (checked)
{
setSerialPort(serialPort2);
if(!serialPort2->open(QIODevice::ReadWrite))
{
QMessageBox::warning(this, "串口错误", "串口打开失败或串口被占用");
ui->Uart2Enable->setChecked(false);
return;
}
}
else
{
serialPort2->close();
}
}
//重绘函数
void MainWindow::realtimeDataSlot()
{
static QTime time(QTime::currentTime());
key = time.elapsed()/1000.0; // 开始到现在的时间,单位秒
static double lastPointKey = 0;
//数据库
QSqlQuery sql_query(*database);
if (key - lastPointKey > 0.002) // 大约20ms添加一次数据,这里其实跟刷新率也有关系,若刷新时间间隔大于该时间间隔则取决于刷新时间间隔
{
// 添加测试数据到graph
double value = qSin(key)+qrand()/(double)RAND_MAX*1*qSin(key/0.3843) * 80;
plot_2->graph(0)->addData(key, value);
sql_query.exec(QString("insert into graph values( '%1', 0, '%2')").arg(key).arg(value)); //将数据存储到数据库
//另一组测试数据,显示在另外一张坐标轴上但不存储
//plot_1->graph(0)->addData(key, (qCos(key)+qrand()/(double)RAND_MAX*0.5*qSin(key/0.4364)) * 70);
//记录当前时刻
lastPointKey = key;
}
// 曲线能动起来的关键在这里,设定x轴范围为最近30个时刻
// 这里设置图像从左边开始绘制,当绘制满整个界面时图像开始整体左移
if (key < 30)
{
plot_1->xAxis->setRange(0, 30, Qt::AlignLeft);
plot_2->xAxis->setRange(0, 30, Qt::AlignLeft);
}
else
{
plot_1->xAxis->setRange(key - 30, 30, Qt::AlignLeft);
plot_2->xAxis->setRange(key - 30, 30, Qt::AlignLeft);
}
//设置坐标轴是否固定范围,若是则从配置文件读取范围参数,否则自动调整y轴范围,ps:前面写配置文件的时候偷懒了,这里可以再优化一下让代码更简洁的
if (isFixedAxisRange == MainWindow::Graph1FixedAxis)
{
config->beginGroup("customPlot1ConfigData");
double ymin = config->value("ymin", -100).toDouble();
double ymax = config->value("ymax", 100).toDouble();
config->endGroup();
plot_1->yAxis->setRange(ymin, ymax);
plot_2->yAxis->rescale();
}
else if (isFixedAxisRange == MainWindow::Graph2FixedAxis)
{
config->beginGroup("customPlot2ConfigData");
double ymin = config->value("ymin", -100).toDouble();
double ymax = config->value("ymax", 100).toDouble();
config->endGroup();
plot_2->yAxis->setRange(ymin, ymax);
plot_1->yAxis->rescale();
}
else if (isFixedAxisRange == (MainWindow::Graph1FixedAxis | MainWindow::Graph2FixedAxis))
{
config->beginGroup("customPlot1ConfigData");
double ymin = config->value("ymin", -100).toDouble();
double ymax = config->value("ymax", 100).toDouble();
config->endGroup();
plot_1->yAxis->setRange(ymin, ymax);
config->beginGroup("customPlot2ConfigData");
ymin = config->value("ymin", -100).toDouble();
ymax = config->value("ymax", 100).toDouble();
config->endGroup();
plot_2->yAxis->setRange(ymin, ymax);
}
else //两个显示区都自动调整坐标轴范围
{
plot_1->yAxis->rescale();
plot_2->yAxis->rescale();
}
//绘图
plot_2->replot();
plot_1->replot();
}
void MainWindow::showMainUi()
{
#ifdef QT_DEBUG
this->show();
#else
this->showFullScreen();
#endif
}
void MainWindow::uart1Recieve()
{
ui->UartInfo_1->clear();
QByteArray buff = serialPort1->readAll();
//将串口数据实时显示
ui->UartInfo_1->setText(QString(buff));
//将串口接收的数据加到曲线中
plot_1->graph(1)->addData(key, buff.toInt());
}
void MainWindow::uart2Recieve()
{
ui->UartInfo_2->clear();
QByteArray buff = serialPort2->readAll();
ui->UartInfo_2->setText(QString(buff));
plot_2->graph(1)->addData(key, buff.toInt());
}
void MainWindow::on_searchBtn_clicked()
{
#ifdef QT_DEBUG
searchUi->show();
#else
searchUi->showFullScreen();
#endif
//这一段代码的作用主要是:解决程序在低配置机器上运行时界面切换会闪烁的问题,这里的延时可以自己根据机器进行调整
QTime dieTime = QTime::currentTime().addMSecs(300);//延时300毫秒
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
this->hide();
}
复制代码
search.h 查询界面头文件
#ifndef SEARCH_H
#define SEARCH_H
#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
class MainWindow;
namespace Ui {
class Search;
}
class Search : public QMainWindow
{
Q_OBJECT
friend class MainWindow;
public:
explicit Search(QSqlDatabase *db, QWidget *parent = nullptr);
~Search();
int queryRowCount(QSqlQuery&);
//void initGraph
private:
Ui::Search *ui;
QSqlDatabase *database;
signals:
void showMainUi();
private slots:
void on_searchBtn_clicked();
void on_backBtn_clicked();
void on_clearBtn_clicked();
};
#endif // SEARCH_H
复制代码
search.cpp 查询界面源文件
#include "search.h"
#include "ui_search.h"
#include <QMessageBox>
#include "mainwindow.h"
Search::Search(QSqlDatabase *db, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Search)
{
ui->setupUi(this);
database = db;
//坐标轴使用时间
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
ui->plot->xAxis->setTicker(timeTicker);
//四边安上坐标轴
ui->plot->axisRect()->setupFullAxesBox();
//设置坐标轴名字
ui->plot->xAxis->setLabel("时间/h:m:s");
ui->plot->yAxis->setLabel("幅值");
//ui->plot->graph(0)->rescaleAxes();
this->setWindowTitle(QString("查询窗口"));
//设置显示风格
MainWindow::setCustomplotStyle("ManjaroMix", ui->plot);
}
Search::~Search()
{
delete ui;
}
//获取表格行数即数据个数
int Search::queryRowCount(QSqlQuery & query)
{
// 获取query指针首地址
int initialPos = query.at();
int pos = 0;
if (query.last()){
pos = query.at() + 1;
}else{
pos = 0;
}
// 计算行数后将query指针放到首地址
query.seek(initialPos);
return pos;
}
void Search::on_backBtn_clicked()
{
emit showMainUi();
QTime dieTime = QTime::currentTime().addMSecs(300);//延时300毫秒
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
this->close();
}
void Search::on_searchBtn_clicked()
{
//输入数据检查
if (ui->bgTimeLEdt->text().toDouble() >= ui->edTimeLEdt->text().toDouble())
{
QMessageBox::warning(this, "warning", "时间输入错误!");
return;
}
//设置曲线颜色
ui->plot->addGraph();
ui->plot->graph(0)->setPen(QPen(QColor("#4fa08b"))); //测试数据
QSqlQuery sql_query(*database);
QString search = QString("select * from graph where graphid = '%1' and timecount >= '%2' and timecount <= '%3';")\
.arg(ui->graphidLEdt->text()).arg(ui->bgTimeLEdt->text()).arg(ui->edTimeLEdt->text());
//QString search("select * from graph where graphid = '%1' and timecount >= '%2' and timecount <= '%3';");
sql_query.exec(search);
while (sql_query.next())
{
double key = sql_query.value(0).toDouble();
double value = sql_query.value(2).toDouble();
ui->plot->graph(0)->addData(key, value);
}
ui->plot->yAxis->rescale();
ui->plot->xAxis->rescale();
ui->plot->replot();
//状态栏显示graph信息
this->statusBar()->showMessage(
QString("曲线点数:%1")
.arg(queryRowCount(sql_query)));
}
void Search::on_clearBtn_clicked()
{
ui->plot->clearGraphs(); //删除所有graph
ui->plot->replot(); //重绘清屏
this->statusBar()->clearMessage();
}
复制代码
setting.h 设置界面头文件
#ifndef SETTING_H
#define SETTING_H
#include <QMainWindow>
#include <QSettings>
namespace Ui {
class Setting;
}
class Setting : public QMainWindow
{
Q_OBJECT
public:
explicit Setting(QWidget *parent = nullptr);
~Setting();
private slots:
void on_backBtn_clicked();
void uartPortChanged(int);
void on_applyBtn_clicked();
private:
Ui::Setting *ui;
QSettings *config;
protected:
void showEvent(QShowEvent *event) override;
void closeEvent(QCloseEvent *event) override;
signals:
void showMainUi();
};
#endif // SETTING_H
复制代码
setting.cpp 设置界面源文件
#include "setting.h"
#include "ui_setting.h"
#include "ui_setting.h"
#include "mainwindow.h"
#include <QScreen>
#include <QGuiApplication>
#include <QDebug>
#include <QSerialPort>
#include <QSerialPortInfo>
Setting::Setting(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Setting)
{
ui->setupUi(this);
//打开配置文件
config = new QSettings("config.ini", QSettings::IniFormat);
//this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
//关联槽函数
connect(ui->buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(uartPortChanged(int)));
//connect(this, SIGNAL(showMainUi()), Widget, )
//扫描并显示串口名
foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->serialName->addItem(info.portName());
}
}
Setting::~Setting()
{
delete ui;
}
void Setting::on_backBtn_clicked()
{
emit showMainUi();
QTime dieTime = QTime::currentTime().addMSecs(300);//延时300毫秒
while (QTime::currentTime() < dieTime)
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
this->close();
}
void Setting::uartPortChanged(int id)
{
Q_UNUSED(id);
QString str = ui->buttonGroup->checkedButton()->text();
if (str == "串口1")
{
//从配置文件读取串口1历史参数并显示
//qDebug() << "1" << endl;
config->beginGroup("serialPort1ConfigData");
ui->serialName->setCurrentText(config->value("serialName").toString());
ui->buadRate->setCurrentText(config->value("baudrate", 9600).toString());
ui->dataBit->setCurrentText(config->value("databit", 8).toString());
ui->stopBit->setCurrentIndex(config->value("stopbit", 0).toInt());
ui->parityBit->setCurrentIndex(config->value("paritybit", 0).toInt());
config->endGroup();
#if 0 //存储串口1的参数
uart1->serialName = ui->serialName->currentText();
uart1->baudRate = ui->buadRate->currentText().toInt();
uart1->dataBit = ui->dataBit->currentText().toInt();
uart1->stopBit = ui->stopBit->currentText().toDouble();
uart1->parityBit = ui->parityBit->currentText();
#endif
}
else if (str == "串口2")
{
//从配置文件读取串口2历史参数并显示
//qDebug() << "2" << endl;
config->beginGroup("serialPort2ConfigData");
ui->serialName->setCurrentText(config->value("serialName").toString());
ui->buadRate->setCurrentText(config->value("baudrate", 9600).toString());
ui->dataBit->setCurrentText(config->value("databit", 8).toString());
ui->stopBit->setCurrentIndex(config->value("stopbit", 0).toInt());
ui->parityBit->setCurrentIndex(config->value("paritybit", 0).toInt());
config->endGroup();
#if 0 //存储串口2的参数
uart1->serialName = ui->serialName->currentText();
uart1->baudRate = ui->buadRate->currentText().toInt();
uart1->dataBit = ui->dataBit->currentText().toInt();
uart1->stopBit = ui->stopBit->currentText().toDouble();
uart1->parityBit = ui->parityBit->currentText();
#endif
}
}
void Setting::showEvent(QShowEvent *event)
{
//获取主屏幕
//QScreen* screen = QGuiApplication::primaryScreen();
// 设置窗口位置和大小,使窗口全屏显示
//setGeometry(screen->geometry());
config->beginGroup("serialPort1ConfigData");
ui->serialName->setCurrentText(config->value("serialName").toString());
ui->buadRate->setCurrentText(config->value("baudrate", 9600).toString());
ui->dataBit->setCurrentText(config->value("databit", 8).toString());
ui->stopBit->setCurrentIndex(config->value("stopbit", 0).toInt());
ui->parityBit->setCurrentIndex(config->value("paritybit", 0).toInt());
config->endGroup();
//坐标轴参数
config->beginGroup("customPlot1ConfigData");
ui->fixedAxisRangeCkbx_1->setChecked(config->value("fixedRange", false).toBool());
ui->cstmPlt1Ymin->setText(config->value("ymin").toString());
ui->cstmPlt1Ymax->setText(config->value("ymax").toString());
config->endGroup();
config->beginGroup("customPlot2ConfigData");
ui->fixedAxisRangeCkbx_2->setChecked(config->value("fixedRange", false).toBool());
ui->cstmPlt2Ymin->setText(config->value("ymin").toString());
ui->cstmPlt2Ymax->setText(config->value("ymax").toString());
config->endGroup();
QWidget::showEvent(event);
}
void Setting::closeEvent(QCloseEvent *event)
{
QWidget::closeEvent(event);
}
void Setting::on_applyBtn_clicked()
{
//将各配置参数更新到配置文件
//串口数据
QString str = ui->buttonGroup->checkedButton()->text();
if (str == "串口1")
{
config->beginGroup("serialPort1ConfigData");
config->setValue("serialName", ui->serialName->currentText());
config->setValue("baudrate", ui->buadRate->currentText());
config->setValue("databit", ui->dataBit->currentText());
config->setValue("stopbit", ui->stopBit->currentIndex());
config->setValue("paritybit", ui->parityBit->currentIndex());
config->endGroup();
}
else if (str == "串口2")
{
config->beginGroup("serialPort2ConfigData");
config->setValue("serialName", ui->serialName->currentText());
config->setValue("baudrate", ui->buadRate->currentText());
config->setValue("databit", ui->dataBit->currentText());
config->setValue("stopbit", ui->stopBit->currentIndex());
config->setValue("paritybit", ui->parityBit->currentIndex());
config->endGroup();
}
//customPlot区域显示坐标轴范围
//区域1
config->beginGroup("customPlot1ConfigData");
config->setValue("fixedRange", ui->fixedAxisRangeCkbx_1->isChecked());
config->setValue("ymin", ui->cstmPlt1Ymin->text().toDouble());
config->setValue("ymax", ui->cstmPlt1Ymax->text().toDouble());
config->endGroup();
//区域2
config->beginGroup("customPlot2ConfigData");
config->setValue("fixedRange", ui->fixedAxisRangeCkbx_2->isChecked());
config->setValue("ymin", ui->cstmPlt2Ymin->text().toDouble());
config->setValue("ymax", ui->cstmPlt2Ymax->text().toDouble());
config->endGroup();
}
复制代码
后续
写到这里的时间间隔我创建这篇草稿已颠末去了大概五六个月,中央因为工作比较忙就没有再写下去,后续再次想要拿起QT的时间才想起来这篇草稿还没发,基本框架实在写得差不多了,而工程文件已经找不到了,如果后续我能找到就放上来,各人可以参考一下代码如何实现。这个项目属于初学者练手项目,另有诸多不敷,欢迎各人评论区讨论。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4