diff --git a/src/chartlayout.cpp b/src/chartlayout.cpp index 9fbf279..233af1e 100644 --- a/src/chartlayout.cpp +++ b/src/chartlayout.cpp @@ -46,7 +46,8 @@ ChartLayout::~ChartLayout() void ChartLayout::setGeometry(const QRectF& rect) { - Q_ASSERT(rect.isValid()); + if (!rect.isValid()) + return; QList axes = m_presenter->axisItems(); ChartTitle* title = m_presenter->titleElement(); diff --git a/src/qchartview.cpp b/src/qchartview.cpp index 919c598..bf1e782 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -88,6 +88,18 @@ QChart* QChartView::chart() const } /*! + Sets the current chart to \a chart. Ownership of the new chart is passed to chartview + and ownership of the previous chart is released. + + To avoid memory leaks users needs to make sure the previous chart is deleted. +*/ + +void QChartView::setChart(QChart *chart) +{ + d_ptr->setChart(chart); +} + +/*! Sets the RubberBandPlicy to \a rubberBand. Selected policy determines the way zooming is performed. */ void QChartView::setRubberBand(const RubberBands& rubberBand) @@ -191,9 +203,7 @@ void QChartView::mouseReleaseEvent(QMouseEvent *event) void QChartView::resizeEvent(QResizeEvent *event) { QGraphicsView::resizeEvent(event); - d_ptr->m_chart->resize(size()); - setMinimumSize(d_ptr->m_chart->minimumSize().toSize()); - setSceneRect(d_ptr->m_chart->geometry()); + d_ptr->resize(); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -221,6 +231,29 @@ QChartViewPrivate::~QChartViewPrivate() } +void QChartViewPrivate::setChart(QChart *chart) +{ + Q_ASSERT(chart); + + if (m_chart == chart) + return; + + if (m_chart) + m_scene->removeItem(m_chart); + + m_chart = chart; + m_scene->addItem(m_chart); + + resize(); +} + +void QChartViewPrivate::resize() +{ + m_chart->resize(q_ptr->size()); + q_ptr->setMinimumSize(m_chart->minimumSize().toSize()); + q_ptr->setSceneRect(m_chart->geometry()); +} + #include "moc_qchartview.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartview.h b/src/qchartview.h index 1754f1a..ec124e0 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -54,7 +54,9 @@ public: void setRubberBand(const RubberBands& rubberBands); RubberBands rubberBand() const; + QChart* chart() const; + void setChart(QChart *chart); protected: void resizeEvent(QResizeEvent *event); diff --git a/src/qchartview_p.h b/src/qchartview_p.h index b23f2f5..c1aa807 100644 --- a/src/qchartview_p.h +++ b/src/qchartview_p.h @@ -45,6 +45,8 @@ class QChartViewPrivate { public: explicit QChartViewPrivate(QChartView *q, QChart *chart = 0); ~QChartViewPrivate(); + void setChart(QChart *chart); + void resize(); protected: QChartView *q_ptr; diff --git a/tests/auto/qchartview/tst_qchartview.cpp b/tests/auto/qchartview/tst_qchartview.cpp index 7e51af1..7a2541e 100644 --- a/tests/auto/qchartview/tst_qchartview.cpp +++ b/tests/auto/qchartview/tst_qchartview.cpp @@ -49,6 +49,7 @@ private Q_SLOTS: void chart(); void rubberBand_data(); void rubberBand(); + void setChart(); private: QChartView* m_view; @@ -183,6 +184,40 @@ void tst_QChartView::rubberBand() } +void tst_QChartView::setChart() +{ + QPointer oldChart = m_view->chart(); + QLineSeries *series1 = new QLineSeries(); + series1->append(0,0); + series1->append(1,1); + oldChart->addSeries(series1); + + QPointer newChart = new QChart(); + QLineSeries *series2 = new QLineSeries(); + series2->append(0,1); + series2->append(1,0); + newChart->addSeries(series2); + + // show current chart + m_view->show(); + QTest::qWaitForWindowShown(m_view); + QTest::qWait(1000); + + // set new chart + m_view->setChart(newChart); + QVERIFY(m_view->chart() == newChart); + QVERIFY(!oldChart.isNull()); // ownership of the oldChart is released and not deleted + QTest::qWait(1000); + + // delete the view + delete m_view; + m_view = 0; + QVERIFY(newChart.isNull()); // view owns and deletes it + QVERIFY(!oldChart.isNull()); // not owned by view + + delete oldChart; +} + QTEST_MAIN(tst_QChartView) #include "tst_qchartview.moc"