研二寒假---关于Qt&CV曲线算法问题

目录

1、单曲线输入和绘图

1、单曲线输入和绘图

  1 #include "cv_plot.h"
  2 #include "ui_cv_plot.h"
  3 #include <QFileDialog>    //for getOpenFileName()
  4 #include <QDebug>
  5 
  6 CV_Plot::CV_Plot(QWidget *parent) :
  7     QWidget(parent),
  8     ui(new Ui::CV_Plot)
  9 {
 10     ui->setupUi(this);
 11 
 12     CV_Plot::GraphSetup(ui->widget);    //坐标轴初始化
 13 }
 14 
 15 CV_Plot::~CV_Plot()
 16 {
 17     delete ui;
 18 }
 19 
 20 //绘图前的一些设置
 21 void CV_Plot::GraphSetup(QCustomPlot* customPlot)
 22 {
 23     QLinearGradient plotGradient;                   //QLinearGradient为线性渐变色类
 24     plotGradient.setColorAt(1,QColor(50,50,50));    //设置画布颜色
 25 
 26     ui->widget->xAxis->setLabelColor(Qt::black);              //x轴文字颜色
 27     ui->widget->yAxis->setLabelColor(Qt::black);
 28     ui->widget->xAxis->setTickLabelColor(Qt::black);          //x轴数字颜色
 29     ui->widget->yAxis->setTickLabelColor(Qt::black);
 30     ui->widget->xAxis->setBasePen(QPen(Qt::black,2));         //x轴颜色及宽度
 31     ui->widget->yAxis->setBasePen(QPen(Qt::black,2));
 32     ui->widget->xAxis->setTickPen(QPen(Qt::black,2));         //主刻度
 33     ui->widget->yAxis->setTickPen(QPen(Qt::black,2));
 34     ui->widget->xAxis->setSubTickPen(QPen(Qt::black,2));      //副刻度
 35     ui->widget->yAxis->setSubTickPen(QPen(Qt::black,2));
 36 
 37     ui->widget->xAxis2->setTickLabelColor(Qt::black);         //坐标轴2数字颜色
 38     ui->widget->yAxis2->setTickLabelColor(Qt::black);
 39     ui->widget->xAxis2->setBasePen(QPen(Qt::black, 2));       //坐标轴颜色及宽度
 40     ui->widget->yAxis2->setBasePen(QPen(Qt::black, 2));
 41     ui->widget->xAxis2->setTickPen(QPen(Qt::black, 2));       //主刻度
 42     ui->widget->yAxis2->setTickPen(QPen(Qt::black, 2));
 43     ui->widget->xAxis2->setSubTickPen(QPen(Qt::black, 2));    //副刻度
 44     ui->widget->yAxis2->setSubTickPen(QPen(Qt::black, 2));
 45 
 46     customPlot->xAxis->setRange(-0.2,0.8);          //x轴范围
 47     customPlot->yAxis->setRange(-0.0002,0.0002);
 48     customPlot->xAxis->setLabel("Potential(V)");    //x轴文字
 49     customPlot->yAxis->setLabel("Current(A)");
 50     customPlot->axisRect()->setupFullAxesBox();     //设置句型边框
 51     customPlot->xAxis2->setVisible(true);           //打开x轴上方的x轴
 52     customPlot->xAxis2->setTickLabels(false);       //关闭显式刻度
 53     customPlot->yAxis2->setVisible(true);           //打开坐标轴右方的y轴
 54     customPlot->yAxis2->setTickLabels(false);
 55 }
 56 
 57 //读取一个txt文本文件按钮槽函数
 58 void CV_Plot::on_readFileButton_clicked()
 59 {
 60     QString fileName = QFileDialog::getOpenFileName(this,tr("Choose a file"),tr("*.txt"));  //获取要打开的文件夹名字
 61     if(fileName.isEmpty())
 62         QMessageBox::information(this,tr("Information"),"fileName is empty");               //如果fileName为空则弹出一个窗口
 63     QFile file(fileName);
 64     if(!file.open(QFile::ReadOnly | QFile::Text))
 65     {
 66 
 67         QMessageBox::warning(NULL,"warning","file open failed",QMessageBox::Yes | QMessageBox::No);
 68     }
 69     QTextStream fileIn(&file);    //创建一个文件输入流对象,使fileIn与file绑定
 70 
 71     while(!fileIn.atEnd())
 72     {
 73         str.append(fileIn.readLine());      //都txt文件的一行到str中
 74         QStringList list = str.split(' ');  //以空格分开str
 75         double a = list[0].toDouble();      //将string转换为double
 76         double b = list[1].toDouble();
 77         xdata.append(a);
 78         ydata.append(b);
 79         str.clear();                        //清空str,并读入下一行
 80     }
 81     qDebug() << "xdata.size()=" << xdata.size();
 82     if (fileIn.atEnd())
 83     {
 84         qDebug() << "End" << endl;
 85         file.close();
 86     }
 87     for (int i = 0; i < xdata.size()/2; i++)                  //下曲线数据
 88     {
 89         xdata1.append(xdata[i]);
 90         ydata1.append(ydata[i]);
 91     }
 92     for (int i = (xdata.size() / 2); i < xdata.size(); i++)    //上曲线数据
 93     {
 94         xdata2.append(xdata[i]);
 95         ydata2.append(ydata[i]);
 96     }
 97 }
 98 
 99 
