Przeglądaj źródła

file show the scope

flower 9 miesięcy temu
rodzic
commit
bd142f16e7

+ 389 - 0
code/chart/chart_file.cpp

@@ -0,0 +1,389 @@
+#include "chart_file.h"
+#include "ui_chart_file.h"
+
+#include <QDebug>
+#include <QMessageBox>
+#include <QFile>
+
+chart_file::chart_file(QWidget *parent) :
+    QWidget(parent),
+    ui(new Ui::chart_file),
+    chart(new QChart),
+    timer(new QTimer),
+    count(0)
+{
+    ui->setupUi(this);
+    QStandardItemModel* model = treeModel->tree_set();
+    ui->tree_set->setModel(model);
+    ui->tree_set->expandAll();
+    // 将 QTreeView 的 clicked 信号连接到 handleTreeItemClicked 槽函数
+    connect(ui->tree_set, &QTreeView::clicked, this, &chart_file::handleTreeItemClicked);
+    // 初始化变量
+    time=nullptr;
+    point1=nullptr;
+    point2=nullptr;
+
+    // 设置点的数量
+    pointsSize=200;
+
+    // 初始化字体颜色
+    initFontColor();
+
+    // 初始化读取数据
+    initReadData();
+
+    // 设置定时器间隔为100毫秒
+    timer->setInterval(100);
+
+    // 启动定时器
+    timer->start();
+}
+
+chart_file::~chart_file()
+{
+    // 释放UI对象和动态分配的内存
+    delete ui;
+
+    // 释放动态分配的数组内存
+    delete [] time;
+    delete [] point1;
+    delete [] point2;
+
+    // 释放二维数组tempDis的内存
+    for (int i=0;i<pointsNum;i++) {
+        delete [] tempDis[i];
+    }
+    delete [] tempDis;
+}
+void chart_file::setTreeModel(tree_model_set* model)
+{
+    treeModel = model; // 设置 tree_model_set 实例
+}
+void chart_file::handleTreeItemClicked(const QModelIndex &index) {
+    // 转发信号
+    emit treeItemClicked(index);
+}
+// 鼠标滚轮事件处理函数
+void chart_file::wheelEvent(QWheelEvent *event)
+{
+    if (event->angleDelta().y() > 0) {
+        chart->zoom(1.1);
+    } else {
+        chart->zoom(10/1.1);
+    }
+
+    QWidget::wheelEvent(event);
+}
+
+void chart_file::initUI()
+{
+    initChart();
+}
+// 初始化图表
+void chart_file::initChart()
+{
+    //chart->createDefaultAxes();
+
+    /**修改**/
+    axisX=new QValueAxis();
+    axisX->setTitleFont(QFont("Microsoft YaHei", 10, QFont::Normal, true));
+    axisX->setTitleText("Time(s)");
+    axisX->setGridLineVisible(true);
+    chart->addAxis(axisX,Qt::AlignBottom);
+
+    axisY=new QValueAxis();
+    axisY->setTitleFont(QFont("Microsoft YaHei", 10, QFont::Normal, true));
+    axisY->setTitleText("值");
+    axisY->setGridLineVisible(true);
+    chart->addAxis(axisY,Qt::AlignLeft);
+    for (int i=0;i<pointsNum;i++) {
+        series[i]->attachAxis(axisX);
+        series[i]->attachAxis(axisY);
+    }
+    /**修改**/
+    chart->legend()->hide();
+    chartView = new QChartView(chart);
+    chartView->setRenderHint(QPainter::Antialiasing);//抗锯齿渲染
+    ui->mainvertical->addWidget(chartView);
+}
+// 初始化信号槽连接
+// 初始化槽函数,连接信号和槽
+void chart_file::initSlot()
+{
+    connect(timer, SIGNAL(timeout()), this, SLOT(timerSlot()));
+    connect(ui->allRadioButton,SIGNAL(clicked()),this,SLOT(selectAll()));
+    connect(ui->invertRadioButton,SIGNAL(clicked()),this,SLOT(invertSelect()));
+
+    for (int i=0;i<pointsNum;i++) {
+        connect(checkBoxVector.at(i),SIGNAL(toggled(bool)),this,SLOT(checkboxChanged()));
+        connect(series[i], SIGNAL(hovered(QPointF, bool)), this, SLOT(tipSlot(QPointF,bool)));
+    }
+}
+
+
+// 添加图表数据
+void chart_file::addChartData(float time, float *pointsDis)
+{
+    float globalMinX = time;
+    float globalMaxX = time;
+    float globalMinY = pointsDis[0];
+    float globalMaxY = pointsDis[0];
+
+    for (int i = 0; i < pointsNum; i++)
+    {
+        QVector<QPointF> data = series[i]->pointsVector();
+
+        data.append(QPointF(time, pointsDis[i]));
+
+        float minX = data.at(0).x();
+        float maxX = data.at(0).x();
+        float minY = data.at(0).y();
+        float maxY = data.at(0).y();
+
+        for (int j = 0; j < data.size(); j++) {
+            if (minY > data.at(j).y())
+                minY = data.at(j).y();
+            if (maxY < data.at(j).y())
+                maxY = data.at(j).y();
+        }
+
+        if (i == 0)
+        {
+            for (int j = 0; j < data.size(); j++) {
+                if (minX > data.at(j).x())
+                    minX = data.at(j).x();
+                if (maxX < data.at(j).x())
+                    maxX = data.at(j).x();
+            }
+            globalMinX = minX;
+            globalMaxX = maxX;
+        }
+
+        globalMinY = globalMinY < minY ? globalMinY : minY;
+        globalMaxY = globalMaxY > maxY ? globalMaxY : maxY;
+
+        series[i]->replace(data);
+
+        tempDis[i][0] = pointsDis[i];
+        tempDis[i][1] = maxY;
+        tempDis[i][2] = minY;
+    }
+
+    addTableData(tempDis);
+
+    axisX->setRange(globalMinX, globalMaxX);
+    axisY->setRange(globalMinY - (globalMaxY - globalMinY) * 0.1, globalMaxY + (globalMaxY - globalMinY) * 0.1);
+}
+
+
+// 设置点的数量
+void chart_file::setPointsNum(int num)
+{
+    if(num>9)
+    {
+        QMessageBox::information(this,"Warnning","The number of points exceed 9!");
+        return;
+    }
+
+    tempDis=new float*[num];
+
+    pointsNum=num;
+    //model->setRowCount(num);
+
+    // 该函数用于初始化图表中的数据系列,并为每个数据系列设置颜色和临时数据存储空间
+    // 参数:
+    // - pointsNum: 数据系列的数量
+    // - series: 指向数据系列数组的指针
+    // - colorTable: 包含每个数据系列颜色的QList
+    // - chart: 指向图表对象的指针
+    // - tempDis: 指向临时数据存储数组的指针
+    for (int i=0;i<pointsNum;i++) {
+        series[i]=new QLineSeries();
+        series[i]->setColor(colorTable.at(i));
+        chart->addSeries(series[i]);
+        tempDis[i]=new float[3];
+    }
+
+    initUI();
+    initTable(num);
+    initSlot();
+}
+
+// 添加表格数据
+void  chart_file::addTableData(float **pointsDis)
+{
+    for (int i=0;i<pointsNum;i++) {
+        for (int j=0;j<3;j++) {
+            model->setItem(i, j+1, new QStandardItem(QString::number(pointsDis[i][j])));
+            model->item(i,j+1)->setForeground(QBrush(colorTable.at(i)));
+            model->item(i, j+1)->setTextAlignment(Qt::AlignCenter);
+        }
+    }
+}
+
+// 初始化表格
+void chart_file::initTable(int pointsNumber)
+{
+    ui->allRadioButton->setChecked(true);
+
+    model = new QStandardItemModel();
+    model->setColumnCount(4);
+    model->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr("曲线编号")));
+    model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("Displacements(mm)")));
+    model->setHorizontalHeaderItem(2, new QStandardItem(QObject::tr("Peak(mm)")));
+    model->setHorizontalHeaderItem(3, new QStandardItem(QObject::tr("Valley(mm)")));
+
+    ui->tableView->setModel(model);
+
+    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
+    ui->tableView->verticalHeader()->hide();
+    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
+    ui->tableView->setColumnWidth(1,100);
+
+    model->setRowCount(pointsNumber);
+
+    // 此函数用于在表格视图中创建多个复选框,并将它们添加到模型中
+    // 每个复选框的标签为 "Point" 加上序号,并根据颜色数组设置样式
+    // 最后,将复选框添加到表格视图中,并设置其初始状态为选中
+    QString str="";
+    for (int i=0;i<pointsNumber;i++) {
+        str="Point";
+        str+=QString::number(i+1);
+        QCheckBox *box=new QCheckBox(str,ui->tableView);
+        box->setStyleSheet(strColor[i]);
+    
+        checkBoxVector.append(box);
+    
+        model->setItem(i, 0, new QStandardItem(""));
+        ui->tableView->setIndexWidget(model->index(i,0),checkBoxVector.at(i));
+        model->item(i, 0)->setTextAlignment(Qt::AlignCenter);
+        checkBoxVector.at(i)->setChecked(true);
+    }
+}
+
+// 全选所有点
+void chart_file::selectAll()
+{
+    for(int i=0;i<checkBoxVector.size();i++)
+        checkBoxVector.at(i)->setChecked(true);
+}
+
+// 初始化字体颜色
+void chart_file::initFontColor()
+{
+    colorTable.append(QColor(255,0,0));//red
+    colorTable.append(QColor(0,0,255));//blue
+    colorTable.append(QColor(0,255,0));//green
+    colorTable.append(QColor(139,0,0));//dark red
+    colorTable.append(QColor(255,255,0));//yellow
+    colorTable.append(QColor(0,0,0));//black
+    colorTable.append(QColor(128,42,42));//棕色
+    colorTable.append(QColor(160,32,240));//purple
+    colorTable.append(QColor(0,255,255));//青色
+
+    strColor[0]="QCheckBox{color:rgb(255,0,0)}";
+    strColor[1]="QCheckBox{color:rgb(0,0,255)}";
+    strColor[2]="QCheckBox{color:rgb(0,255,0)}";
+    strColor[3]="QCheckBox{color:rgb(139,0,0)}";
+    strColor[4]="QCheckBox{color:rgb(255,255,0)}";
+    strColor[5]="QCheckBox{color:rgb(128,42,42)}";
+    strColor[7]="QCheckBox{color:rgb(160,32,240)}";
+    strColor[8]="QCheckBox{color:rgb(0,255,255)}";
+}
+
+// 反选所有点
+void  chart_file::invertSelect()
+{
+    for(int i=0;i<checkBoxVector.size();i++){
+        if(checkBoxVector.at(i)->checkState())
+            checkBoxVector.at(i)->setChecked(false);
+        else
+            checkBoxVector.at(i)->setChecked(true);
+    }
+}
+
+// 复选框状态改变处理函数
+void  chart_file::checkboxChanged()
+{
+    for (int i=0;i<pointsNum;i++) {
+        if(checkBoxVector.at(i)->checkState()){
+            series[i]->setVisible(true);
+        }
+        else {
+            series[i]->setVisible(false);
+        }
+    }
+}
+
+// 初始化读取数据
+void chart_file::initReadData()
+{
+    QString name = "D:/document/code_all/Qt/QtChartWidget-master/QtChartWidget/data/3.txt";
+
+    QFile readFile(name);
+    int i=0;
+
+    if(!readFile.open(QIODevice::ReadOnly | QIODevice::Text))
+    {
+        QMessageBox::information(nullptr, "Warning", "Fail to read the file");
+        return;
+    }
+
+    QTextStream readStream(&readFile);
+    QString line;
+
+    time=new float[10000];
+    point1=new float[10000];
+    point2= new float[10000];
+    QString str="";
+
+    while(i<10000)
+    {
+        line=readStream.readLine();
+
+        if(!line.isEmpty())
+        {
+            QStringList list = line.split(QRegularExpression(","), Qt::SkipEmptyParts);
+
+            time[i]=list.at(0).toFloat();
+            point1[i]=list.at(1).toFloat();
+            point2[i]=list.at(2).toFloat();
+
+            if(i<20)
+            {
+                str+=QString::number(time[i]);
+                str+=", ";
+                str+=QString::number(point1[i]);
+                str+=", ";
+                str+=QString::number(point2[i]);
+                str+="\n";
+            }
+        }
+
+        i++;
+    }
+
+    readFile.close();
+}
+
+// 添加数据
+void chart_file::addData()
+{
+    float y[2];
+    y[0]=point1[count];
+    y[1]=point2[count];
+
+    addChartData(time[count],y);
+
+    count++;
+}
+
+// 定时器槽函数
+void chart_file::timerSlot()
+{
+    if (QObject::sender() == timer) {
+        addData();
+    }
+}
+
+

