@@ -14,13 +14,15 Widget::Widget(QWidget *parent) | |||
|
14 | 14 | : QWidget(parent), |
|
15 | 15 | m_scene(0), |
|
16 | 16 | m_chart(0), |
|
17 | m_view(0) | |
|
17 | m_view(0), | |
|
18 | m_tooltip(0) | |
|
18 | 19 | { |
|
19 | 20 | // chart |
|
20 | 21 | m_chart = new QChart; |
|
21 | 22 | m_chart->setMinimumSize(640, 480); |
|
23 | m_chart->setTitle("Hover the line to show callout. Click the line to make it stay"); | |
|
24 | m_chart->legend()->hide(); | |
|
22 | 25 | QLineSeries *series = new QLineSeries; |
|
23 | series->setName("Click the line to create a movable callout"); | |
|
24 | 26 | series->append(1, 3); |
|
25 | 27 | series->append(4, 5); |
|
26 | 28 | series->append(5, 4.5); |
@@ -38,7 +40,8 Widget::Widget(QWidget *parent) | |||
|
38 | 40 | mainLayout->addWidget(m_view); |
|
39 | 41 | setLayout(mainLayout); |
|
40 | 42 | |
|
41 |
connect(series, SIGNAL(clicked(QPointF)), this, SLOT( |
|
|
43 | connect(series, SIGNAL(clicked(QPointF)), this, SLOT(keepCallout())); | |
|
44 | connect(series, SIGNAL(hovered(QPointF, bool)), this, SLOT(tooltip(QPointF,bool))); | |
|
42 | 45 | } |
|
43 | 46 | |
|
44 | 47 | Widget::~Widget() |
@@ -46,11 +49,23 Widget::~Widget() | |||
|
46 | 49 | |
|
47 | 50 | } |
|
48 | 51 | |
|
49 |
void Widget:: |
|
|
52 | void Widget::keepCallout() | |
|
50 | 53 | { |
|
51 |
|
|
|
52 | label->setText(QString("X: %1\nY: %2").arg(point.x()).arg(point.y())); | |
|
53 | label->setAnchor(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos())))); | |
|
54 | label->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50))))); | |
|
55 | label->setZValue(11); | |
|
54 | m_tooltip = new Callout(m_chart); | |
|
55 | } | |
|
56 | ||
|
57 | void Widget::tooltip(QPointF point, bool state) | |
|
58 | { | |
|
59 | if (m_tooltip == 0) | |
|
60 | m_tooltip = new Callout(m_chart); | |
|
61 | ||
|
62 | if (state) { | |
|
63 | m_tooltip->setText(QString("X: %1\nY: %2").arg(point.x()).arg(point.y())); | |
|
64 | m_tooltip->setAnchor(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos())))); | |
|
65 | m_tooltip->setPos(m_chart->mapFromParent(m_view->mapToScene(m_view->mapFromGlobal(QCursor::pos() + QPoint(10, -50))))); | |
|
66 | m_tooltip->setZValue(11); | |
|
67 | m_tooltip->show(); | |
|
68 | } else { | |
|
69 | m_tooltip->hide(); | |
|
70 | } | |
|
56 | 71 | } |
@@ -6,6 +6,7 | |||
|
6 | 6 | |
|
7 | 7 | class QGraphicsScene; |
|
8 | 8 | class QGraphicsView; |
|
9 | class Callout; | |
|
9 | 10 | |
|
10 | 11 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | 12 | |
@@ -18,12 +19,14 public: | |||
|
18 | 19 | ~Widget(); |
|
19 | 20 | |
|
20 | 21 | public slots: |
|
21 |
void |
|
|
22 | void keepCallout(); | |
|
23 | void tooltip(QPointF point, bool state); | |
|
22 | 24 | |
|
23 | 25 | private: |
|
24 | 26 | QGraphicsScene *m_scene; |
|
25 | 27 | QChart *m_chart; |
|
26 | 28 | QGraphicsView *m_view; |
|
29 | Callout *m_tooltip; | |
|
27 | 30 | }; |
|
28 | 31 | |
|
29 | 32 | #endif // WIDGET_H |
@@ -35,6 +35,7 LineChartItem::LineChartItem(QLineSeries *series, ChartPresenter *presenter) | |||
|
35 | 35 | m_series(series), |
|
36 | 36 | m_pointsVisible(false) |
|
37 | 37 | { |
|
38 | setAcceptHoverEvents(true); | |
|
38 | 39 | setZValue(ChartPresenter::LineChartZValue); |
|
39 | 40 | QObject::connect(series->d_func(), SIGNAL(updated()), this, SLOT(handleUpdated())); |
|
40 | 41 | QObject::connect(series, SIGNAL(visibleChanged()), this, SLOT(handleUpdated())); |
@@ -132,6 +133,18 void LineChartItem::mousePressEvent(QGraphicsSceneMouseEvent *event) | |||
|
132 | 133 | QGraphicsItem::mousePressEvent(event); |
|
133 | 134 | } |
|
134 | 135 | |
|
136 | void LineChartItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) | |
|
137 | { | |
|
138 | emit XYChart::hovered(calculateDomainPoint(event->pos()), true); | |
|
139 | QGraphicsItem::hoverEnterEvent(event); | |
|
140 | } | |
|
141 | ||
|
142 | void LineChartItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) | |
|
143 | { | |
|
144 | emit XYChart::hovered(calculateDomainPoint(event->pos()), false); | |
|
145 | QGraphicsItem::hoverEnterEvent(event); | |
|
146 | } | |
|
147 | ||
|
135 | 148 | #include "moc_linechartitem_p.cpp" |
|
136 | 149 | |
|
137 | 150 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -60,6 +60,8 public Q_SLOTS: | |||
|
60 | 60 | protected: |
|
61 | 61 | void updateGeometry(); |
|
62 | 62 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
63 | void hoverEnterEvent(QGraphicsSceneHoverEvent *event); | |
|
64 | void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); | |
|
63 | 65 | |
|
64 | 66 | private: |
|
65 | 67 | QLineSeries *m_series; |
@@ -76,6 +76,7 public: | |||
|
76 | 76 | |
|
77 | 77 | Q_SIGNALS: |
|
78 | 78 | void clicked(const QPointF &point); |
|
79 | void hovered(const QPointF &point, bool state); | |
|
79 | 80 | void pointReplaced(int index); |
|
80 | 81 | void pointRemoved(int index); |
|
81 | 82 | void pointAdded(int index); |
@@ -47,6 +47,7 XYChart::XYChart(QXYSeries *series, ChartPresenter *presenter) | |||
|
47 | 47 | QObject::connect(series, SIGNAL(pointAdded(int)), this, SLOT(handlePointAdded(int))); |
|
48 | 48 | QObject::connect(series, SIGNAL(pointRemoved(int)), this, SLOT(handlePointRemoved(int))); |
|
49 | 49 | QObject::connect(this, SIGNAL(clicked(QPointF)), series, SIGNAL(clicked(QPointF))); |
|
50 | QObject::connect(this, SIGNAL(hovered(QPointF,bool)), series, SIGNAL(hovered(QPointF,bool))); | |
|
50 | 51 | } |
|
51 | 52 | |
|
52 | 53 | void XYChart::setGeometryPoints(const QVector<QPointF>& points) |
General Comments 0
You need to be logged in to leave comments.
Login now