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\(event)); return QChart::event(event); }** **bool Chart::gestureEvent(QGestureEvent \*event) { if (QGesture \*gesture = event-\>gesture(Qt::PanGesture)) { QPanGesture \*pan = static_cast\(gesture); QChart::scroll(-(pan-\>delta().x()), pan-\>delta().y()); }** **if (QGesture \*gesture = event-\>gesture(Qt::PinchGesture)) { QPinchGesture \*pinch = static_cast\(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.

相关推荐
(Charon)7 小时前
【C++/Qt】C++/Qt 实现 TCP Server:支持启动监听、消息收发、日志保存
c++·qt·tcp/ip
2601_949194267 小时前
Python爬虫完整代码拿走不谢
开发语言·爬虫·python
c***89208 小时前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
aq55356008 小时前
C语言、C++和C#:三大编程语言核心差异详解
java·开发语言·jvm
visual_zhang8 小时前
Swift 方法派发机制深度解析 —— 兼与 Objective-C `objc_msgSend` 对比
objective-c·swift
并不喜欢吃鱼8 小时前
从零开始C++----七.继承及相关模型和底层(上篇)
开发语言·c++
沐知全栈开发8 小时前
XML CDATA
开发语言
APIshop8 小时前
Python 爬虫获取闲鱼商品详情 API 接口实战指南
开发语言·爬虫·python
代码羊羊9 小时前
rust-字符串(切片)、元组、结构体、枚举、数组
开发语言·后端·rust
逻辑驱动的ken9 小时前
Java高频面试考点场景题08
java·开发语言·面试·求职招聘·春招