100 //绘图
101 void CV_Plot::GraphShow(QCustomPlot* customPlot)
102 {
103     //设置属性可缩放,移动等
104     ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
105         QCP::iSelectLegend | QCP::iSelectPlottables);
106     customPlot->addGraph();//添加一条曲线
107     customPlot->graph(0)->setPen(QPen(Qt::blue)); //0是曲线序号,添加的第一条是0,设置曲线颜色
108     customPlot->graph(0)->setData(xdata1, ydata1); //输出各点的图像,x和y都是QVector类
109 
110     customPlot->addGraph();  //添加第二条曲线 添加一个曲线图层
111     customPlot->graph(1)->setPen(QPen(Qt::blue));  //1是曲线序号,添加的第二条是1,设置曲线颜色
112     customPlot->graph(1)->setData(xdata2, ydata2);  //参数必须为vector类型
113 
114     customPlot->replot();  //重新绘图 这一个语句会退出此槽函数
115 }
116 
117 
118 //绘图按钮槽函数
119 void CV_Plot::on_dataPlotButton_clicked()
120 {
121     CV_Plot::GraphShow(ui->widget);
122 
123 }
cv_plot.cpp
 1 #ifndef CV_PLOT_H
 2 #define CV_PLOT_H
 3 
 4 #include <QWidget>
 5 #include <QVector>
 6 #include "ui_cv_plot.h"
 7 #include "qcustomplot.h"
 8 
 9 namespace Ui {
10 class CV_Plot;
11 }
12 
13 class CV_Plot : public QWidget
14 {
15     Q_OBJECT
16 
17 public:
18     explicit CV_Plot(QWidget *parent = 0);
19     ~CV_Plot();
20     void GraphShow(QCustomPlot* customPlot);
21     void GraphSetup(QCustomPlot* customPlot);  //绘图前的一些设置
22 
23 private slots:
24     void on_readFileButton_clicked();        //读取txt文件按钮
25     void on_dataPlotButton_clicked();        //绘图按钮
26 
27 private:
28     Ui::CV_Plot *ui;
29     QString str;  //保存文本数据
30     QVector<double> xdata;
31     QVector<double> ydata;
32     QVector<double> xdata1;  //上曲线数据
33     QVector<double> ydata1;  //上曲线数据
34     QVector<double> xdata2;  //下曲线数据
35     QVector<double> ydata2;  //下曲线数据
36 };
37 
38 #endif // CV_PLOT_H
cv_plot,h

工程结构:

posted @ 2020-01-21 23:01  兵临城下的匹夫  阅读(469)  评论(0编辑  收藏  举报
TOP