Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help about QChart and repainting it



  • Hello everyone, I'm new in this community, as well as in QT programming and GUI programming. I'm using QT Creator to develop a project for the university and, since I don't have much knowledge about how programming a GUI except a bit of C#, I'm sure some of my questions could sound stupid.
    First of all, one of the page of my GUI should plot a QChart though a QChartView. In it there is a QScatterSeries with 0 points at first. Then, around every minute, in the final version of the project a thread should create a list of points to be added and a list of points to be removed, let's call it T1, which is a non-GUI thread (potentially written with standard C++ library, non QT) focused on collecting data (e.g. though socket connections on a LAN). At the moment, let's focus just on the list of points to add.
    My idea is the following: when the T1 has collected all the data (every minute, every 1.5 minutes) signals it with a std::condition_variable. I did that the GUI thread, when creating the layout, uses a thread (let's call T2) of the QThreadPool::globalInstance() to wait for the notification on this condition variable.
    Then, I tried to pass the pointer of the QScatterSeries to the istance of the subclass of QRunnable and in the method run I wrote the following code:

      *series << QPointF(x, y)<< QPointF(x, y+1)<< QPointF(x, y+2)<< QPointF(x, y+3)<< QPointF(x, y+4);
    

    And I noticed that these points are immadiately showed on the screen! But this T2 is a thread of the pool, not the GUI thread. How is it possible? Or it's normal but I should avoid? I thought that in the method run I should have emited a signal to slot which the GUi thread should have executed, to repaint the QChartView.
    Another question: this way am I paint a point at time? What could I do do add many points and showing them all together?

    I hope to have been the clearest as possible, and thank you for the attention!



  • Been a few months since I did some stuff with QCharts....

    If I remember right you add series to the chart - in which case when you modify the series, you will see that modification right away, From what I am seeing you are dereferencing a pointer that is likely to that series. Regardless of thread, if you have a pointer it will write to the original address and that is probably why you are getting this unexpected behavior.



  • Yes, you're perfectly right. So is it legit what I did, or is it against the principle of GUi Programming?

    Besides I tried to do the followinf double for cycle:

    for(x=0; x < 15; x+=0.2)
                for(y=0; y < 10; y +=0.2, count++)
                    series->insert(count, QPointF(x, y));
    

    And it works in both the GUI Thread and T2, but I imagined the GUi immediately freezes and can't continua to work, so I should find a way to process all the series together I suppose..



  • @KuriGohan

    1. QSeries << operator (https://doc.qt.io/qt-5.11/qxyseries.html#operator-lt-lt) is just the same as QSeries::append(). I don't know whether QSeries::insert() is any slower than append() (try it and see?), otherwise I would expect them to take the same amount of time.

    2. The difference is your second code is adding a lot more points than your first!

    3. I think there was a thread recently where we were discussing the speed/GUI "freeze" when adding lots of points. You're adding say 2,500 points. That takes time! You can make the GUI be more responsive by calling http://doc.qt.io/qt-5/qcoreapplication.html#processEvents() intermittently, e.g. each time round your x loop (not too frequently, though, else it will actually slow things down; I think we found every 100 points or so was good). Also, you are adding your points one at a time; something like https://doc.qt.io/qt-5.11/qxyseries.html#append-2 adds a list, I can't recall if we found that was any faster.


Log in to reply