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.

相关推荐
电子云与长程纠缠2 分钟前
UE中使用TGuardValue与TInlineComponentArray数据结构
开发语言·数据结构·学习·ue5·游戏引擎
呜喵王阿尔萨斯32 分钟前
C/C++ const -- 多义混乱
c语言·开发语言·c++
__log32 分钟前
幂等性设计:从“重复提交“到“稳如磐石“的系统防护
java·开发语言·spring boot
spider_xcxc33 分钟前
Helm 部署 K8s 集群完整笔记
java·开发语言·kubernetes
海清河晏11134 分钟前
Qt实战:从零构建美化登录界面
开发语言·c++·qt
一只小灿灿44 分钟前
C++ 各类特殊符号、运算符
开发语言·c++
Fu_Lin_1 小时前
《Qt嵌入式从零基础到精通》前言与阅读指南
开发语言·qt
名字还没想好☜1 小时前
Go 的 select 实战:超时、非阻塞收发与优雅退出的三个套路
开发语言·数据库·golang·go·并发
geovindu1 小时前
java: Backtracking Algorithm
java·开发语言·windows·后端·算法·回溯算法
Scott9999HH9 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python