##// END OF EJS Templates
Updated callout example
Marek Rosa -
r2346:1937d28dc870
parent child
Show More
@@ -0,0 +1,59
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef VIEW_H
22 #define VIEW_H
23 #include <QGraphicsView>
24 #include <QChartGlobal>
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 class QChart;
28 QTCOMMERCIALCHART_END_NAMESPACE
29
30 class QGraphicsScene;
31 class QResizeEvent;
32 class Callout;
33 class QMouseEvent;
34
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
37 class View: public QGraphicsView
38 {
39 Q_OBJECT
40
41 public:
42 View(QWidget *parent = 0);
43
44 protected:
45 void resizeEvent(QResizeEvent *event);
46 void mouseMoveEvent(QMouseEvent *event);
47
48 public slots:
49 void keepCallout();
50 void tooltip(QPointF point, bool state);
51
52 private:
53 QGraphicsSimpleTextItem *m_coordX;
54 QGraphicsSimpleTextItem *m_coordY;
55 QChart *m_chart;
56 Callout *m_tooltip;
57 };
58
59 #endif
@@ -2,7 +2,7
2 #include <QPainter>
2 #include <QPainter>
3 #include <QFontMetrics>
3 #include <QFontMetrics>
4 #include <QGraphicsSceneMouseEvent>
4 #include <QGraphicsSceneMouseEvent>
5 #include <QCursor>
5 #include <QMouseEvent>
6
6
7 Callout::Callout(QGraphicsItem * parent):
7 Callout::Callout(QGraphicsItem * parent):
8 QGraphicsItem(parent)
8 QGraphicsItem(parent)
@@ -5,9 +5,11
5 TARGET = callout
5 TARGET = callout
6 TEMPLATE = app
6 TEMPLATE = app
7
7
8 SOURCES += main.cpp\
8 SOURCES += \
9 widget.cpp \
9 main.cpp\
10 callout.cpp
10 callout.cpp \
11 view.cpp
11
12
12 HEADERS += widget.h \
13 HEADERS += \
13 callout.h
14 callout.h \
15 view.h
@@ -1,10 +1,10
1 #include <QApplication>
1 #include <QApplication>
2 #include "widget.h"
2 #include "view.h"
3
3
4 int main(int argc, char *argv[])
4 int main(int argc, char *argv[])
5 {
5 {
6 QApplication a(argc, argv);
6 QApplication a(argc, argv);
7 Widget w;
7 View w;
8 w.show();
8 w.show();
9
9
10 return a.exec();
10 return a.exec();
@@ -1,26 +1,47
1 #include "widget.h"
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "view.h"
22 #include <QResizeEvent>
2 #include <QGraphicsScene>
23 #include <QGraphicsScene>
3 #include <QGraphicsView>
4 #include <QVBoxLayout>
5 #include <QChart>
24 #include <QChart>
6 #include <QLineSeries>
25 #include <QLineSeries>
7 #include <QSplineSeries>
26 #include <QSplineSeries>
8 #include <QGraphicsTextItem>
27 #include <QGraphicsTextItem>
9 #include <QGraphicsLineItem>
10 #include "callout.h"
28 #include "callout.h"
29 #include <QMouseEvent>
11
30
12 QTCOMMERCIALCHART_USE_NAMESPACE
31 View::View(QWidget *parent)
13
32 : QGraphicsView(new QGraphicsScene, parent),
14 Widget::Widget(QWidget *parent)
33 m_coordX(0),
15 : QWidget(parent),
34 m_coordY(0),
16 m_scene(0),
17 m_chart(0),
35 m_chart(0),
18 m_view(0),
19 m_tooltip(0)
36 m_tooltip(0)
20 {
37 {
38 setDragMode(QGraphicsView::NoDrag);
39 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41
21 // chart
42 // chart
22 m_chart = new QChart;
43 m_chart = new QChart;
23 m_chart->setMinimumSize(640, 480);
44 m_chart->setMinimumSize(800, 600);
24 m_chart->setTitle("Hover the line to show callout. Click the line to make it stay");
45 m_chart->setTitle("Hover the line to show callout. Click the line to make it stay");
25 m_chart->legend()->hide();
46 m_chart->legend()->hide();
26 QLineSeries *series = new QLineSeries;
47 QLineSeries *series = new QLineSeries;
@@ -40,34 +61,47 Widget::Widget(QWidget *parent)
40 m_chart->addSeries(series2);
61 m_chart->addSeries(series2);
41
62
42 m_chart->createDefaultAxes();
63 m_chart->createDefaultAxes();
64 m_chart->setAcceptHoverEvents(true);
43
65
44 m_scene = new QGraphicsScene;
66 setRenderHint(QPainter::Antialiasing);
45 m_view = new QGraphicsView(m_scene);
67 scene()->addItem(m_chart);
46 m_view->setRenderHint(QPainter::Antialiasing);
47 m_scene->addItem(m_chart);
48
68
49 QVBoxLayout *mainLayout = new QVBoxLayout;
69 m_coordX = new QGraphicsSimpleTextItem(m_chart);
50 mainLayout->addWidget(m_view);
70 m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height());
51 setLayout(mainLayout);
71 m_coordX->setText("X: ");
72 m_coordY = new QGraphicsSimpleTextItem(m_chart);
73 m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height());
74 m_coordY->setText("Y: ");
52
75
53 connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
76 connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
54 connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
77 connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
55
78
56 connect(series2, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
79 connect(series2, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout()));
57 connect(series2, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
80 connect(series2, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool)));
81
82 this->setMouseTracking(true);
58 }
83 }
59
84
60 Widget::~Widget()
85 void View::resizeEvent(QResizeEvent *event)
61 {
86 {
87 if (scene())
88 scene()->setSceneRect(QRect(QPoint(0, 0), event->size()));
89 QGraphicsView::resizeEvent(event);
90 }
62
91
92 void View::mouseMoveEvent(QMouseEvent *event)
93 {
94 m_coordX->setText(QString("X: %1").arg(m_chart->mapToValue(event->pos()).x()));
95 m_coordY->setText(QString("Y: %1").arg(m_chart->mapToValue(event->pos()).y()));
96 QGraphicsView::mouseMoveEvent(event);
63 }
97 }
64
98
65 void Widget::keepCallout()
99 void View::keepCallout()
66 {
100 {
67 m_tooltip = new Callout(m_chart);
101 m_tooltip = new Callout(m_chart);
68 }
102 }
69
103
70 void Widget::tooltip(QPointF point, bool state)
104 void View::tooltip(QPointF point, bool state)
71 {
105 {
72 if (m_tooltip == 0)
106 if (m_tooltip == 0)
73 m_tooltip = new Callout(m_chart);
107 m_tooltip = new Callout(m_chart);
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now