+ 91 - 0
code/chart/chart_file.h

@@ -0,0 +1,91 @@
+#ifndef CHART_FILE_H
+#define CHART_FILE_H
+
+#include <QWidget>
+#include <QChart>
+#include <QLineSeries>
+#include <QVector>
+#include <QTimer>
+#include <QStandardItemModel>
+#include <QCheckBox>
+#include <QValueAxis>
+#include <QChartView>
+#include "code/tree/tree_model_set.h"
+//QT_USE_NAMESPACE
+
+namespace Ui {
+class chart_file;
+}
+
+class chart_file : public QWidget
+{
+    Q_OBJECT
+
+public:
+    explicit chart_file(QWidget *parent = nullptr);
+    ~chart_file();
+    void setTreeModel(tree_model_set *model); // 接受 tree_model_set 实例
+    void setPointsNum(int num);
+    void addChartData(float time, float *pointsDis);
+
+
+signals:
+    void treeItemClicked(const QModelIndex &index);
+
+private:
+    Ui::chart_file *ui;
+    void wheelEvent(QWheelEvent *event);
+    tree_model_set *treeModel; // 保存 tree_model_set 实例的指针
+    void initUI();
+    void initChart();
+    void initSlot();
+    void initTable(int pointsNumber);
+    void initFontColor();
+    void handleTreeItemClicked(const QModelIndex &index);
+
+    void initReadData();
+    void addData();
+    QChartView *chartView;
+    QChart *chart;
+    QLineSeries *series[8];
+    int pointsSize;//用于画曲线的数据点数
+
+
+    float *time;
+    float *point1;
+    float *point2;
+    int pointsNum;
+
+    float **tempDis;//store dis,mini dis, max dis for every points;
+
+    QTimer *timer;
+
+    quint16 count;
+
+
+
+
+private slots:
+    void timerSlot();
+    void selectAll();
+    void invertSelect();
+    void checkboxChanged();
+
+
+
+private:
+
+    QVector<QCheckBox *> checkBoxVector;
+    QString strColor[9];
+    QVector<QColor> colorTable;
+    QStandardItemModel *model;
+
+    QValueAxis *axisX;
+    QValueAxis *axisY;
+
+    void addTableData(float **pointsDis);
+
+
+};
+
+#endif // CHART_FILE_H

