QT基础 QChart绘制折线

目录

 

1.简单折线

 2.数学折线

3.可滑动折线


 

1.简单折线

//![1]
//! 折现段坐标QLineSeries *series = new QLineSeries();
//![1]//![2]
//! 添加点series->append(0, 6);series->append(2, 4);series->append(3, 8);series->append(7, 4);series->append(10, 5);*series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
//![2]//![3]QChart *chart = new QChart();chart->legend()->hide();chart->addSeries(series);//根据已添加到图表中的系列为图表创建轴。以前添加到图表中的任何轴都将被删除chart->createDefaultAxes();chart->setTitle("Simple line chart example");
//![3]//![4]QChartView *chartView = new QChartView(chart);chartView->setRenderHint(QPainter::Antialiasing);
//![4]

 2.数学折线

 QChartView* cview = new QChartView(this);cview->setRenderHints(QPainter::Antialiasing);QChart* chart = new QChart();chart->setTitle("Simple line");cview->setChart(chart);setCentralWidget(cview);QLineSeries* seriesS = new QLineSeries;QLineSeries* seriesC = new QLineSeries;seriesS->setName("Sinθ");seriesC->setName("Cosθ");chart->addSeries(seriesS);chart->addSeries(seriesC);//添加数据绘制qreal y0,y1,t=0,intv=0.1;size_t count=100;for (size_t i=0;i<count;i++){y0 = qSin(t);y1 = qCos(t);seriesS->append(t,y0);seriesC->append(t,y1);t += intv;}QValueAxis* axixX = new QValueAxis;axixX->setRange(0,10);chart->setAxisX(axixX,seriesS);chart->setAxisX(axixX,seriesC);QValueAxis* axixY = new QValueAxis;axixY->setRange(-2,1);chart->setAxisY(axixY,seriesS);chart->setAxisY(axixY,seriesC);

3.可滑动折线

本质逻辑上与上面差不多,但是如果要放大/缩小等操作,需要重写一下QChartView函数,如果是触摸的需要重写QChart的bool sceneEvent(QEvent *event);

    bool viewportEvent(QEvent *event);void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void keyPressEvent(QKeyEvent *event);

chart.h

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#ifndef CHART_H
#define CHART_H#include <QtCharts/QChart>QT_BEGIN_NAMESPACE
class QGestureEvent;
QT_END_NAMESPACEQT_CHARTS_USE_NAMESPACE//![1]
class Chart : public QChart
//![1]
{
public:explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);~Chart();protected:bool sceneEvent(QEvent *event);private:bool gestureEvent(QGestureEvent *event);private:};#endif // CHART_H

chart.cpp

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "chart.h"
#include <QtWidgets/QGesture>
#include <QtWidgets/QGraphicsScene>
#include <QtWidgets/QGraphicsView>Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags): QChart(QChart::ChartTypeCartesian, parent, wFlags)
{// Seems that QGraphicsView (QChartView) does not grab gestures.// They can only be grabbed here in the QGraphicsWidget (QChart).grabGesture(Qt::PanGesture);grabGesture(Qt::PinchGesture);
}Chart::~Chart()
{}//![1]
bool Chart::sceneEvent(QEvent *event)
{if (event->type() == QEvent::Gesture)return gestureEvent(static_cast<QGestureEvent *>(event));return QChart::event(event);
}bool Chart::gestureEvent(QGestureEvent *event)
{if (QGesture *gesture = event->gesture(Qt::PanGesture)) {QPanGesture *pan = static_cast<QPanGesture *>(gesture);QChart::scroll(-(pan->delta().x()), pan->delta().y());}if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)QChart::zoom(pinch->scaleFactor());}return true;
}
//![1]

