Crash when dynamically add line series to QChart after setMarkerShape(QLegend::MarkerShapeFromSeries)
-
I am using QCharts to show several XY line series. Among the series, there are several fixed, and others need to be added later, and dynamically. So the code runs as below:
chartViewWidget->chart()->addSeries(s1); chartViewWidget->chart()->addSeries(s2); chartViewWidget->chart()->createDefaultAxes(); chartViewWidget->chart()->legend()->setAlignment(Qt::AlignRight); chartViewWidget->chart()->legend()->setMarkerShape(QLegend::MarkerShapeFromSeries); // some other code // ........ chartViewWidget->chart()->addSeries(s3);
When adding s3, the program crashed. When I comment out the line contains "setMarkerShape(QLegend::MarkerShapeFromSeries)", all works well.
Because s1 and s2 is fixed, I don't want to remove all series, then re-add s1 and s2 every time when I adding s3. So, any suggestions?
Thanks.
BTW:
My tool chain is: Win7 64bit + Qt 5.10.1_32bit + Qt VS Tools + VS2015.
-
I can't try myself at the moment.
Can you reproduce even with this minimal example?#include <QApplication> #include <QChart> #include <QChartView> #include <QXYSeries> #include <QPointF> #include <QLegend> int main(int argc, char *argv[]) { QApplication a(argc, argv); QChartView mainView; QChart* mainChart = new QChart(&mainView); mainView->setChart(mainChart); QXYSeries* s1 = new QXYSeries({QPointF(0.0,0.0),QPointF(1.0,1.0),QPointF(2.0,2.0)}); QXYSeries* s2 = new QXYSeries({QPointF(0.0,2.0),QPointF(1.0,1.0),QPointF(2.0,0.0)}); QXYSeries* s3 = new QXYSeries({QPointF(0.0,0.5),QPointF(1.0,1.5),QPointF(2.0,0.5)}); mainChart->addSeries(s1); mainChart->addSeries(s2); mainChart->createDefaultAxes(); mainChart->legend()->setAlignment(Qt::AlignRight); mainChart->setMarkerShape(QLegend::MarkerShapeFromSeries); mainChart->addSeries(s3); mainView.show(); return a.exec(); }
-
I can't try myself at the moment.
Can you reproduce even with this minimal example?#include <QApplication> #include <QChart> #include <QChartView> #include <QXYSeries> #include <QPointF> #include <QLegend> int main(int argc, char *argv[]) { QApplication a(argc, argv); QChartView mainView; QChart* mainChart = new QChart(&mainView); mainView->setChart(mainChart); QXYSeries* s1 = new QXYSeries({QPointF(0.0,0.0),QPointF(1.0,1.0),QPointF(2.0,2.0)}); QXYSeries* s2 = new QXYSeries({QPointF(0.0,2.0),QPointF(1.0,1.0),QPointF(2.0,0.0)}); QXYSeries* s3 = new QXYSeries({QPointF(0.0,0.5),QPointF(1.0,1.5),QPointF(2.0,0.5)}); mainChart->addSeries(s1); mainChart->addSeries(s2); mainChart->createDefaultAxes(); mainChart->legend()->setAlignment(Qt::AlignRight); mainChart->setMarkerShape(QLegend::MarkerShapeFromSeries); mainChart->addSeries(s3); mainView.show(); return a.exec(); }
@VRonin I did some minor reviews to your code, and made it compiled, the changed code is as below:
#include <QApplication> #include <QtCharts/QChartView> #include <QtCharts/QChart> #include <QtCharts/QLineSeries> #include <QtCharts/QValueAxis> #include <QtCharts/QLegendMarker> #include <QtCharts/QXYSeries> #include <QPointF> #include <QtCharts/QLegend> QT_CHARTS_USE_NAMESPACE int main(int argc, char *argv[]) { QApplication a(argc, argv); QChartView *mainView = new QChartView(); QChart* mainChart = new QChart(); mainView->setChart(mainChart); QLineSeries* s1 = new QLineSeries(); s1->append({ QPointF(0.0,0.0),QPointF(1.0,1.0),QPointF(2.0,2.0) }); QLineSeries* s2 = new QLineSeries(); s2->append({ QPointF(0.0,2.0),QPointF(1.0,1.0),QPointF(2.0,0.0) }); QLineSeries* s3 = new QLineSeries(); s3->append({ QPointF(0.0,0.5),QPointF(1.0,1.5),QPointF(2.0,0.5) }); mainChart->addSeries(s1); mainChart->addSeries(s2); mainChart->createDefaultAxes(); mainChart->legend()->setAlignment(Qt::AlignRight); //mainChart->legend()->setMarkerShape(QLegend::MarkerShapeFromSeries); mainChart->addSeries(s3); mainView->show(); return a.exec(); }
I must comment out that line too, to make the code run well, or it will crash.
-
Ok, then it's probably a bug. Report it on https://bugreports.qt.io including the below example, the OS, compiler and Qt version