+ 125 - 0
code/chart/chart_file.ui

@@ -0,0 +1,125 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>chart_file</class>
+ <widget class="QWidget" name="chart_file">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>882</width>
+    <height>694</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <layout class="QGridLayout" name="gridLayout">
+   <item row="1" column="3">
+    <widget class="QPushButton" name="pushButton">
+     <property name="text">
+      <string>清空窗口</string>
+     </property>
+    </widget>
+   </item>
+   <item row="0" column="1" rowspan="5" colspan="2">
+    <layout class="QVBoxLayout" name="mainvertical">
+     <property name="spacing">
+      <number>0</number>
+     </property>
+     <item>
+      <layout class="QVBoxLayout" name="behindvertical">
+       <property name="spacing">
+        <number>0</number>
+       </property>
+       <item>
+        <widget class="QTableView" name="tableView"/>
+       </item>
+      </layout>
+     </item>
+    </layout>
+   </item>
+   <item row="0" column="3">
+    <widget class="QGroupBox" name="groupBox">
+     <property name="minimumSize">
+      <size>
+       <width>120</width>
+       <height>0</height>
+      </size>
+     </property>
+     <property name="maximumSize">
+      <size>
+       <width>120</width>
+       <height>100</height>
+      </size>
+     </property>
+     <property name="toolTipDuration">
+      <number>-1</number>
+     </property>
+     <property name="layoutDirection">
+      <enum>Qt::LeftToRight</enum>
+     </property>
+     <property name="title">
+      <string>曲线</string>
+     </property>
+     <property name="flat">
+      <bool>false</bool>
+     </property>
+     <layout class="QVBoxLayout" name="verticalLayout">
+      <item>
+       <widget class="QRadioButton" name="allRadioButton">
+        <property name="text">
+         <string>全选</string>
+        </property>
+        <property name="iconSize">
+         <size>
+          <width>16</width>
+          <height>16</height>
+         </size>
+        </property>
+       </widget>
+      </item>
+      <item>
+       <widget class="QRadioButton" name="invertRadioButton">
+        <property name="text">
+         <string>反转</string>
+        </property>
+       </widget>
+      </item>
+     </layout>
+    </widget>
+   </item>
+   <item row="0" column="0" rowspan="5">
+    <widget class="QTreeView" name="tree_set">
+     <property name="minimumSize">
+      <size>
+       <width>120</width>
+       <height>0</height>
+      </size>
+     </property>
+     <property name="maximumSize">
+      <size>
+       <width>120</width>
+       <height>16777215</height>
+      </size>
+     </property>
+    </widget>
+   </item>
+   <item row="2" column="3">
+    <widget class="QPushButton" name="pushButton_2">
+     <property name="text">
+      <string>归一化</string>
+     </property>
+    </widget>
+   </item>
+   <item row="3" column="3">
+    <widget class="QPushButton" name="pushButton_3">
+     <property name="text">
+      <string>保存</string>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>

