功能描述:
利用QFile读取在不断刷新的文本文件,并用QtCharts组件进行动态绘制,且x轴可变,能随x的增大而增加。
实现:
对于qt动态曲(折)线图的绘制也用到了定时器QTimer ,每隔x秒则在QtCharts中append一个点。Qt Charts可以绘制很多样式的图形,比如折线、饼图等,可以先浏览下Qt自带的示例中的静态图。注,用到QtCharts,还需在.pro文件中引入 “QT+=charts”。
代码示例:
rightchart.h
#ifndef RIGHTECHART_H
#define RIGHTECHART_H
#include <QWidget>
#include <QChartView>
#include <QtCharts>
#include <QVBoxLayout>
#include <QFile>
#include <QTextStream>
#include <QTimer>
#include <QAbstractScrollArea>
#include <QTextCursor>
#include <QFileInfo>
#include <QStringList>
#include <QDebug>
class RightEChart : public QWidget
{
Q_OBJECT
public:
explicit RightEChart(QWidget *parent = nullptr);
QLineSeries *var1Series;
QLineSeries *var2Series;
QChart *chart;
QChartView *chartView;
QValueAxis *axisY;
QVBoxLayout *vLayout;
QTimer * timerChart;
int allSteps=50;
int lastReadSteps=1;//上次读到的跌代步数step,初始为1
int lastDrawRowNums=0;
QValueAxis *axisX;
signals:
public slots:
void drawFilePoints();
};
#endif // RIGHTECHART_H
rightchart.cpp
#include "rightechart.h"
RightEChart::RightEChart(QWidget *parent) : QWidget(parent)
{
//******chart begain*********
var1Series = new QLineSeries();
var2Series= new QLineSeries();
var1Series->setName("var1");
var2Series->setName("var2");
chart = new QChart();
//增加各变量的点
chart->addSeries(var1Series);
chart->addSeries(var2Series);
chart->setTitle("DYChart");
chart->createDefaultAxes();
//默认坐标轴设置
// chart->axisY()->setRange(0,10);
// chart->axisX()->setRange(0,20);//y轴初始固定值
// chart->axisX()->setTitleText("Accumulated Time Step");
// chart->axisY()->setTitleText("Variable Value");
//chart->axisX()->setMinorTickCount(4);
//自定义x轴以及相关属性
axisX=new QValueAxis;
axisX->setRange(0,20);
axisX->setTitleText("X");//X轴标题
axisX->setGridLineVisible(true);//是否显示网格线
// axisX->setMinorTickCount(4);//小刻度线数目
// axisX->setTickCount(5);//大刻度线数目
axisX->setMinorGridLineVisible(false);
chart->setAxisX(axisX,var1Series);
chart->setAxisX(axisX,var2Series);
//因为y轴是科学计数法,所以需要自定义y轴以及相关属性
axisY=new QValueAxis;
axisY->setRange(0,1);
axisY->setLabelFormat("%E");//设置刻度的格式
axisY->setTitleText("Y");//Y轴标题
axisY->setGridLineVisible(true);//是否显示网格线
// axisY->setMinorTickCount(4);//小刻度线数目
// axisY->setMinorGridLineVisible(false);
axisY->setTickCount(6);//大刻度线数目
chart->setAxisY(axisY,0);
chart->legend()->setVisible(true);//图例的隐藏->hide();
//chart->setAnimationOptions(QChart::SeriesAnimations);//设置曲线呈动画显示
//******chart end********
//显示图标的view
chartView = new QtCharts::QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
vLayout=new QVBoxLayout;
vLayout->addWidget(chartView);
this->setLayout(vLayout);
this->show();
timerChart=new QTimer;
}
void RightEChart::drawFilePoints()
{
QFile outFile(":/resultdata/result/data.txt");
if(!outFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"canot open!";
}
QTextStream stream(&outFile);
//每次读取一行每一行为一个QStringList,
//第一列values[0]:step,
//第二列values[1]:var1,
//第三列values[2]:var2,
//分别声明接受各列变量的QStringList
QStringList stepStrArr,var1StrArr,var2StrArr;
QStringList column;//如果声明到循环内程序崩掉!!!
while(!stream.atEnd()){
QString wholeLine=stream.readLine();
qDebug()<<"wholeLine"<<wholeLine;
//过滤掉文本中的#行,和“variables”行
if (wholeLine.startsWith("#") ||wholeLine.startsWith("variables"))
continue;
//以“ ”为分隔符将每一行划分成多列
column=wholeLine.split(" ",QString::SkipEmptyParts);
stepStrArr.append(column.at(0));//接受column[0]的值
var1StrArr.append(column.at(1));//接受column[1]的值
var2StrArr.append(column.at(2));//接受column[2]的值
}
//将各变量对应的QStringList转换为int或float以便作为点的y值
int stepInt=stepStrArr.at(lastDrawRowNums).toInt();
int maxValueAxisX=stepInt;//声明递增的Y轴刻度
float var1Float=var1StrArr.at(lastDrawRowNums).toFloat();
float var2Float=var2StrArr.at(lastDrawRowNums).toFloat();
if(maxValueAxisX>20){//当step大于20,设置为动态可变x轴
chart->axisX()->setRange(0,maxValueAxisX);
}
var1Series->append(stepInt,var1Float);
var2Series->append(stepInt,var2Float);
lastDrawRowNums++;
//杀掉定时器
if(lastDrawRowNums>allSteps-1){//此处数目设置为文件的最大行数(即step)
qDebug()<<"lastDrawRowNums:"<<lastDrawRowNums;
qDebug()<<"stop timerChart!";
timerChart->stop();
}
}
运行截图:
完整项目代码获取:
https://download.csdn.net/download/vvyingning/10979809