chartview.h

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#ifndef CHARTVIEW_H
#define CHARTVIEW_H#include <QtCharts/QChartView>
#include <QtWidgets/QRubberBand>QT_CHARTS_USE_NAMESPACE//![1]
class ChartView : public QChartView
//![1]
{
public:ChartView(QChart *chart, QWidget *parent = 0);//![2]
protected:bool viewportEvent(QEvent *event);void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);void keyPressEvent(QKeyEvent *event);
//![2]private:bool m_isTouching;
};#endif

chartview.cpp 

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "chartview.h"
#include <QtGui/QMouseEvent>ChartView::ChartView(QChart *chart, QWidget *parent) :QChartView(chart, parent),m_isTouching(false)
{setRubberBand(QChartView::RectangleRubberBand);
}bool ChartView::viewportEvent(QEvent *event)
{if (event->type() == QEvent::TouchBegin) {// By default touch events are converted to mouse events. So// after this event we will get a mouse event also but we want// to handle touch events as gestures only. So we need this safeguard// to block mouse events that are actually generated from touch.m_isTouching = true;// Turn off animations when handling gestures they// will only slow us down.chart()->setAnimationOptions(QChart::NoAnimation);}return QChartView::viewportEvent(event);
}void ChartView::mousePressEvent(QMouseEvent *event)
{if (m_isTouching)return;QChartView::mousePressEvent(event);
}void ChartView::mouseMoveEvent(QMouseEvent *event)
{if (m_isTouching)return;QChartView::mouseMoveEvent(event);
}void ChartView::mouseReleaseEvent(QMouseEvent *event)
{if (m_isTouching)m_isTouching = false;// Because we disabled animations when touch event was detected// we must put them back on.chart()->setAnimationOptions(QChart::SeriesAnimations);QChartView::mouseReleaseEvent(event);
}//![1]
void ChartView::keyPressEvent(QKeyEvent *event)
{switch (event->key()) {case Qt::Key_Plus:chart()->zoomIn();break;case Qt::Key_Minus:chart()->zoomOut();break;
//![1]case Qt::Key_Left:chart()->scroll(-10, 0);break;case Qt::Key_Right:chart()->scroll(10, 0);break;case Qt::Key_Up:chart()->scroll(0, 10);break;case Qt::Key_Down:chart()->scroll(0, -10);break;default:QGraphicsView::keyPressEvent(event);break;}
}

main.cpp

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/#include "chart.h"
#include "chartview.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCore/QtMath>
#include <QtCore/QRandomGenerator>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>QT_CHARTS_USE_NAMESPACEint main(int argc, char *argv[])
{QApplication a(argc, argv);//![1]QLineSeries *series = new QLineSeries();for (int i = 0; i < 500; i++) {QPointF p((qreal) i, qSin(M_PI / 50 * i) * 100);p.ry() += QRandomGenerator::global()->bounded(20);*series << p;}
//![1]Chart *chart = new Chart();chart->addSeries(series);chart->setTitle("Zoom in/out example");chart->setAnimationOptions(QChart::SeriesAnimations);chart->legend()->hide();chart->createDefaultAxes();ChartView *chartView = new ChartView(chart);chartView->setRenderHint(QPainter::Antialiasing);QMainWindow window;window.setCentralWidget(chartView);window.resize(400, 300);window.grabGesture(Qt::PanGesture);window.grabGesture(Qt::PinchGesture);window.show();return a.exec();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/132203.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Keil 厂商DFP pack实现原理

要想在Keil中方便地通过界面点击来导入芯片厂商提供的库&#xff0c;通常需要安装厂商提供的pack&#xff0c;如下图&#xff1a; 这个过程是如何实现的&#xff1f; 双击安装pack后&#xff0c;pack文件会将自身的内容解压到下图的目录&#xff0c;命名为厂商名字的文件夹&…

Kubernetes新增节点

1. K8S节点Hosts及防火墙设置 node3节点进行如下配置&#xff1a; #添加hosts解析&#xff1b; cat >/etc/hosts<<EOF 127.0.0.1 localhost localhost.localdomain 192.168.1.146 master1 192.168.1.147 node3 EOF #临时关闭selinux和防火墙&#xff1b; sed -i /SE…

canvas基础2 -- 形状

七巧板 七巧板本质上就是 分别由几个直线 拼成一个个图形&#xff0c;再将这些图形结合起来 var tangram [{ p: [{ x: 0, y: 0 }, { x: 800, y: 0 }, { x: 400, y: 400 }], color: "#caff67" },{ p: [{ x: 0, y: 0 }, { x: 400, y: 400 }, { x: 0, y: 800 }], col…

AlphaPose Pytorch 代码详解(一):predict

前言 代码地址&#xff1a;AlphaPose-Pytorch版 本文以图像 1.jpg&#xff08;854x480&#xff09;为例对整个预测过程的各个细节进行解读并记录 python demo.py --indir examples/demo --outdir examples/res --save_img1. YOLO 1.1 图像预处理 cv2读取BGR图像 img [480,…

Redis 的过期键 | Navicat 技术干货

Redis 是一种高性能的内存数据存储&#xff0c;以其速度和多功能性而闻名。其中一个有用的特性是为键设置过期时间的功能。在 Redis 中&#xff0c;为键设置过期时间对于管理数据和确保过时或临时数据自动从数据库中删除是至关重要的。在本文中&#xff0c;我们将探讨在 redis-…

4、在 CentOS 8 系统上安装 pgAdmin 4

pgAdmin 4 是一个开源的数据库管理工具&#xff0c;专门用于管理和操作 PostgreSQL 数据库系统。它提供了一个图形用户界面&#xff08;GUI&#xff09;&#xff0c;使用户能够轻松地连接到 PostgreSQL 数据库实例&#xff0c;执行 SQL 查询&#xff0c;管理数据库对象&#xf…

STM32物联网基于ZigBee智能家居控制系统

实践制作DIY- GC0169-ZigBee智能家居 一、功能说明&#xff1a; 基于STM32单片机设计-ZigBee智能家居 二、功能介绍&#xff1a; 1个主机显示板&#xff1a;STM32F103C最小系统ZigBee无线模块OLED显示器 语音识别模块多个按键ESP8266-WIFI模块&#xff08;仅WIFI版本有&…

java模拟GPT流式问答

流式请求gpt并且流式推送相关前端页面 1&#xff09;java流式获取gpt答案 1、读取文件流的方式 使用post请求数据&#xff0c;由于gpt是eventsource的方式返回数据&#xff0c;所以格式是data&#xff1a;&#xff0c;需要手动替换一下值 /** org.apache.http.client.metho…

QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样 Chapter1 QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样一、本自制虚拟键盘特点二、windows打开系统自带软键盘三、让…

一款轻量级事件驱动型应用程序框架

QP™/C 实时嵌入式框架 &#xff08;RTEF&#xff09; 是专为实时嵌入式 &#xff08;RTE&#xff09; 系统量身定制的活动对象计算模型的轻量级实现。QP 既是用于构建由活动对象&#xff08;参与者&#xff09;组成的应用程序的软件基础结构&#xff0c;也是用于以确定性方式执…

TensorFlow入门(二十三、退化学习率)

学习率 学习率,控制着模型的学习进度。模型训练过程中,如果学习率的值设置得比较大,训练速度会提升,但训练结果的精度不够,损失值容易爆炸;如果学习率的值设置得比较小,精度得到了提升,但训练过程会耗费太多的时间,收敛速度慢,同时也容易出现过拟合的情况。 退化学习率 退化学…

Linux文件与目录的增删改查

一、增 1、mkdir命令 作用: 创建一个新目录。格式: mkdir [选项] 要创建的目录 常用参数: -p:创建目录结构中指定的每一个目录,如果目录不存在则创建,如果目录已存在也不会被覆盖。用法示例: 1、mkdir directory:创建单个目录 这个命令会在当前目录下创建一个名为…