画动态曲线另一种方式方式

1.QChart

在pro文件中加入QT   += charts

在.h文件中加入#include <QtCharts>

代码如下

.h文件

1  QChart *chart;
2  QChartView *chartView;
3  QLineSeries*    _vol_series;

.cpp文件

 1 void widget::initNumber()
 2 {
 3 
 4     //曲线类
 5     _vol_series = new QLineSeries;
 6     _vol_series->setPen(QPen(QColor(160, 217, 246), 4)); 
 7     //画笔
 8     chart = new QChart();
 9     chart->legend()->hide(); // 隐藏图例
10     chart->setTitleBrush(Qt::white);
11     chart->setTitleFont(QFont("Montserrat", 22));
12     chart->setBackgroundBrush(QColor(0, 200, 0, 0));
13     //设置标题
14     chart->setTitle(QObject::tr("Voltage measurements from probe 1 to 2 during pulses 1 to 90"));
15     chart->addSeries(_vol_series);
16     //坐标轴
17     QValueAxis* vol_axisX = new QValueAxis;
18     //X轴
19     vol_axisX->setRange(0, 100);
20     vol_axisX->setVisible(false);
21     //设置是否显示网格线
22     vol_axisX->setGridLineVisible(false);
23     vol_axisX->setTitleText(QObject::tr(""));
24     chart->addAxis(vol_axisX, Qt::AlignBottom);
25     //Y轴
26     QValueAxis* vol_axisY = new QValueAxis;
27     //Y轴分成8份
28     vol_axisY->setTickCount(8);
29     //Y轴的范围
30     vol_axisY->setRange(0, 3500);
31     //Y的格式
32     vol_axisY->setLabelFormat("%i");
33     vol_axisY->setTitleBrush(Qt::white);
34     vol_axisY->setLabelsColor(Qt::white);
35     //
36     vol_axisY->setGridLineVisible(false);
37     vol_axisY->setTitleFont(QFont("Montserrat", 13));
38     vol_axisY->setLabelsFont(QFont("Montserrat", 10));
39     vol_axisY->setTitleBrush(QColor(240, 133, 25));
40     //设置Y轴的标题
41     vol_axisY->setTitleText(QObject::tr("Voltage(V)"));
42     //
43     chart->addAxis(vol_axisY, Qt::AlignLeft);
44     _vol_series->attachAxis(vol_axisX);
45     _vol_series->attachAxis(vol_axisY);
46     //画布
47     chartView = new QChartView(chart, this);
48     chartView->setRenderHints(QPainter::Antialiasing);
49     chartView->setStyleSheet("background:rgb(0, 0, 200, 0);");
50     //
51     chartView->setGeometry(0, 60, 890, 270);
52     timer = new QTimer(this);
53     connect(timer,SIGNAL(timeout()),this,SLOT(ontime()));
54     timer->start(1000);
55 }
56 
57 
58 void widget::ontime()
59 {
60     //产生一个数据
61     appendVolPlotValue(qrand()%3500);
62 }
63 
64 
65 //画线
66 void widget::appendVolPlotValue(int value)
67 {
68     //
69     if(nullptr != _vol_series)
70     {
71         //
72 
73         int count = _vol_series->count()*10;
74         //
75         _vol_series->append(count, value);  //76         //
77         if(count == 890)
78         {
79           _vol_series->clear();
80         }
81         chartView->chart()->axisX()->setRange(0, 890);
82     }else{}
83 }

 

posted on 2019-01-18 14:15  小小小哈  阅读(968)  评论(0编辑  收藏  举报

导航