summaryrefslogtreecommitdiffstats
path: root/examples/charts
diff options
context:
space:
mode:
authorLiang Qi <liang.qi@qt.io>2016-08-23 08:23:42 +0200
committerLiang Qi <liang.qi@qt.io>2016-08-23 09:13:20 +0200
commitd352d707f95c3c4e213585361a3ec737389e301e (patch)
tree492cd77328b19731cecbd5470d9c667d78d75e79 /examples/charts
parenta7b80c0b6176692ee2b7abdce7acdc5c37a72d00 (diff)
parent7a0be48ac39243d139cc4a78423ca033d5d90b3a (diff)
Merge remote-tracking branch 'origin/5.6' into 5.7
Conflicts: .qmake.conf README src/charts/qchartglobal.h tests/auto/chartdataset/tst_chartdataset.cpp tests/auto/domain/tst_domain.cpp Change-Id: Ib4e01f2646d87b691c7b2f8bee4ed1f5521e4f6d
Diffstat (limited to 'examples/charts')
-rw-r--r--examples/charts/callout/callout.cpp18
-rw-r--r--examples/charts/callout/callout.h11
-rw-r--r--examples/charts/callout/view.cpp8
-rw-r--r--examples/charts/callout/view.h1
4 files changed, 29 insertions, 9 deletions
diff --git a/examples/charts/callout/callout.cpp b/examples/charts/callout/callout.cpp
index 2c843393..bb37802a 100644
--- a/examples/charts/callout/callout.cpp
+++ b/examples/charts/callout/callout.cpp
@@ -32,15 +32,17 @@
#include <QtGui/QFontMetrics>
#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtGui/QMouseEvent>
+#include <QtCharts/QChart>
-Callout::Callout(QGraphicsItem * parent):
- QGraphicsItem(parent)
+Callout::Callout(QChart *chart):
+ QGraphicsItem(chart),
+ m_chart(chart)
{
}
QRectF Callout::boundingRect() const
{
- QPointF anchor = mapFromParent(m_anchor);
+ QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));
QRectF rect;
rect.setLeft(qMin(m_rect.left(), anchor.x()));
rect.setRight(qMax(m_rect.right(), anchor.x()));
@@ -56,7 +58,7 @@ void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
QPainterPath path;
path.addRoundedRect(m_rect, 5, 5);
- QPointF anchor = mapFromParent(m_anchor);
+ QPointF anchor = mapFromParent(m_chart->mapToPosition(m_anchor));
if (!m_rect.contains(anchor)) {
QPointF point1, point2;
@@ -88,7 +90,7 @@ void Callout::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q
point2.setY(y2);
path.moveTo(point1);
- path.lineTo(mapFromParent(m_anchor));
+ path.lineTo(anchor);
path.lineTo(point2);
path = path.simplified();
}
@@ -126,3 +128,9 @@ void Callout::setAnchor(QPointF point)
{
m_anchor = point;
}
+
+void Callout::updateGeometry()
+{
+ prepareGeometryChange();
+ setPos(m_chart->mapToPosition(m_anchor) + QPoint(10, -50));
+}
diff --git a/examples/charts/callout/callout.h b/examples/charts/callout/callout.h
index 35de08d5..67cc76ff 100644
--- a/examples/charts/callout/callout.h
+++ b/examples/charts/callout/callout.h
@@ -30,6 +30,7 @@
#ifndef CALLOUT_H
#define CALLOUT_H
+#include <QtCharts/QChartGlobal>
#include <QtWidgets/QGraphicsItem>
#include <QtGui/QFont>
@@ -37,13 +38,20 @@ QT_BEGIN_NAMESPACE
class QGraphicsSceneMouseEvent;
QT_END_NAMESPACE
+QT_CHARTS_BEGIN_NAMESPACE
+class QChart;
+QT_CHARTS_END_NAMESPACE
+
+QT_CHARTS_USE_NAMESPACE
+
class Callout : public QGraphicsItem
{
public:
- Callout(QGraphicsItem * parent = 0);
+ Callout(QChart *parent);
void setText(const QString &text);
void setAnchor(QPointF point);
+ void updateGeometry();
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
@@ -58,6 +66,7 @@ private:
QRectF m_rect;
QPointF m_anchor;
QFont m_font;
+ QChart *m_chart;
};
#endif // CALLOUT_H
diff --git a/examples/charts/callout/view.cpp b/examples/charts/callout/view.cpp
index 1c90eef0..9c1eca69 100644
--- a/examples/charts/callout/view.cpp
+++ b/examples/charts/callout/view.cpp
@@ -98,6 +98,8 @@ void View::resizeEvent(QResizeEvent *event)
m_chart->resize(event->size());
m_coordX->setPos(m_chart->size().width()/2 - 50, m_chart->size().height() - 20);
m_coordY->setPos(m_chart->size().width()/2 + 50, m_chart->size().height() - 20);
+ foreach (Callout *callout, m_callouts)
+ callout->updateGeometry();
}
QGraphicsView::resizeEvent(event);
}
@@ -111,6 +113,7 @@ void View::mouseMoveEvent(QMouseEvent *event)
void View::keepCallout()
{
+ m_callouts.append(m_tooltip);
m_tooltip = new Callout(m_chart);
}
@@ -121,10 +124,9 @@ void View::tooltip(QPointF point, bool state)
if (state) {
m_tooltip->setText(QString("X: %1 \nY: %2 ").arg(point.x()).arg(point.y()));
- QXYSeries *series = qobject_cast<QXYSeries *>(sender());
- m_tooltip->setAnchor(m_chart->mapToPosition(point, series));
- m_tooltip->setPos(m_chart->mapToPosition(point, series) + QPoint(10, -50));
+ m_tooltip->setAnchor(point);
m_tooltip->setZValue(11);
+ m_tooltip->updateGeometry();
m_tooltip->show();
} else {
m_tooltip->hide();
diff --git a/examples/charts/callout/view.h b/examples/charts/callout/view.h
index 4744a1c3..4d17461b 100644
--- a/examples/charts/callout/view.h
+++ b/examples/charts/callout/view.h
@@ -66,6 +66,7 @@ private:
QGraphicsSimpleTextItem *m_coordY;
QChart *m_chart;
Callout *m_tooltip;
+ QList<Callout *> m_callouts;
};
#endif