+ 4 - 1
code/login/login_switch.cpp

@@ -15,6 +15,9 @@ login_switch::~login_switch()
 
 void login_switch::on_pushButton_clicked()
 {
-    page_main.show();
+    page_main.showMaximized(); //显示主界面全屏显示
+    this->hide();
+    
+   
 }
 

+ 4 - 0
code/login/login_switch.ui

@@ -34,6 +34,10 @@
         <property name="layoutDirection">
          <enum>Qt::LeftToRight</enum>
         </property>
+        <property name="styleSheet">
+         <string notr="true">
+background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(255, 178, 102, 255), stop:0.55 rgba(235, 148, 61, 255), stop:0.98 rgba(0, 0, 0, 255), stop:1 rgba(0, 0, 0, 0));</string>
+        </property>
         <property name="text">
          <string>台架选择</string>
         </property>

+ 81 - 51
code/scope/scope.cpp

@@ -4,6 +4,8 @@
 #include <QValueAxis>
 #include <QLineSeries>
 #include <QChart>
+#include <QRandomGenerator>
+#include <QVector>
 
 scope::scope(QWidget *parent) :
     QMainWindow(parent),
@@ -15,7 +17,6 @@ scope::scope(QWidget *parent) :
     ui->tree_set->expandAll();
     // 将 QTreeView 的 clicked 信号连接到 handleTreeItemClicked 槽函数
     connect(ui->tree_set, &QTreeView::clicked, this, &scope::handleTreeItemClicked);
-
 }
 
 scope::~scope()
@@ -33,66 +34,95 @@ void scope::handleTreeItemClicked(const QModelIndex &index) {
     emit treeItemClicked(index);
 }
 
-// 图像初始化
+//函数功能:初始化图表
+//参数:无
+//返回值:无
+//实现流程:
+//1.创建 QGraphicsScene 并设置到 QGraphicsView
+//2.初始化 lineSeries,如果为空则创建
+//3.创建 QChart 并添加 lineSeries
+//4.创建 QChartView 并将 chart 添加到 QGraphicsScene 中
 void scope::Chart_Init()
 {
-    // 初始化 QChart 的实例
-    chart = new QChart();
-    lineSeries = new QSplineSeries();
-    lineSeries->setName("测试曲线");
-    chart->addSeries(lineSeries);
-
-    // 声明并初始化 X 轴和 Y 轴
-    QValueAxis *axisX = new QValueAxis();
-    QValueAxis *axisY = new QValueAxis();
-    axisX->setRange(0, MAX_X);
-    axisY->setRange(-1.5, MAX_Y);
+    //初始化QChart的实例
+        chart = new QChart();
+        //初始化QSplineSeries的实例
+        lineSeries = new QSplineSeries();
+        //设置曲线的名称
+        lineSeries->setName("测试曲线");
+        //把曲线添加到QChart的实例chart中
+        chart->addSeries(lineSeries);
+        //声明并初始化X轴、两个Y轴
+        QValueAxis *axisX = new QValueAxis();
+        QValueAxis *axisY = new QValueAxis();
+        //设置坐标轴显示的范围
+        axisX->setMin(0);
+        axisX->setMax(MAX_X);
+        axisY->setMin(-1.5);
+        axisY->setMax(MAX_Y);
+        //设置坐标轴上的格点
+        axisX->setTickCount(10);
+        axisY->setTickCount(10);
+        //设置坐标轴显示的名称
+        QFont font("Microsoft YaHei",8,QFont::Normal);//微软雅黑。字体大小8
+        axisX->setTitleFont(font);
+        axisY->setTitleFont(font);
+        axisX->setTitleText("X-Test");
+        axisY->setTitleText("Y-Test");
+        //设置网格不显示
+        axisY->setGridLineVisible(false);
+        //下方:Qt::AlignBottom,左边:Qt::AlignLeft
+        //右边:Qt::AlignRight,上方:Qt::AlignTop
+        chart->addAxis(axisX, Qt::AlignBottom);
+        chart->addAxis(axisY, Qt::AlignLeft);
+        //把曲线关联到坐标轴
+        lineSeries->attachAxis(axisX);
+        lineSeries->attachAxis(axisY);
+        //把chart显示到窗口上
+        ui->gra_scope11->setChart(chart);
+        ui->gra_scope11->setRenderHint(QPainter::Antialiasing);      // 设置渲染:抗锯齿,如果不设置那么曲线就显得不平滑
+}
 
