1 | NO CONTENT: new file 100644, binary diff hidden |
|
NO CONTENT: new file 100644, binary diff hidden |
1 | NO CONTENT: new file 100644, binary diff hidden |
|
NO CONTENT: new file 100644, binary diff hidden |
@@ -4,4 +4,19 | |||||
4 | \subtitle |
|
4 | \subtitle | |
5 |
|
5 | |||
6 | The example shows how to create your own custom zooming effect with QRubberBand. |
|
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 | void ChartView::keyPressEvent(QKeyEvent *event) |
|
63 | void ChartView::keyPressEvent(QKeyEvent *event) | |
63 | { |
|
64 | { | |
64 | switch (event->key()) { |
|
65 | switch (event->key()) { | |
@@ -68,6 +69,7 void ChartView::keyPressEvent(QKeyEvent *event) | |||||
68 | case Qt::Key_Minus: |
|
69 | case Qt::Key_Minus: | |
69 | m_chart->zoomOut(); |
|
70 | m_chart->zoomOut(); | |
70 | break; |
|
71 | break; | |
|
72 | //![1] | |||
71 | case Qt::Key_Left: |
|
73 | case Qt::Key_Left: | |
72 | m_chart->scrollLeft(); |
|
74 | m_chart->scrollLeft(); | |
73 | break; |
|
75 | break; |
@@ -25,23 +25,25 | |||||
25 |
|
25 | |||
26 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
26 | QTCOMMERCIALCHART_USE_NAMESPACE | |
27 |
|
27 | |||
|
28 | //![1] | |||
28 | class ChartView : public QChartView |
|
29 | class ChartView : public QChartView | |
|
30 | //![1] | |||
29 | { |
|
31 | { | |
30 | public: |
|
32 | public: | |
31 | ChartView(QChart *chart, QWidget *parent = 0); |
|
33 | ChartView(QChart *chart, QWidget *parent = 0); | |
32 |
|
34 | |||
|
35 | //![2] | |||
33 | protected: |
|
36 | protected: | |
34 | void mousePressEvent(QMouseEvent *event); |
|
37 | void mousePressEvent(QMouseEvent *event); | |
35 | void mouseMoveEvent(QMouseEvent *event); |
|
38 | void mouseMoveEvent(QMouseEvent *event); | |
36 | void mouseReleaseEvent(QMouseEvent *event); |
|
39 | void mouseReleaseEvent(QMouseEvent *event); | |
37 | void keyPressEvent(QKeyEvent *event); |
|
40 | void keyPressEvent(QKeyEvent *event); | |
|
41 | //![2] | |||
38 |
|
42 | |||
39 | private: |
|
43 | private: | |
40 | bool rubberBandIsShown; |
|
|||
41 | QRubberBand m_rubberBand; |
|
44 | QRubberBand m_rubberBand; | |
42 | QPoint m_origin; |
|
45 | QPoint m_origin; | |
43 | QChart* m_chart; |
|
46 | QChart* m_chart; | |
44 |
|
||||
45 | }; |
|
47 | }; | |
46 |
|
48 | |||
47 | #endif |
|
49 | #endif |
@@ -30,42 +30,27 int main(int argc, char *argv[]) | |||||
30 | QApplication a(argc, argv); |
|
30 | QApplication a(argc, argv); | |
31 |
|
31 | |||
32 | //![1] |
|
32 | //![1] | |
33 |
QLineSeries* series |
|
33 | QLineSeries* series = new QLineSeries(); | |
34 | QPen blue(Qt::blue); |
|
34 | qreal yValue = 0.0; | |
35 | blue.setWidth(3); |
|
35 | for (int i(0); i < 500; i++) { | |
36 | series0->setPen(blue); |
|
36 | yValue = yValue + (qreal) (qrand() % 10) / 500.0; | |
37 |
|
37 | QPointF value((i + (qreal) rand() / (qreal) RAND_MAX) * (10.0 / 500.0), yValue); | ||
38 | QLineSeries* series1 = new QLineSeries(); |
|
38 | *series << value; | |
39 | QPen red(Qt::red); |
|
39 | } | |
40 | red.setWidth(3); |
|
|||
41 | series1->setPen(red); |
|
|||
42 | //![1] |
|
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 | QChart* chart = new QChart(); |
|
42 | QChart* chart = new QChart(); | |
51 |
|
43 | chart->addSeries(series); | ||
52 | chart->addSeries(series0); |
|
44 | chart->setTitle("Zoom in/out example"); | |
53 | chart->addSeries(series1); |
|
|||
54 | chart->setTitle("Zoom in/out chart example"); |
|
|||
55 | chart->setAnimationOptions(QChart::AllAnimations); |
|
45 | chart->setAnimationOptions(QChart::AllAnimations); | |
56 | //![3] |
|
|||
57 |
|
46 | |||
58 | //![4] |
|
|||
59 |
|
|
47 | ChartView* chartView = new ChartView(chart); | |
60 |
|
|
48 | chartView->setRenderHint(QPainter::Antialiasing); | |
61 | //![4] |
|
|||
62 |
|
49 | |||
63 | //![5] |
|
|||
64 |
|
|
50 | QMainWindow window; | |
65 |
|
|
51 | window.setCentralWidget(chartView); | |
66 |
|
|
52 | window.resize(400, 300); | |
67 |
|
|
53 | window.show(); | |
68 | //![5] |
|
|||
69 |
|
54 | |||
70 | return a.exec(); |
|
55 | return a.exec(); | |
71 | } |
|
56 | } |
General Comments 0
You need to be logged in to leave comments.
Login now