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. Plotting chart using lots of data
QtWS25 Last Chance

Plotting chart using lots of data

Scheduled Pinned Locked Moved Solved General and Desktop
threadchartchartview
8 Posts 4 Posters 1.6k 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.
  • S Offline
    S Offline
    StudyQt1
    wrote on last edited by StudyQt1
    #1

    Hi, I am plotting chart using qchart and qlineseries requiring lots of values, and the processing of values takes long time which blocks the GUI. To optimize this issue, I am trying to move the processing procedure into another thread. And I would like to ask 2 questions:

    1. I am now appending values into qlineseries like:

        for(int i = 0;  i < dataCount; i++)
        {
              series->append(val[i]);
        }
      

    I realized it's very time consuming, since I am appending one by one, I noticed there's QVXYModelMapper which can append the whole data model once, may I ask is it faster to use QVXYModelMapper ? I think maybe I can also try append(const QList<QPointF> &points); which loads all values just once.

    1. I am facing the thread affinity problem.

      First I created QChartView in GUI(main) thread:
      QChartView *chartView = new QChartView;

      Then I construct the worker class for appending data to line series, I move worker to another thread, loading data into qlineseries in worker::loadData

      First I do:
      Worker *worker = new worker(chartView);
      worker ->moveToThread(thread);

      Then in loadData:

      void Worker::loadData()
      {
              QLineSeries *series = new QLineSeries;
              // loading data into series, time-consuming
             //.....
             
             chartView->append(series);
            // chartView is constructed in main thread
           // Error: QObject::setParent: Cannot set parent, new parent is in a different thread
      }
      

    looks like I cannot change chartView which is constructed in main thread, is there a way to resolve this? I heard people use signal and slot https://stackoverflow.com/questions/3268073/qobject-cannot-create-children-for-a-parent-that-is-in-a-different-thread

    Thank you very much

    JonBJ 1 Reply Last reply
    0
    • S StudyQt1

      Hi, I am plotting chart using qchart and qlineseries requiring lots of values, and the processing of values takes long time which blocks the GUI. To optimize this issue, I am trying to move the processing procedure into another thread. And I would like to ask 2 questions:

      1. I am now appending values into qlineseries like:

          for(int i = 0;  i < dataCount; i++)
          {
                series->append(val[i]);
          }
        

      I realized it's very time consuming, since I am appending one by one, I noticed there's QVXYModelMapper which can append the whole data model once, may I ask is it faster to use QVXYModelMapper ? I think maybe I can also try append(const QList<QPointF> &points); which loads all values just once.

      1. I am facing the thread affinity problem.

        First I created QChartView in GUI(main) thread:
        QChartView *chartView = new QChartView;

        Then I construct the worker class for appending data to line series, I move worker to another thread, loading data into qlineseries in worker::loadData

        First I do:
        Worker *worker = new worker(chartView);
        worker ->moveToThread(thread);

        Then in loadData:

        void Worker::loadData()
        {
                QLineSeries *series = new QLineSeries;
                // loading data into series, time-consuming
               //.....
               
               chartView->append(series);
              // chartView is constructed in main thread
             // Error: QObject::setParent: Cannot set parent, new parent is in a different thread
        }
        

      looks like I cannot change chartView which is constructed in main thread, is there a way to resolve this? I heard people use signal and slot https://stackoverflow.com/questions/3268073/qobject-cannot-create-children-for-a-parent-that-is-in-a-different-thread

      Thank you very much

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

      @StudyQt1
      I don't know about the first question. For the second one, in Qt only the main UI thread should access any UI stuff. So your computation thread cannot do the chartView->append(series) (and I don't think you should pass it to new worker(chartView);). The idea is that at the end the time-consuming series population the thread should emit signal passing the series to be plotted and the main UI thread should have a slot to receive it and do the chartView->append(series) in the main UI thread.

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @StudyQt1
        I don't know about the first question. For the second one, in Qt only the main UI thread should access any UI stuff. So your computation thread cannot do the chartView->append(series) (and I don't think you should pass it to new worker(chartView);). The idea is that at the end the time-consuming series population the thread should emit signal passing the series to be plotted and the main UI thread should have a slot to receive it and do the chartView->append(series) in the main UI thread.

        S Offline
        S Offline
        StudyQt1
        wrote on last edited by
        #3

        @JonB I think "UI stuff" means classes inherited from QWidget ? Thank you

        JonBJ 1 Reply Last reply
        0
        • S StudyQt1

          @JonB I think "UI stuff" means classes inherited from QWidget ? Thank you

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

          @StudyQt1 said in Plotting chart using lots of data:

          @JonB I think "UI stuff" means classes inherited from QWidget ?

          It's a bit more than just QWidget, hence my "UI stuff" terminology :) For example, QImage can be accessed in a secondary thread, but QPixmap cannot; QGraphics... objects are not QWidgets but can't be accessed by another thread. But yes, for your purposes, certainly all widgets.

          S 1 Reply Last reply
          0
          • A.A.SEZENA Offline
            A.A.SEZENA Offline
            A.A.SEZEN
            wrote on last edited by A.A.SEZEN
            #5

            @StudyQt1 said in Plotting chart using lots of data:

            chartView->append(series);

            I don't understand this.

            I think it will take more time if the lineseries are attached on the chart while you are loading the data. It would be an idea if you could give an example number for the lot of data and time you are talking about.

            S 1 Reply Last reply
            0
            • A.A.SEZENA A.A.SEZEN

              @StudyQt1 said in Plotting chart using lots of data:

              chartView->append(series);

              I don't understand this.

              I think it will take more time if the lineseries are attached on the chart while you are loading the data. It would be an idea if you could give an example number for the lot of data and time you are talking about.

              S Offline
              S Offline
              StudyQt1
              wrote on last edited by
              #6

              @A-A-SEZEN Thank you for your reply, later I found that chartView->append(series); actually doesn't take much time, so I put this in GUI thread

              1 Reply Last reply
              0
              • JonBJ JonB

                @StudyQt1 said in Plotting chart using lots of data:

                @JonB I think "UI stuff" means classes inherited from QWidget ?

                It's a bit more than just QWidget, hence my "UI stuff" terminology :) For example, QImage can be accessed in a secondary thread, but QPixmap cannot; QGraphics... objects are not QWidgets but can't be accessed by another thread. But yes, for your purposes, certainly all widgets.

                S Offline
                S Offline
                StudyQt1
                wrote on last edited by StudyQt1
                #7

                @JonB Thank you very much for your help, I put "GUI staff" back to GUI thread and the problem is resolved.

                May I ask another question: Long-time processing functions may cause GUI frozen & not responding, in this case we need to put them into new threads. But how to determine which functions are "long" freezing the GUI, which are "short"? Is there a generic way to decide?
                Now I just run those functions to "see" & "feel" which block the GUI from responding, this is too random

                jsulmJ 1 Reply Last reply
                0
                • S StudyQt1

                  @JonB Thank you very much for your help, I put "GUI staff" back to GUI thread and the problem is resolved.

                  May I ask another question: Long-time processing functions may cause GUI frozen & not responding, in this case we need to put them into new threads. But how to determine which functions are "long" freezing the GUI, which are "short"? Is there a generic way to decide?
                  Now I just run those functions to "see" & "feel" which block the GUI from responding, this is too random

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @StudyQt1 said in Plotting chart using lots of data:

                  Is there a generic way to decide?

                  Use a profiler. If you're using GCC you can use https://www.thegeekstuff.com/2012/08/gprof-tutorial/
                  Easier way which is often enough is simply to put debug output at the beginning and end of your methods/functions with timestamps and then check the output of your application while it is running.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1

                  • Login

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