一生一世学坛

 找回密码
 立即注册
搜索
查看: 5552|回复: 0
打印 上一主题 下一主题

Qt坐标系展示图表

[复制链接]

334

主题

385

帖子

6816

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6816
跳转到指定楼层
楼主
发表于 2022-7-1 15:03:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Charts module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:GPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU
  19. ** General Public License version 3 or (at your option) any later version
  20. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  21. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  22. ** included in the packaging of this file. Please review the following
  23. ** information to ensure the GNU General Public License requirements will
  24. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  25. **
  26. ** $QT_END_LICENSE$
  27. **
  28. ****************************************************************************/

  29. #ifndef CHART_H
  30. #define CHART_H

  31. #include <QtCharts/QChart>

  32. QT_BEGIN_NAMESPACE
  33. class QGestureEvent;
  34. QT_END_NAMESPACE

  35. QT_CHARTS_USE_NAMESPACE

  36. //![1]
  37. class Chart : public QChart
  38. //![1]
  39. {
  40. public:
  41.     explicit Chart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
  42.     ~Chart();

  43. protected:
  44.     bool sceneEvent(QEvent *event);

  45. private:
  46.     bool gestureEvent(QGestureEvent *event);

  47. private:

  48. };

  49. #endif // CHART_H
  50. /****************************************************************************
  51. **
  52. ** Copyright (C) 2016 The Qt Company Ltd.
  53. ** Contact: https://www.qt.io/licensing/
  54. **
  55. ** This file is part of the Qt Charts module of the Qt Toolkit.
  56. **
  57. ** $QT_BEGIN_LICENSE:GPL$
  58. ** Commercial License Usage
  59. ** Licensees holding valid commercial Qt licenses may use this file in
  60. ** accordance with the commercial license agreement provided with the
  61. ** Software or, alternatively, in accordance with the terms contained in
  62. ** a written agreement between you and The Qt Company. For licensing terms
  63. ** and conditions see https://www.qt.io/terms-conditions. For further
  64. ** information use the contact form at https://www.qt.io/contact-us.
  65. **
  66. ** GNU General Public License Usage
  67. ** Alternatively, this file may be used under the terms of the GNU
  68. ** General Public License version 3 or (at your option) any later version
  69. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  70. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  71. ** included in the packaging of this file. Please review the following
  72. ** information to ensure the GNU General Public License requirements will
  73. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  74. **
  75. ** $QT_END_LICENSE$
  76. **
  77. ****************************************************************************/

  78. #ifndef CHARTVIEW_H
  79. #define CHARTVIEW_H

  80. #include <QtCharts/QChartView>
  81. #include <QtWidgets/QRubberBand>

  82. QT_CHARTS_USE_NAMESPACE

  83. //![1]
  84. class ChartView : public QChartView
  85. //![1]
  86. {
  87. public:
  88.     ChartView(QChart *chart, QWidget *parent = 0);

  89. //![2]
  90. protected:
  91.     bool viewportEvent(QEvent *event);
  92.     void mousePressEvent(QMouseEvent *event);
  93.     void mouseMoveEvent(QMouseEvent *event);
  94.     void mouseReleaseEvent(QMouseEvent *event);
  95.     void keyPressEvent(QKeyEvent *event);
  96.     void wheelEvent(QWheelEvent*event);
  97. //![2]

  98. private:
  99.     bool m_isTouching;
  100. };

  101. #endif


  102. /****************************************************************************
  103. **
  104. ** Copyright (C) 2016 The Qt Company Ltd.
  105. ** Contact: https://www.qt.io/licensing/
  106. **
  107. ** This file is part of the Qt Charts module of the Qt Toolkit.
  108. **
  109. ** $QT_BEGIN_LICENSE:GPL$
  110. ** Commercial License Usage
  111. ** Licensees holding valid commercial Qt licenses may use this file in
  112. ** accordance with the commercial license agreement provided with the
  113. ** Software or, alternatively, in accordance with the terms contained in
  114. ** a written agreement between you and The Qt Company. For licensing terms
  115. ** and conditions see https://www.qt.io/terms-conditions. For further
  116. ** information use the contact form at https://www.qt.io/contact-us.
  117. **
  118. ** GNU General Public License Usage
  119. ** Alternatively, this file may be used under the terms of the GNU
  120. ** General Public License version 3 or (at your option) any later version
  121. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  122. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  123. ** included in the packaging of this file. Please review the following
  124. ** information to ensure the GNU General Public License requirements will
  125. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  126. **
  127. ** $QT_END_LICENSE$
  128. **
  129. ****************************************************************************/

  130. #include "chart.h"
  131. #include <QtWidgets/QGesture>
  132. #include <QtWidgets/QGraphicsScene>
  133. #include <QtWidgets/QGraphicsView>

  134. Chart::Chart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
  135.     : QChart(QChart::ChartTypeCartesian, parent, wFlags)
  136. {
  137.     // Seems that QGraphicsView (QChartView) does not grab gestures.
  138.     // They can only be grabbed here in the QGraphicsWidget (QChart).
  139.     grabGesture(Qt::PanGesture);
  140.     grabGesture(Qt::PinchGesture);
  141. }

  142. Chart::~Chart()
  143. {

  144. }

  145. //![1]
  146. bool Chart::sceneEvent(QEvent *event)
  147. {
  148.     if (event->type() == QEvent::Gesture)
  149.         return gestureEvent(static_cast<QGestureEvent *>(event));
  150.     return QChart::event(event);
  151. }

  152. bool Chart::gestureEvent(QGestureEvent *event)
  153. {
  154.     if (QGesture *gesture = event->gesture(Qt::PanGesture)) {
  155.         QPanGesture *pan = static_cast<QPanGesture *>(gesture);
  156.         QChart::scroll(-(pan->delta().x()), pan->delta().y());
  157.     }

  158.     if (QGesture *gesture = event->gesture(Qt::PinchGesture)) {
  159.         QPinchGesture *pinch = static_cast<QPinchGesture *>(gesture);
  160.         if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged)
  161.             QChart::zoom(pinch->scaleFactor());
  162.     }

  163.     return true;
  164. }
  165. //![1]


  166. /****************************************************************************
  167. **
  168. ** Copyright (C) 2016 The Qt Company Ltd.
  169. ** Contact: https://www.qt.io/licensing/
  170. **
  171. ** This file is part of the Qt Charts module of the Qt Toolkit.
  172. **
  173. ** $QT_BEGIN_LICENSE:GPL$
  174. ** Commercial License Usage
  175. ** Licensees holding valid commercial Qt licenses may use this file in
  176. ** accordance with the commercial license agreement provided with the
  177. ** Software or, alternatively, in accordance with the terms contained in
  178. ** a written agreement between you and The Qt Company. For licensing terms
  179. ** and conditions see https://www.qt.io/terms-conditions. For further
  180. ** information use the contact form at https://www.qt.io/contact-us.
  181. **
  182. ** GNU General Public License Usage
  183. ** Alternatively, this file may be used under the terms of the GNU
  184. ** General Public License version 3 or (at your option) any later version
  185. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  186. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  187. ** included in the packaging of this file. Please review the following
  188. ** information to ensure the GNU General Public License requirements will
  189. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  190. **
  191. ** $QT_END_LICENSE$
  192. **
  193. ****************************************************************************/

  194. #include "chartview.h"
  195. #include <QtGui/QMouseEvent>

  196. ChartView::ChartView(QChart *chart, QWidget *parent) :
  197.     QChartView(chart, parent),
  198.     m_isTouching(false)
  199. {
  200.     setRubberBand(QChartView::RectangleRubberBand);
  201. }

  202. bool ChartView::viewportEvent(QEvent *event)
  203. {
  204.     if (event->type() == QEvent::TouchBegin) {
  205.         // By default touch events are converted to mouse events. So
  206.         // after this event we will get a mouse event also but we want
  207.         // to handle touch events as gestures only. So we need this safeguard
  208.         // to block mouse events that are actually generated from touch.
  209.         m_isTouching = true;

  210.         // Turn off animations when handling gestures they
  211.         // will only slow us down.
  212.         chart()->setAnimationOptions(QChart::NoAnimation);
  213.     }
  214.     return QChartView::viewportEvent(event);
  215. }

  216. void ChartView::mousePressEvent(QMouseEvent *event)
  217. {
  218.     if (m_isTouching)
  219.         return;
  220.     QChartView::mousePressEvent(event);
  221. }

  222. void ChartView::mouseMoveEvent(QMouseEvent *event)
  223. {
  224.     if (m_isTouching)
  225.         return;
  226.     QChartView::mouseMoveEvent(event);
  227. }

  228. void ChartView::mouseReleaseEvent(QMouseEvent *event)
  229. {
  230.     if (m_isTouching)
  231.         m_isTouching = false;

  232.     // Because we disabled animations when touch event was detected
  233.     // we must put them back on.
  234.     chart()->setAnimationOptions(QChart::SeriesAnimations);

  235.     QChartView::mouseReleaseEvent(event);
  236. }

  237. //![1]
  238. void ChartView::keyPressEvent(QKeyEvent *event)
  239. {
  240.     switch (event->key()) {
  241.     case Qt::Key_Plus:
  242.         chart()->zoomIn();
  243.         break;
  244.     case Qt::Key_Minus:
  245.         chart()->zoomOut();
  246.         break;
  247. //![1]
  248.     case Qt::Key_Left:
  249.         chart()->scroll(-10, 0);
  250.         break;
  251.     case Qt::Key_Right:
  252.         chart()->scroll(10, 0);
  253.         break;
  254.     case Qt::Key_Up:
  255.         chart()->scroll(0, 10);
  256.         break;
  257.     case Qt::Key_Down:
  258.         chart()->scroll(0, -10);
  259.         break;
  260.     default:
  261.         QGraphicsView::keyPressEvent(event);
  262.         break;
  263.     }
  264. }

  265. void ChartView::wheelEvent(QWheelEvent*event)
  266. {
  267.     qreal factor;//缩放因子
  268.     if ( event->delta() > 0 )
  269.         factor = 2.0;
  270.     else
  271.         factor = 0.5;
  272.     //橡皮筋为水平方向时才缩放
  273.     if(event->delta()>0)
  274.     {
  275.         //鼠标的当前位置
  276.        chart()->zoomIn();
  277.     }
  278.     else
  279.     {
  280.         chart()->zoomOut();
  281.     }
  282.     QChartView::wheelEvent(event);
  283. }

  284. /****************************************************************************
  285. **
  286. ** Copyright (C) 2016 The Qt Company Ltd.
  287. ** Contact: https://www.qt.io/licensing/
  288. **
  289. ** This file is part of the Qt Charts module of the Qt Toolkit.
  290. **
  291. ** $QT_BEGIN_LICENSE:GPL$
  292. ** Commercial License Usage
  293. ** Licensees holding valid commercial Qt licenses may use this file in
  294. ** accordance with the commercial license agreement provided with the
  295. ** Software or, alternatively, in accordance with the terms contained in
  296. ** a written agreement between you and The Qt Company. For licensing terms
  297. ** and conditions see https://www.qt.io/terms-conditions. For further
  298. ** information use the contact form at https://www.qt.io/contact-us.
  299. **
  300. ** GNU General Public License Usage
  301. ** Alternatively, this file may be used under the terms of the GNU
  302. ** General Public License version 3 or (at your option) any later version
  303. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  304. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  305. ** included in the packaging of this file. Please review the following
  306. ** information to ensure the GNU General Public License requirements will
  307. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  308. **
  309. ** $QT_END_LICENSE$
  310. **
  311. ****************************************************************************/

  312. #include "chart.h"
  313. #include "chartview.h"
  314. #include <QtWidgets/QApplication>
  315. #include <QtWidgets/QMainWindow>
  316. #include <QtCore/QtMath>
  317. #include <QtCore/QRandomGenerator>
  318. #include <QtCharts/QLineSeries>
  319. #include <QtCharts/QValueAxis>

  320. QT_CHARTS_USE_NAMESPACE

  321. int main(int argc, char *argv[])
  322. {
  323.     QApplication a(argc, argv);

  324. //![1]
  325.     QLineSeries *series = new QLineSeries();
  326.     for (int i = 0; i < 500; i++) {
  327.         QPointF p((qreal) i, qSin(M_PI / 50 * i) * 100);
  328.         p.ry() += QRandomGenerator::global()->bounded(20);
  329.         *series << p;
  330.     }

  331. //![1]

  332.     Chart *chart = new Chart();
  333.     chart->addSeries(series);
  334.     chart->setTitle("Zoom in/out example");
  335.     chart->setAnimationOptions(QChart::SeriesAnimations);
  336.     chart->legend()->hide();
  337.     chart->createDefaultAxes();

  338.     ChartView *chartView = new ChartView(chart);
  339.     chartView->setRenderHint(QPainter::Antialiasing);

  340.     QMainWindow window;
  341.     window.setCentralWidget(chartView);
  342.     window.resize(400, 300);
  343.     window.grabGesture(Qt::PanGesture);
  344.     window.grabGesture(Qt::PinchGesture);
  345.     window.show();

  346.     return a.exec();
  347. }
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|分享学习  

GMT+8, 2024-5-2 10:24 , Processed in 0.048543 second(s), 5 queries , File On.

声明:本站严禁任何人以任何形式发表违法言论!

本站内容由网友原创或转载,如果侵犯了您的合法权益,请及时联系处理!© 2017 zamxqun@163.com

皖公网安备 34010402700634号

皖ICP备17017002号-1

快速回复 返回顶部 返回列表