QtCharts模块勾画折线和曲线图
QtCharts画线图主要三个部分组成 QLIneSeries或QSplineSeries用于保存联系的坐标位置数据,QChart用于管理图像显示,例如图例,坐标主题等,QChartView则用于显示。
上代码
#ifndef WIDGET_H
#define WIDGET_H
#define SPLINE #include <QWidget>
#include <QList>
#include <QTimerEvent>
#include <QtCharts/QLineSeries>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE class Widget : public QWidget
{
Q_OBJECT public:
Widget(QWidget *parent = nullptr);
~Widget(); protected:
void timerEvent(QTimerEvent *event);
private:
void dataRefresh(QList<float> *t);
Ui::Widget *ui;
int _timerid;
int _x = 20;
int _y = 80;
int _lineNumber = 10;
QList<QList<float >*> *_data;
#ifndef SPLINE
QList<QtCharts::QLineSeries*> *_lines;
#else
QList<QtCharts::QSplineSeries*> *_splines;
#endif
QtCharts::QChart * _chart;
QtCharts::QChartView * _chartView;
int count = 0;
};
#endif // WIDGET_H
下面是cpp
#include "widget.h"
#include "./ui_widget.h"
#include <QDateTime>
#include <QtWidgets/QHBoxLayout>
#include <QtCharts/QValueAxis>
#include <QDebug> Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
_data = new QList<QList<float >*>();
#ifndef SPLINE
_lines = new QList<QtCharts::QLineSeries*>();
#else
_splines = new QList<QtCharts::QSplineSeries*>();
#endif
_chart = new QtCharts::QChart();
for(int i = 0; i< _lineNumber; i++)
{
#ifndef SPLINE
QtCharts::QLineSeries *s = new QtCharts::QLineSeries();
connect(s, &QtCharts::QLineSeries::clicked,[](const QPointF& pointF){qDebug()<< pointF;});
_lines->push_back(s);
#else
QtCharts::QSplineSeries *s = new QtCharts::QSplineSeries();
connect(s, &QtCharts::QSplineSeries::clicked,[](const QPointF& pointF){qDebug()<<pointF;});
_splines->push_back(s);
#endif s->setPointLabelsClipping(false);
s->setPointLabelsVisible(true);
s->setPointsVisible(true);
s->setPointLabelsFormat(QStringLiteral("(@xPoint,@yPoint)"));
s->setName(QString::fromStdString("温度")+QString::number(i));
//文档说明使用OpenGL加速可以更快的的勾画并且支持更多的点,但是会缺乏一些显示功能相对与CPU渲染
// s->setUseOpenGL(true);
qDebug()<<"OpenGL:"<<s->useOpenGL();
_chart->addSeries(s);
}
_chart->setTitle(QString("曲线图"));
QtCharts::QValueAxis *x = new QtCharts::QValueAxis();
x->setRange(0, _x);
x->setTitleText(QString::fromStdString("min"));
x->setTickCount(30);
QtCharts::QValueAxis *y = new QtCharts::QValueAxis();
y->setRange(0, _y);
y->setTitleText(QString::fromStdString("摄氏度"));
y->setTickCount(20);
_chart->addAxis(x,Qt::AlignBottom);
_chart->addAxis(y,Qt::AlignLeft);
for(int i = 0; i< _lineNumber; i++){
#ifndef SPLINE
(*_lines)[i]->attachAxis(x);
(*_lines)[i]->attachAxis(y);
#else
(*_splines)[i]->attachAxis(x);
(*_splines)[i]->attachAxis(y);
#endif
} _chart->legend()->show();
// _chart->createDefaultAxes();
_chartView = new QtCharts::QChartView(_chart);
_chartView->setRenderHint(QPainter::Antialiasing);
// _chartView->setRubberBand(QtCharts::QChartView::RectangleRubberBand);
QHBoxLayout *h_layout = new QHBoxLayout();
h_layout->addWidget(_chartView);
setLayout(h_layout);
_timerid = startTimer(1000);
qsrand(QDateTime::currentDateTime().toTime_t()); } Widget::~Widget()
{
delete ui;
}
void Widget::timerEvent(QTimerEvent *event) {
if(event->timerId() == _timerid)
{
QList<float > *v_list = new QList<float >();
for (int i = 0; i < _lineNumber; i++) {
float v = qrand() % _y;
v_list->push_back(v);
}
dataRefresh(v_list);
}
} void Widget::dataRefresh(QList<float> *t) {
if(_data->length() >= _x)
{
_data->clear();
#ifndef SPLINE
for(int i=0; i < _lines->length();i++)
{
(*_lines)[i]->clear();
}
#else
for (int i = 0; i < _splines->length(); ++i) {
(*_splines)[i]->clear();
}
#endif
}
_data->push_back(t);
for (int i = 0; i < _lineNumber; ++i) {
qDebug()<<_data->length()-1<<(*_data->last())[i];
#ifndef SPLINE
(*_lines)[i]->append((_data->length()-1),(*_data->last())[i]);
#else
(*_splines)[i]->append((_data->length()-1), (*_data->last())[i]);
#endif
}
qDebug()<< ++count; }
本人是在ubuntu18.04平台 Qt版本5.12.6下进行的测试,用cmake进行的组织,使用了一个SPLINE宏进行了两个折线和曲线的切换。测试时发现series使用OpenGl渲染出现明显锯齿并且无法显示坐标点图标,所以本模所有功能块未对GPU渲染提供完全支持,官方文档也提供了提示。

