Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Crash when dynamically add line series to QChart after setMarkerShape(QLegend::MarkerShapeFromSeries)
QtWS25 Last Chance

Crash when dynamically add line series to QChart after setMarkerShape(QLegend::MarkerShapeFromSeries)

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • divergerD Offline
    divergerD Offline
    diverger
    wrote on last edited by diverger
    #1

    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.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      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();
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      divergerD 1 Reply Last reply
      0
      • VRoninV VRonin

        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();
        }
        
        divergerD Offline
        divergerD Offline
        diverger
        wrote on last edited by diverger
        #3

        @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.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Ok, then it's probably a bug. Report it on https://bugreports.qt.io including the below example, the OS, compiler and Qt version

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved