QT chart案例

examples-zoomlinechart.qdoc

Zoom Line Example | Qt Charts 5.15.2

Qt 5.15

Qt Charts

Zoom Line Example

Qt Charts | Commercial or GPLv3

Contents

Running the Example

Customizing Zooming Effects

Zoom Line Example

$$$zoomlinechart-brief

The example shows how to create your own custom zooming effect.

@@@zoomlinechart

$$$zoomlinechart-description

The example shows how to create your own custom zooming effect with QRubberBand by using a mouse and how to use touch gestures for paning and zooming.

Running the Example

To run the example from Qt Creator, open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

Customizing Zooming Effects

Let's first create a line series with some example data.
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;
}

Then we create a custom chart view by deriving from QChartView:

class ChartView : public QChartView

We override mouse and key event handling
protected:
bool viewportEvent(QEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);

Then we implement a custom logic for mouse and key events. For example pressing the '+' key will zoom in and pressing the '-' key will zoom out.
void ChartView::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Plus:
chart()->zoomIn();
break;
case Qt::Key_Minus:
chart()->zoomOut();
break;

We also create our own QChart:

class Chart : public QChart

Where we can handle the gestures:
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;
}

Note that you will need to call grabGesture() to both QMainWindow and QChart.

Example project @ code.qt.io

@@@zoomlinechart

© 2020 The Qt Company Ltd.

Documentation contributions included herein are the copyrights of

their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property

of their respective owners.

相关推荐
biter down14 分钟前
从 0 到 1 搭建 Python 接口自动化测试框架(博客系统实战)
开发语言·python
threelab2 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
武器大师722 小时前
lv_binding_js 代码解读
开发语言·javascript·ecmascript
不知名的老吴2 小时前
线程的生命周期之线程“插队“
java·开发语言·python
kaikaile19953 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
秋94 小时前
Go语言(Golang)开发工程师全景解析:岗位职责·语言优势与使用场景·各城市薪资·发展前景·高考志愿填报(2026版)
开发语言·golang·高考
huangdong_5 小时前
1688商品图片采集技术解析:登录态处理与SKU图自动分类
开发语言
chase_my_dream5 小时前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
Cloud_Shy6185 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
天佑木枫6 小时前
15天Python入门系列 · 序
开发语言·python