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.

相关推荐
Funny Valentine-js10 分钟前
swift菜鸟教程1-5(语法,变量,类型,常量,字面量)
开发语言·ios·swift
上线之叁29 分钟前
小迪安全-tp框架反序列化,利用链,rce执行,文件删除
java·开发语言
躺着听Jay31 分钟前
QCustomPlot-相关优化
java·qt·算法
新知图书1 小时前
第一个Qt开发的OpenCV程序
开发语言·qt
梦里藍天1 小时前
QT 调用动态链接库
qt
阿斌_bingyu7091 小时前
Arduino开发物联网ESP32快速入门指南(包含开发语言说明、学习路径和实战教程)
开发语言·物联网·学习
这个懒人2 小时前
linux下io操作详细解析
开发语言·c++·io
东方醴歌2 小时前
VMware安装飞牛私有云fnOS并挂载小雅Alist实现异地远程访问
开发语言·后端·golang
暗影~行星2 小时前
C语言,原码、补码、反码
c语言·开发语言
晨曦5432103 小时前
python——正则表达式
开发语言·正则表达式