如过要使用OpenGL加速有较多的限制,只有自己权衡了。
QtCharts模块勾画折线和曲线图的更多相关文章
- QtCharts模块在QtWideget中图表绘制(非QML)
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QtCharts模块在QtWideget中图表绘制(非QML) 本文地址:http:/ ...
- qml: QtCharts模块的使用(基本配置)------<一>
QtCharts模块可以用于绘制图表: 导入模块: import QtCharts 2.2 例子: import QtQuick 2.0 import QtCharts 2.2 ChartView { ...
- qml: QtCharts模块得使用(数据整合和显示) ---- <二>
QtCharts目前已经可以免费使用,而且使用非常方便.快捷,并且提供了各种类别的支持(例如:曲线图,柱形图,折线图,饼图等). 这里讲解qml端图表显示,C++端进行数据整合,并能实现实时数据刷新( ...
- python的turtle模块画折线图
代码如下: import turtle yValues = [10.0,7.4,6.4,5.3,4.4,3.7,2.6] def main(): t = turtle.Turtle() t.hidet ...
- 47.QT-QChart之曲线图,饼状图,条形图使用
1.使用准备 在pro中, 添加QT+= charts 然后在界面头文件中添加头文件并声明命名空间,添加: #include <QtCharts> QT_CHARTS_USE_NAMES ...
- Android实现天气预报温度/气温折线趋势图
Android实现天气预报温度/气温折线趋势图 天气预报的APP应用中,难免会遇到绘制天气温度/气温,等关于数据趋势的折线或者曲线图,这类关于气温/温度的折线图,通常会有两条线.一条是高温线,一 ...
- Python中好用的模块们
目录 Python中好用的模块们 datetime模块 subprocess模块 matplotlib折线图 importlib模块 Python中好用的模块们 datetime模块 相信我们都使 ...
- JHChart iOS图表工具库1.0.3新版本详解
前言. 从2016年4月14日开始,本人着手开发了JHChart图表工具库.经过断断续续的开发,截止到现在,已经实现了折线图.柱状图.饼状图.环形图和表格样式的图表功能.为了方便使用,我已经将一个简单 ...
- 基于ssh框架的highcharts前后台数据交互实例
Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表,并且免费提供给个人学习.个人网站和非商业用途使用.HighCh ...
随机推荐
- 测试开发jmeter forEach控制器
测试开发jmeter forEach控制器 forEach控制器的使用场景:主要是对大量数据轮询就行接口请求 forEach控制器的使用前提:将数据进行参数化 测试开发jmeter forEach控制 ...
- 研发效能|DevOps 已死平台工程永存带来的焦虑
最近某位大神在推特上发了一个帖子,结果引来了国内众多卖课机构.培训机构的狂欢,开始贩卖焦虑,其实「平台工程」也不是什么特别高深莫测的东西.闲得无聊,把这位大神的几个帖子薅了下来,你看过之后就会觉得没啥 ...
- Typora图床上传配置:PicGo+Gitee 不完全指南
每次写Markdown都要手动传图,再复制链接到Typora里,这样比较繁琐. 设置好图床,搭配PicGo,写作时直接剪贴图片到Typora,就能实现自动上传,这样就方便很多. Gitee配置: 许多 ...
- Codeforces Global Round 23 D.Paths on the Tree(记忆化搜索)
https://codeforces.ml/contest/1746/problem/D 题目大意:一棵n节点有根树,根节点为1,分别有两个数组 s[i] 顶点 i 的魅力值 c[i] 覆盖顶点 i ...
- FastApi学习
vscode配置 插件 code runner在 setting.json中关于python的修改为,因为我使用了虚拟环境,得让vscode找到python的路径 "code-runner. ...
- Go语言正/反向代理的姿势
先重温一下什么叫反向代理,正向代理. 鹅厂二面,nginx回忆录 所谓正向,反向代理取决于代理的是出站请求,还是入站请求. 正向代理: 代理的出站请求, 客户端能感知到代理程序,架构上距离客户端更近. ...
- C#字典出错“集合已经修改,可能无法执行枚举操作”
出现这个现象的原因是由于线程安全考虑,如果你边对字典循环,又同时移除字典中的某个键值对, 那么将会出现这种错误,解决这种问题的方法是你没次remove某个键值对后需要break结束对字典的循环.
- C#多线程之同步基础篇
目录 一.基本概念 二.锁构造 Monitor Mutex 死锁 三.信号构造 Semaphore ManualResetEvent AutoResetEvent CountdownEvent 四.等 ...
- RocketMQ 在物流行业的应用与运维
本文作者:丁威 - 中通快递资深架构师,<RocketMQ技术内幕>作者,Apache RocketMQ社区首席布道师,公众号「中间件兴趣圈」维护者. 01 物流行业的业务特点 物流行业有 ...
- Class文件解析
1 准备工作 获取class文件byte[] public static byte[] getFileBytes(File file) { try (FileInputStream fileInput ...