-    axisX->setTickCount(10);
-    axisY->setTickCount(10);
 
-    QFont font("Microsoft YaHei", 8, QFont::Normal);
-    axisX->setTitleFont(font);
-    axisY->setTitleFont(font);
-    axisX->setTitleText("X-Test");
-    axisY->setTitleText("Y-Test");
-    axisY->setGridLineVisible(false);
 
-    chart->addAxis(axisX, Qt::AlignBottom);
-    chart->addAxis(axisY, Qt::AlignLeft);
-    lineSeries->attachAxis(axisX);
-    lineSeries->attachAxis(axisY);
+void scope::DrawLine(float value)
+{
+    
+    
 
-    // 创建一个 QGraphicsScene 并将 chart 添加进去
-    QGraphicsScene *scene = new QGraphicsScene(this);
-    scene->addItem(chart);
+    // // 如果超过了最大 X 轴范围,移除最早的数据点
+    // if (count > MAX_X) {
+    //     lineSeries->removePoints(0, lineSeries->count() - MAX_X);
+        
+    //     chart->axisX()->setRange(count - MAX_X, count); // 更新 X 轴范围
+    // }
 
-    // 将 QGraphicsScene 设置到 QGraphicsView
-    ui->gra_scope11->setScene(scene);
-    ui->gra_scope11->setRenderHint(QPainter::Antialiasing);  // 设置抗锯齿
+    // 遍历 newData,将新的数据点添加到曲线末端
+    
+        lineSeries->append(count, value); // 添加新的数据点
+        chart->axisX()->setRange(0, count); // 更新 X 轴范围
+        count++; // 更新点的计数
+    
 
-    // 调整 QGraphicsView 的内容填充整个视口
-    ui->gra_scope11->fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
+}
 
-    // 使 QChart 填充整个 QGraphicsView
-    QRectF viewRect = ui->gra_scope11->viewport()->rect();
-    chart->resize(viewRect.width(), viewRect.height());
 
-    // 设置 QGraphicsView 自动调整大小
-    ui->gra_scope11->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+// 将 generateRandomData测试使用
+QVector<QPointF> scope::generateRandomData(int pointCount) {
+    QVector<QPointF> data;
+    for (int i = 0; i < pointCount; ++i) {
+        qreal x = i;
+        qreal y = QRandomGenerator::global()->bounded(0, 100); // 生成0到100之间的随机数
+        data.append(QPointF(x, y));
+    }
+    return data; // 返回数据
 }
-void scope::DrawLine(float data)
+
+void scope::on_pushButton_clicked()//清空图像
 {
-    static int count = 0;
-    if(count > MAX_X)
-    {
-        //当曲线上最早的点超出X轴的范围时,剔除最早的点,
-        lineSeries->removePoints(0,lineSeries->count() - MAX_X);
-        // 更新X轴的范围
-        chart->axisX()->setMin(count - MAX_X);
-        chart->axisX()->setMax(count);
-    }
-    //增加新的点到曲线末端
-    lineSeries->append(count, data);//
-    count ++;
+    lineSeries->clear();
+    chart->axisX()->setRange(0, 0); // 更新 X 轴范围
+    chart->axisY()->setRange(-1.5, 1.5); // 更新 Y 轴范围
+    count=0;
 }
+

+ 18 - 14
code/scope/scope.h

@@ -5,14 +5,15 @@
 #include "code/tree/tree_model_set.h"
 #include <QtCharts/QChartView>
 #include <QtCharts/QSplineSeries>
-//图像设置
-#define MAX_X   50
-#define MAX_Y   1.5
-#define TRUE    1
-#define FALSE   0
-
-namespace Ui {
-class scope;
+// 图像设置
+#define MAX_X 50
+#define MAX_Y 1.5
+#define TRUE 1
+#define FALSE 0
+
+namespace Ui
+{
+    class scope;
 }
 
 class scope : public QMainWindow
@@ -21,23 +22,26 @@ class scope : public QMainWindow
 
 public:
     explicit scope(QWidget *parent = nullptr);
-    void setTreeModel(tree_model_set* model); // 接受 tree_model_set 实例
+    void setTreeModel(tree_model_set *model); // 接受 tree_model_set 实例
     void Chart_Init();
-    void DrawLine(float data);
+    void DrawLine(float value);
     ~scope();
 
+
+
 signals:
     void treeItemClicked(const QModelIndex &index);
 private slots:
     void handleTreeItemClicked(const QModelIndex &index);
-
+    void on_pushButton_clicked();
 
 private:
     Ui::scope *ui;
-    tree_model_set* treeModel; // 保存 tree_model_set 实例的指针
+    tree_model_set *treeModel; // 保存 tree_model_set 实例的指针
     QChart *chart;
-    QSplineSeries *lineSeries;
-
+    QLineSeries *lineSeries = nullptr; //
+    QVector<QPointF> generateRandomData(int pointCount);
+    int count = 0; // 记录数据点的序号
 };
 
 #endif // SCOPE_H

+ 132 - 104
code/scope/scope.ui

@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>1031</width>
-    <height>731</height>
+    <width>1115</width>
+    <height>712</height>
    </rect>
   </property>
   <property name="windowTitle">
