畫動態曲線另外一種方式方式

1.QChartapp

在pro文件中加入QT   += chartside

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

代碼以下spa

.h文件code

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

.cpp文件orm

 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 }
相關文章
相關標籤/搜索