##// END OF EJS Templates
Tuning zoom example (key zooming was removed from QChartView)
Tero Ahola -
r966:69613711a0c3
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
@@ -4,4 +4,19
4 4 \subtitle
5 5
6 6 The example shows how to create your own custom zooming effect with QRubberBand.
7 \image example_zoomlinechart1.png
8 \image example_zoomlinechart2.png
9
10 Let's first create a line series with some example data.
11 \snippet ../examples/zoomlinechart/main.cpp 1
12
13 Then we create a custom chart view by deriving from QChartView:
14 \snippet ../examples/zoomlinechart/chartview.h 1
15
16 We override mouse and key event handling
17 \snippet ../examples/zoomlinechart/chartview.h 2
18
19 Then we implement a custom logic for mouse and key events. For example pressing '+' key will zoom in and pressing
20 the '-' key will zoom out.
21 \snippet ../examples/zoomlinechart/chartview.cpp 1
7 22 */
@@ -59,6 +59,7 void ChartView::mouseReleaseEvent(QMouseEvent *event)
59 59 }
60 60 }
61 61
62 //![1]
62 63 void ChartView::keyPressEvent(QKeyEvent *event)
63 64 {
64 65 switch (event->key()) {
@@ -68,6 +69,7 void ChartView::keyPressEvent(QKeyEvent *event)
68 69 case Qt::Key_Minus:
69 70 m_chart->zoomOut();
70 71 break;
72 //![1]
71 73 case Qt::Key_Left:
72 74 m_chart->scrollLeft();
73 75 break;
@@ -25,23 +25,25
25 25
26 26 QTCOMMERCIALCHART_USE_NAMESPACE
27 27
28 //![1]
28 29 class ChartView : public QChartView
30 //![1]
29 31 {
30 32 public:
31 33 ChartView(QChart *chart, QWidget *parent = 0);
32 34
35 //![2]
33 36 protected:
34 37 void mousePressEvent(QMouseEvent *event);
35 38 void mouseMoveEvent(QMouseEvent *event);
36 39 void mouseReleaseEvent(QMouseEvent *event);
37 40 void keyPressEvent(QKeyEvent *event);
41 //![2]
38 42
39 43 private:
40 bool rubberBandIsShown;
41 44 QRubberBand m_rubberBand;
42 45 QPoint m_origin;
43 46 QChart* m_chart;
44
45 47 };
46 48
47 49 #endif
@@ -30,42 +30,27 int main(int argc, char *argv[])
30 30 QApplication a(argc, argv);
31 31
32 32 //![1]
33 QLineSeries* series0 = new QLineSeries();
34 QPen blue(Qt::blue);
35 blue.setWidth(3);
36 series0->setPen(blue);
37
38 QLineSeries* series1 = new QLineSeries();
39 QPen red(Qt::red);
40 red.setWidth(3);
41 series1->setPen(red);
33 QLineSeries* series = new QLineSeries();
34 qreal yValue = 0.0;
35 for (int i(0); i < 500; i++) {
36 yValue = yValue + (qreal) (qrand() % 10) / 500.0;
37 QPointF value((i + (qreal) rand() / (qreal) RAND_MAX) * (10.0 / 500.0), yValue);
38 *series << value;
39 }
42 40 //![1]
43 41
44 //![2]
45 *series0 << QPointF(0, 6) << QPointF(2, 4) << QPointF(3, 8) << QPointF(7, 4) << QPointF(10, 5);
46 *series1 << QPointF(1, 1) << QPointF(3, 3) << QPointF(7, 6) << QPointF(8, 3) << QPointF(10, 2);
47 //![2]
48
49 //![3]
50 42 QChart* chart = new QChart();
51
52 chart->addSeries(series0);
53 chart->addSeries(series1);
54 chart->setTitle("Zoom in/out chart example");
43 chart->addSeries(series);
44 chart->setTitle("Zoom in/out example");
55 45 chart->setAnimationOptions(QChart::AllAnimations);
56 //![3]
57 46
58 //![4]
59 ChartView* chartView = new ChartView(chart);
60 chartView->setRenderHint(QPainter::Antialiasing);
61 //![4]
47 ChartView* chartView = new ChartView(chart);
48 chartView->setRenderHint(QPainter::Antialiasing);
62 49
63 //![5]
64 QMainWindow window;
65 window.setCentralWidget(chartView);
66 window.resize(400, 300);
67 window.show();
68 //![5]
50 QMainWindow window;
51 window.setCentralWidget(chartView);
52 window.resize(400, 300);
53 window.show();
69 54
70 55 return a.exec();
71 56 }
General Comments 0
You need to be logged in to leave comments. Login now