@@ -18,119 +18,140 @@
     <normaloff>:/graph-up.svg</normaloff>:/graph-up.svg</iconset>
   </property>
   <widget class="QWidget" name="centralwidget">
-   <layout class="QVBoxLayout" name="verticalLayout">
+   <layout class="QVBoxLayout" name="verticalLayout_3">
+    <property name="spacing">
+     <number>0</number>
+    </property>
+    <property name="leftMargin">
+     <number>0</number>
+    </property>
+    <property name="topMargin">
+     <number>0</number>
+    </property>
+    <property name="rightMargin">
+     <number>0</number>
+    </property>
+    <property name="bottomMargin">
+     <number>0</number>
+    </property>
     <item>
-     <widget class="QGroupBox" name="groupBox">
-      <property name="title">
-       <string/>
-      </property>
-      <layout class="QHBoxLayout" name="horizontalLayout_2">
-       <item>
-        <spacer name="horizontalSpacer">
-         <property name="orientation">
-          <enum>Qt::Horizontal</enum>
-         </property>
-         <property name="sizeHint" stdset="0">
-          <size>
-           <width>704</width>
-           <height>20</height>
-          </size>
-         </property>
-        </spacer>
-       </item>
-       <item>
-        <widget class="QPushButton" name="pushButton">
-         <property name="text">
-          <string>PushButton</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <widget class="QPushButton" name="btn_one">
-         <property name="text">
-          <string>归一化</string>
-         </property>
-        </widget>
-       </item>
-       <item>
-        <widget class="QPushButton" name="btn_save">
-         <property name="text">
-          <string>保存图像</string>
-         </property>
-        </widget>
-       </item>
-      </layout>
-     </widget>
-    </item>
-    <item>
-     <layout class="QHBoxLayout" name="horizontalLayout">
+     <layout class="QVBoxLayout" name="verticalLayout_2">
       <item>
-       <widget class="QTreeView" name="tree_set">
-        <property name="maximumSize">
-         <size>
-          <width>150</width>
-          <height>16777215</height>
-         </size>
-        </property>
-       </widget>
-      </item>
-      <item>
-       <widget class="QGroupBox" name="groupBox_2">
-        <property name="maximumSize">
-         <size>
-          <width>16777215</width>
-          <height>16777215</height>
-         </size>
-        </property>
+       <widget class="QGroupBox" name="groupBox">
         <property name="title">
          <string/>
         </property>
-        <layout class="QGridLayout" name="gridLayout">
-         <property name="leftMargin">
-          <number>1</number>
-         </property>
-         <property name="topMargin">
-          <number>1</number>
-         </property>
-         <property name="rightMargin">
-          <number>1</number>
-         </property>
-         <property name="bottomMargin">
-          <number>1</number>
-         </property>
-         <property name="spacing">
-          <number>1</number>
-         </property>
-         <item row="0" column="0">
-          <widget class="QGraphicsView" name="gra_scope11"/>
+        <layout class="QHBoxLayout" name="horizontalLayout_2">
+         <item>
+          <spacer name="horizontalSpacer">
+           <property name="orientation">
+            <enum>Qt::Horizontal</enum>
+           </property>
+           <property name="sizeHint" stdset="0">
+            <size>
+             <width>704</width>
+             <height>20</height>
+            </size>
+           </property>
+          </spacer>
          </item>
-         <item row="0" column="1">
-          <widget class="QGraphicsView" name="graphicsView_2"/>
+         <item>
+          <widget class="QPushButton" name="pushButton">
+           <property name="text">
+            <string>PushButton</string>
+           </property>
+          </widget>
          </item>
-         <item row="0" column="2">
-          <widget class="QGraphicsView" name="graphicsView_3"/>
+         <item>
+          <widget class="QPushButton" name="btn_one">
+           <property name="text">
+            <string>归一化</string>
+           </property>
+          </widget>
          </item>
-         <item row="1" column="0">
-          <widget class="QGraphicsView" name="graphicsView_6"/>
-         </item>
-         <item row="1" column="1">
-          <widget class="QGraphicsView" name="graphicsView_5"/>
-         </item>
-         <item row="1" column="2">
-          <widget class="QGraphicsView" name="graphicsView_4"/>
-         </item>
-         <item row="2" column="0">
-          <widget class="QGraphicsView" name="graphicsView_9"/>
-         </item>
-         <item row="2" column="1">
-          <widget class="QGraphicsView" name="graphicsView_8"/>
-         </item>
-         <item row="2" column="2">
-          <widget class="QGraphicsView" name="graphicsView_7"/>
+         <item>
+          <widget class="QPushButton" name="btn_save">
+           <property name="text">
+            <string>保存图像</string>
+           </property>
+          </widget>
          </item>
         </layout>
        </widget>
       </item>
