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. Deleting QChart Causes 30+ Second Application Hang!
Forum Updated to NodeBB v4.3 + New Features

Deleting QChart Causes 30+ Second Application Hang!

Scheduled Pinned Locked Moved Unsolved General and Desktop
51 Posts 5 Posters 3.9k Views 2 Watching
  • 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.
  • JoeCFDJ JoeCFD

    @FleetingMemory 10000 series + 10 points

    Jon's code and chartView is not deleted. Series are cleared quickly.

    #include "chart.h"
    
    // using namespace QtCharts;
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setGeometry(100, 100, 800, 600);
        setLayout(new QVBoxLayout);
    
        this->chart = new QChart();
        this->chartView = new QChartView(chart, this);
        layout()->addWidget(chartView);
        chartView->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
    
        for (int i = 0; i < 10000; i++)
        {
            QLineSeries* series = new QLineSeries();
            for (int x = 0; x < 10; x++)
                series->append(x, x * (i + 1));
            chart->addSeries(series);
        }
        
        chart->createDefaultAxes();
        chart->axes(Qt::Horizontal).first()->setTitleText("X Axis");
        chart->axes(Qt::Vertical).first()->setTitleText("Y Axis");
        QTimer::singleShot(5000, this, &Widget::deleteChart);
    }
    
    void Widget::deleteChart()
    {
        QElapsedTimer elapsed;
        qDebug() << "Starting to delete";
        elapsed.start(); 
    
        chartView->setChart( new QChart() );
        chart->deleteLater();
    
        layout()->removeWidget(chartView);
        //chart->removeAllSeries();
        //delete chart;
        //chart = nullptr;
        //delete chartView;
        //chartView = nullptr;
        qDebug() << "Finished deleting" << elapsed.elapsed();
    }
    
    Widget::~Widget() {}
    
    F Offline
    F Offline
    FleetingMemory
    wrote on last edited by
    #38

    @JoeCFD Going back to my original code, I tried moving setchart(new QChart()) and deletelater() to before I populate the new chart with new series. Previously I was populating a new chart then setting it, then deleting a pointer to the old chart. Now, if I watch the memory, deleteLater still doesn't actually call delete until after the new chart is populated fully. So it looks like it deleted quickly but it still hangs after the new chart pops in with data. Doing it this way, however does take slightly less time to delete.

    JoeCFDJ 1 Reply Last reply
    0
    • F FleetingMemory

      @JoeCFD Going back to my original code, I tried moving setchart(new QChart()) and deletelater() to before I populate the new chart with new series. Previously I was populating a new chart then setting it, then deleting a pointer to the old chart. Now, if I watch the memory, deleteLater still doesn't actually call delete until after the new chart is populated fully. So it looks like it deleted quickly but it still hangs after the new chart pops in with data. Doing it this way, however does take slightly less time to delete.

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #39

      @FleetingMemory Maybe try to delete it in a thread. Also a splash widget may be needed at exit.

      F 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        @FleetingMemory Maybe try to delete it in a thread. Also a splash widget may be needed at exit.

        F Offline
        F Offline
        FleetingMemory
        wrote on last edited by FleetingMemory
        #40

        @JoeCFD Can't delete in a thread, it calls on GUI functions because it loops across removeSeries(), which calls update. This causes a cross-thread exception in Qt. DeleteLater() doesn't actually delete immediately within your timer bounds in the code you posted above (that is just timing QT scheduling it), so it still freezes the program later on.

        1 Reply Last reply
        0
        • JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #41

          @FleetingMemory
          As you have now discovered, it is the deleting of each series which is so time consuming (for unknown reasons). There is a huge difference between 10 series with 10,000 points each versus 10,000 series with 10 points each. (In fact, in latter case timing is same even if all series are empty/have no points.) There seems to be little you can do to affect that timing. I see no evidence it has anything to do with whether the view/chart/series are shown or not, have signals disabled or not, etc.

          Which takes me back to my question as to why you need 10,000 series or any such figure, which you did not answer. There is no chance the user can view or appreciate such a large number, so what is the point of adding that many onto the chart in the first place? That is where I would look for efficiency improvement.

          If you really are not prepared to reduce that, then: I presume any deleteLater() won't help as that just delays when the delay comes to a bit later. I assume you are right that series cannot or should not be deleted from a thread as they are some kind of UI object. Which would leave you with one possible "hack" to distribute the deletion time "nicely". Follow @JoeCFD's code to assign a new, blank chart to the view (chartView->setChart( new QChart() );). But do not immediately delete the original chart there: instead, keep it around in a suitable variable. In UI code thread set off a repeating timer every so often: in each timeout use removeSeries() to delete a few of the 10,000 series, example perhaps 100 at a time. You can do this because you are in the main/UI thread. Over time you will have deleted all the series from the old QChart object, at that point you can delete the (empty) chart and terminate the timer. Seems to me reducing the number of series in the first place is preferable, but there you are if you really want to do this.

          F 1 Reply Last reply
          1
          • JonBJ JonB

            @FleetingMemory
            As you have now discovered, it is the deleting of each series which is so time consuming (for unknown reasons). There is a huge difference between 10 series with 10,000 points each versus 10,000 series with 10 points each. (In fact, in latter case timing is same even if all series are empty/have no points.) There seems to be little you can do to affect that timing. I see no evidence it has anything to do with whether the view/chart/series are shown or not, have signals disabled or not, etc.

            Which takes me back to my question as to why you need 10,000 series or any such figure, which you did not answer. There is no chance the user can view or appreciate such a large number, so what is the point of adding that many onto the chart in the first place? That is where I would look for efficiency improvement.

            If you really are not prepared to reduce that, then: I presume any deleteLater() won't help as that just delays when the delay comes to a bit later. I assume you are right that series cannot or should not be deleted from a thread as they are some kind of UI object. Which would leave you with one possible "hack" to distribute the deletion time "nicely". Follow @JoeCFD's code to assign a new, blank chart to the view (chartView->setChart( new QChart() );). But do not immediately delete the original chart there: instead, keep it around in a suitable variable. In UI code thread set off a repeating timer every so often: in each timeout use removeSeries() to delete a few of the 10,000 series, example perhaps 100 at a time. You can do this because you are in the main/UI thread. Over time you will have deleted all the series from the old QChart object, at that point you can delete the (empty) chart and terminate the timer. Seems to me reducing the number of series in the first place is preferable, but there you are if you really want to do this.

            F Offline
            F Offline
            FleetingMemory
            wrote on last edited by
            #42

            @JonB said in Deleting QChart Causes 30+ Second Application Hang!:

            Which takes me back to my question as to why you need 10,000 series or any such figure, which you did not answer. There is no chance the user can view or appreciate such a large number, so what is the point of adding that many onto the chart in the first place? That is where I would look for efficiency improvement.

            The chart is to be used for displaying & isolating specific data/time frames, thus all data/lines need to be present, and each series is a line being displayed. By manipulating the X-Axis the user can zoom into/out of to a specific time frame. The idea of deleting a few at a time is something I have definitely been exploring as a possible solution however it doesn't fix the fact it will still hang the whole application when the window is closed or application exited as well.

            JoeCFDJ 1 Reply Last reply
            0
            • F FleetingMemory

              @JonB said in Deleting QChart Causes 30+ Second Application Hang!:

              Which takes me back to my question as to why you need 10,000 series or any such figure, which you did not answer. There is no chance the user can view or appreciate such a large number, so what is the point of adding that many onto the chart in the first place? That is where I would look for efficiency improvement.

              The chart is to be used for displaying & isolating specific data/time frames, thus all data/lines need to be present, and each series is a line being displayed. By manipulating the X-Axis the user can zoom into/out of to a specific time frame. The idea of deleting a few at a time is something I have definitely been exploring as a possible solution however it doesn't fix the fact it will still hang the whole application when the window is closed or application exited as well.

              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #43

              @FleetingMemory that is why I wrote earlier that you need a splash widget with some nice animation at exit and make your app wait for these series to be cleared.

              You can also file a bug with Jon's test code to Qt for optimization in removeAllSeries call. I guess one update may be enough instead of one update after one deletion.

              F 2 Replies Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #44

                I've tested @JonB 's code with 12000 series / 40 data points each in debug mode:

                Starting to create
                Finished creating 32858
                Starting to delete
                Finished deleting 195421

                Digged a little bit around to see what happens during destruction. The most time the legend gets updated... so:
                I added this before delete chart;:
                delete chart->legend();

                -->
                Starting to create
                Finished creating 33861
                Starting to delete
                Finished deleting 27167

                The best thing I found is to add d_ptr->m_dataset->disconnect(); in QChart dtor. I doubt this will go in into official QtCharts since it's in maintainance mode only.

                -->
                Starting to create
                Finished creating 31901
                Starting to delete
                Finished deleting 16988

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                JonBJ F 2 Replies Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  I've tested @JonB 's code with 12000 series / 40 data points each in debug mode:

                  Starting to create
                  Finished creating 32858
                  Starting to delete
                  Finished deleting 195421

                  Digged a little bit around to see what happens during destruction. The most time the legend gets updated... so:
                  I added this before delete chart;:
                  delete chart->legend();

                  -->
                  Starting to create
                  Finished creating 33861
                  Starting to delete
                  Finished deleting 27167

                  The best thing I found is to add d_ptr->m_dataset->disconnect(); in QChart dtor. I doubt this will go in into official QtCharts since it's in maintainance mode only.

                  -->
                  Starting to create
                  Finished creating 31901
                  Starting to delete
                  Finished deleting 16988

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #45

                  @Christian-Ehrlicher
                  All I saw was all the time being spent in removeAllSeries(), or whichever you remove series if one by one. I looked at remoceSeries() source code online but I don't have sources to dig further. I don't know how that is affected by legend. Nothing struck me except however it is storing its model data. That could relate to your d_ptr->m_dataset. And d_ptr requires OP to change and compile Qt sources, right?

                  It's taking 30 seconds to get rid of 10,000 series which it never took 30 seconds to create, it is a lot of time per series.

                  P.S.
                  Of course, as you say QtCharts is going so realise nothing is going to be done about sources.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    I've tested @JonB 's code with 12000 series / 40 data points each in debug mode:

                    Starting to create
                    Finished creating 32858
                    Starting to delete
                    Finished deleting 195421

                    Digged a little bit around to see what happens during destruction. The most time the legend gets updated... so:
                    I added this before delete chart;:
                    delete chart->legend();

                    -->
                    Starting to create
                    Finished creating 33861
                    Starting to delete
                    Finished deleting 27167

                    The best thing I found is to add d_ptr->m_dataset->disconnect(); in QChart dtor. I doubt this will go in into official QtCharts since it's in maintainance mode only.

                    -->
                    Starting to create
                    Finished creating 31901
                    Starting to delete
                    Finished deleting 16988

                    F Offline
                    F Offline
                    FleetingMemory
                    wrote on last edited by
                    #46

                    @Christian-Ehrlicher I currently have legend disabled, is it still updating in the background even with it off?

                    1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      @FleetingMemory that is why I wrote earlier that you need a splash widget with some nice animation at exit and make your app wait for these series to be cleared.

                      You can also file a bug with Jon's test code to Qt for optimization in removeAllSeries call. I guess one update may be enough instead of one update after one deletion.

                      F Offline
                      F Offline
                      FleetingMemory
                      wrote on last edited by FleetingMemory
                      #47

                      original bug report was ignored, had to refile and explain more. Linked the new report below

                      1 Reply Last reply
                      0
                      • JoeCFDJ JoeCFD

                        @FleetingMemory that is why I wrote earlier that you need a splash widget with some nice animation at exit and make your app wait for these series to be cleared.

                        You can also file a bug with Jon's test code to Qt for optimization in removeAllSeries call. I guess one update may be enough instead of one update after one deletion.

                        F Offline
                        F Offline
                        FleetingMemory
                        wrote on last edited by
                        #48

                        @JoeCFD Filed the bug report yesterday -- Feel free to add any findings to it!
                        https://bugreports.qt.io/browse/QTBUG-134490

                        JoeCFDJ 1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #49

                          As I already said QtCharts is deprecated. Nothing will be changed there. I gave you two solutions...

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          2
                          • F FleetingMemory

                            @JoeCFD Filed the bug report yesterday -- Feel free to add any findings to it!
                            https://bugreports.qt.io/browse/QTBUG-134490

                            JoeCFDJ Offline
                            JoeCFDJ Offline
                            JoeCFD
                            wrote on last edited by
                            #50

                            @FleetingMemory They will not fix it for you. Maybe you can dig into the source code of this module and try some fixes. Then build it for yourself.

                            1 Reply Last reply
                            1
                            • F Offline
                              F Offline
                              FleetingMemory
                              wrote on last edited by
                              #51

                              @Christian-Ehrlicher @JoeCFD Thank you for helping find the fix will try what was submitted to the bug report.

                              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