diff options
author | Miikka Heikkinen <miikka.heikkinen@qt.io> | 2016-08-15 16:30:28 +0300 |
---|---|---|
committer | Miikka Heikkinen <miikka.heikkinen@qt.io> | 2016-08-16 06:47:44 +0000 |
commit | c6d72bb6ebd860a38aac2e557bca330b0d5b1926 (patch) | |
tree | 2f4da0685db88710361e250214f928c39c029bcf /examples/charts | |
parent | cccd1b688333dcc5616e1fa48050c60a1a534421 (diff) |
Fix resize handling in Callout example
Callouts are now positioned correctly when the chart is resized.
Task-number: QTBUG-54492
Change-Id: I5660eb48e58348cc2d649b48965c342488294ae4
Reviewed-by: Mika Salmela <mika.salmela@qt.io>
Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io>
Diffstat (limited to 'examples/charts')
-rw-r--r-- | examples/charts/callout/callout.cpp | 18 | ||||
-rw-r--r-- | examples/charts/callout/callout.h | 11 | ||||
-rw-r--r-- | examples/charts/callout/view.cpp | 8 | ||||
-rw-r--r-- | examples/charts/callout/view.h | 1 |
4 files changed, 29 insertions, 9 deletions
diff --git a/examples/charts/callout/callout.cpp b/examples/charts/callout/callout.cpp index f20d0609..9699df61 100644 --- a/examples/charts/callout/callout.cpp +++ b/examples/charts/callout/callout.cpp @@ -24,15 +24,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())); @@ -48,7 +50,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; @@ -80,7 +82,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(); } @@ -118,3 +120,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 149c95e5..11d6ae59 100644 --- a/examples/charts/callout/callout.h +++ b/examples/charts/callout/callout.h @@ -22,6 +22,7 @@ #ifndef CALLOUT_H #define CALLOUT_H +#include <QtCharts/QChartGlobal> #include <QtWidgets/QGraphicsItem> #include <QtGui/QFont> @@ -29,13 +30,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); @@ -50,6 +58,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 b413989e..4d1adb11 100644 --- a/examples/charts/callout/view.cpp +++ b/examples/charts/callout/view.cpp @@ -90,6 +90,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); } @@ -103,6 +105,7 @@ void View::mouseMoveEvent(QMouseEvent *event) void View::keepCallout() { + m_callouts.append(m_tooltip); m_tooltip = new Callout(m_chart); } @@ -113,10 +116,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 e72df120..b7085b21 100644 --- a/examples/charts/callout/view.h +++ b/examples/charts/callout/view.h @@ -58,6 +58,7 @@ private: QGraphicsSimpleTextItem *m_coordY; QChart *m_chart; Callout *m_tooltip; + QList<Callout *> m_callouts; }; #endif |