+      <item>
+       <layout class="QHBoxLayout" name="horizontalLayout" stretch="1,7">
+        <item>
+         <layout class="QVBoxLayout" name="verticalLayout">
+          <item>
+           <widget class="QTreeView" name="tree_set_2">
+            <property name="minimumSize">
+             <size>
+              <width>150</width>
+              <height>0</height>
+             </size>
+            </property>
+            <property name="maximumSize">
+             <size>
+              <width>150</width>
+              <height>16777215</height>
+             </size>
+            </property>
+           </widget>
+          </item>
+          <item>
+           <widget class="QTreeView" name="tree_set">
+            <property name="minimumSize">
+             <size>
+              <width>150</width>
+              <height>0</height>
+             </size>
+            </property>
+            <property name="maximumSize">
+             <size>
+              <width>150</width>
+              <height>16777215</height>
+             </size>
+            </property>
+           </widget>
+          </item>
+         </layout>
+        </item>
+        <item>
+         <widget class="QGroupBox" name="groupBox_2">
+          <property name="maximumSize">
+           <size>
+            <width>16777215</width>
+            <height>16777215</height>
+           </size>
+          </property>
+          <property name="title">
+           <string/>
+          </property>
+          <layout class="QGridLayout" name="gridLayout">
+           <property name="leftMargin">
+            <number>1</number>
+           </property>
+           <property name="topMargin">
+            <number>1</number>
+           </property>
+           <property name="rightMargin">
+            <number>1</number>
+           </property>
+           <property name="bottomMargin">
+            <number>1</number>
+           </property>
+           <property name="spacing">
+            <number>1</number>
+           </property>
+           <item row="0" column="0">
+            <widget class="QChartView" name="gra_scope11"/>
+           </item>
+          </layout>
+         </widget>
+        </item>
+       </layout>
+      </item>
      </layout>
     </item>
    </layout>
@@ -140,13 +161,20 @@
     <rect>
      <x>0</x>
      <y>0</y>
-     <width>1031</width>
+     <width>1115</width>
      <height>25</height>
     </rect>
    </property>
   </widget>
   <widget class="QStatusBar" name="statusbar"/>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>QChartView</class>
+   <extends>QGraphicsView</extends>
+   <header location="global">qchartview.h</header>
+  </customwidget>
+ </customwidgets>
  <resources>
   <include location="../../icon/icon.qrc"/>
   <include location="../../icon/icon.qrc"/>

+ 94 - 53
code/tcp/tcp.cpp

@@ -1,34 +1,30 @@
 #include "tcp.h"
 #include "ui_tcp.h"
 
-
-tcp::tcp(QWidget *parent, scope *pageScope) :
-    QMainWindow(parent),
-    ui(new Ui::tcp),
-    page_scope(pageScope)  // 初始化 scope
+tcp::tcp(QWidget *parent, scope *pageScope) : QMainWindow(parent),
+                                              ui(new Ui::tcp),
+                                              page_scope(pageScope) // 初始化 scope
 {
     ui->setupUi(this);
     ui->port->setText("8899");
     setWindowTitle("tcp设置");
-    connectNum=0;
+    connectNum = 0;
     ui->disconnect->setDisabled(true);
     ui->sendMsg->setEnabled(true);
     // 创建监听的服务器对象
     ms = new QTcpServer(this);
-    connect(ms, &QTcpServer::newConnection, this, [=]() {
-        QTcpSocket* clientSocket = ms->nextPendingConnection();
-        mstatus->setPixmap(QPixmap(":/accessibility.svg").scaled(20, 20));
-
-        ui->disconnect->setEnabled(true);
-
-        // 将客户端连接添加到连接管理中
-        connectedClients[clientSocket] = "";
+    connect(ms, &QTcpServer::newConnection, this, [=]()
+            {
+                QTcpSocket *clientSocket = ms->nextPendingConnection();
+                mstatus->setPixmap(QPixmap(":/accessibility.svg").scaled(20, 20));
 
+                ui->disconnect->setEnabled(true);
 
-        // 增加处理连接的信号和槽
-        connectClient(clientSocket);
+                // 将客户端连接添加到连接管理中
+                connectedClients[clientSocket] = "";
 
-    });
+                // 增加处理连接的信号和槽
+                connectClient(clientSocket); });
 
     // 状态栏处理动作
     mstatus = new QLabel;
@@ -44,20 +40,21 @@ tcp::~tcp()
 
 void tcp::on_setListen_clicked()
 {
-    unsigned short port = ui->port->text().toUShort();  // 获取端口号
-    ms->listen(QHostAddress::Any, port);  // 监听所有地址上的指定端口
-    ui->setListen->setDisabled(true);  // 禁用监听按钮
-    page_scope->Chart_Init();  // 使用 page_scope 指针调用方法
+    unsigned short port = ui->port->text().toUShort(); // 获取端口号
+    ms->listen(QHostAddress::Any, port);               // 监听所有地址上的指定端口
+    ui->setListen->setDisabled(true);                  // 禁用监听按钮
+    page_scope->Chart_Init();                          // 使用 page_scope 指针调用方法
     page_scope->show();
-
+    this->hide();                                  // 隐藏本窗口
 }
 
 void tcp::connectClient(QTcpSocket *clientSocket)
 {
     connectNum++;
-    connect(clientSocket, &QTcpSocket::readyRead, this, [=]() {
+    connect(clientSocket, &QTcpSocket::readyRead, this, [=]()
+            {
 
-        ui->usermsg->append("matlab连接");
+        ui->usermsg->append("matlab发送信息");
         QByteArray data = clientSocket->readAll();
         if (data.isEmpty())
             return;
@@ -71,53 +68,97 @@ void tcp::connectClient(QTcpSocket *clientSocket)
             ui->record->append("收到文本: " + message);
         } else if (dataType == 'D')  {
             // 处理数字数据
-            QVector<double> receivedData;
-            int dataSize = data.size() / sizeof(double);
-
-            for (int i = 0; i < dataSize; ++i) {
-                double value;
-                memcpy(&value, data.data() + i * sizeof(double), sizeof(double));
-                page_scope->DrawLine(value);  // 使用 page_scope 指针调用方法
-
-                receivedData.append(value);
-            }
-            QString numbers;
-            for (double num : receivedData)
-            {
-                numbers += QString::number(num, 'f', 6) + " ";
-            }
-
-            ui->record->append("收到数据: " + numbers.trimmed());
-            QString dataSizeStr = QString::number(dataSize);
-            ui->msg->append("收到数据长度: " + dataSizeStr);
-            data.clear();  // 清空 data
-            page_scope->Chart_Init();
+        QVector<QPointF> receivedData;
+        int dataSize = data.size() / sizeof(double);
+
+
+        for (int i = 0; i < dataSize; ++i) {
+            double value;
+            memcpy(&value, data.data() + i * sizeof(double), sizeof(double));
+            if (i>0)
+            { 
+            page_scope->DrawLine(value);
+            }// 绘制曲线解释代码:
+            receivedData.append(QPointF(i, value));
+        }
 
+        QString numbers;
+        for (const QPointF& point : receivedData) {
+            numbers += QString::number(point.y(), 'f', 6) + " ";
         }
-    });
 
-    connect(clientSocket, &QTcpSocket::disconnected, this, [=]() {
+
+        QString dataSizeStr = QString::number(dataSize);
+        ui->record->append("收到数据长度: " + dataSizeStr + "  数据: " + numbers.trimmed());
+        ui->msg->append(numbers.trimmed());
+        
+        on_sendMsg_clicked();
+        
+
+        data.clear();  // 清空 data
+        } });
+
+    connect(clientSocket, &QTcpSocket::disconnected, this, [=]()
+            {
         clientSocket->close();
         connectedClients.remove(clientSocket);
         connectNum = 0;
 
         // 更新状态栏
         if (connectNum == 0) {
-            mstatus->setPixmap(QPixmap(":/f.PNG").scaled(20, 20));
+            mstatus->setPixmap(QPixmap(":/chat-bubble-xmark.svg").scaled(20, 20));
             ui->usermsg->append("matlab断开连接");
-        }
-    });
+        } }); 
+        
+}
+
+// 接受TCP传输的文件
+
+void tcp::receiveFile(const QString &filename, const QByteArray &data)
+{
+    QString savePath = "./" + filename;
+    QFile file(savePath);
+    if (file.open(QIODevice::WriteOnly)) {
+        // 进行Base64解码
+        QByteArray decodedData = QByteArray::fromBase64(data);
+
+        // 将解码后的数据写入文件
+        file.write(decodedData);
+
+        file.close();
+        ui->record->append("成功接收到文件: " + filename);
+
+
+    } 
 }
 
-void tcp::on_sendMsg_clicked()
+void tcp::on_sendMsg_clicked() // 接受输入的消息并 发送消息
 {
+    
+    // 发送数字1
+
+    // QByteArray data;
+    // data.append('1');
+    // for (auto clientSocket : connectedClients.keys())
+    // {
+    //     // 遍历所有连接的客户端
+    //     if (clientSocket->state() == QAbstractSocket::ConnectedState)
+    //     {
+    //         clientSocket->write(data); // 发送数据
+    //     }
+    // }
+
+
+    // 发送ui->msg里面输入的文本
     QString msg = ui->msg->toPlainText();  // 获取要发送的消息
-    QString serverMsg = "[Server] " + msg;  // 添加服务器标识
+    QString serverMsg =  'D'+msg;  // 添加服务器标识
     for (auto clientSocket : connectedClients.keys()) {
         if (clientSocket->state() == QAbstractSocket::ConnectedState) {
             clientSocket->write(serverMsg.toUtf8());  // 发送消息
         }
     }
     ui->record->append("服务端广播消息: " + msg);  // 在记录中显示发送的消息
-}
 
+    // 清空消息框
+    ui->msg->clear();
+}

+ 1 - 1
code/tcp/tcp.h

@@ -25,7 +25,7 @@ private slots:
     void connectClient(QTcpSocket *clientSocket);
 
     void on_sendMsg_clicked();
-
+    void receiveFile(const QString &filename, const QByteArray &data);
 private:
     Ui::tcp *ui;
     QTcpServer* ms;

+ 7 - 0
code/tree/tree_model_set.cpp

@@ -19,12 +19,19 @@ QStandardItemModel* tree_model_set::tree_set()
     QStandardItem *childItem1 = new QStandardItem("TCP设置");
     childItem1->setFlags(childItem1->flags() & ~Qt::ItemIsEditable);
     childItem1->setData("page_tcp", Qt::UserRole);
+
+
     QStandardItem *childItem2 = new QStandardItem("工况设置");
     //childItem2->setFlags(childItem2->flags() & ~Qt::ItemIsEditable);
     childItem2->setData("page_set", Qt::UserRole);
+
+    QStandardItem *childItem3 = new QStandardItem("曲线显示");
+    childItem3->setFlags(childItem3->flags() & ~Qt::ItemIsEditable);
+    childItem3->setData("page_chart", Qt::UserRole);
     // 将子节点添加到根节点
     rootItem->appendRow(childItem1);
     rootItem->appendRow(childItem2);
+    rootItem->appendRow(childItem3);
     // 将根节点添加到模型
     model->appendRow(rootItem);
     // 将模型设置为 QTreeView 的模型

+ 13 - 0
code/tree/tree_model_set.h

@@ -0,0 +1,13 @@
+#ifndef TREE_MODEL_SET_H
+#define TREE_MODEL_SET_H
+
+#include <QStandardItemModel>
+
+class tree_model_set
+{
+public:
+    tree_model_set();
+    QStandardItemModel* tree_set();
+};
+
+#endif // TREE_MODEL_SET_H

Plik diff jest za duży
+ 12686 - 0
data/3.txt