QTChart draws line from first to last point
-
Hi i'm using QT chart to draw Real Time series with data coming from variable "new_vibe" wich it's refreshed all time.
My serie are composed of 15 points, to refresh ma serie, i append to each point the value of the point +1 and i append "new_vibe" to the point 15.There is the code of my thread :
while(1) { new_vibe = new_data(); //function that refresh data series->clear(); for(int j=0; j <= 15; j++) { if(j == 15) { graph[j] = VIBRATION; } else { graph[j] = graph[j+1]; } series->append(QPointF(j, graph[j])); } chart->update(); QThread::msleep(500); } the refresh process work fine and i check that data are correct, but draws line from , you can see below, i try to append in the order 15 -> 0 but nothing change ! can some one help me ? thanks. ![ll.png](https://ddgobkiprc33d.cloudfront.net/dabb007e-f54b-43c8-8d80-501c4795b429.png)
-
I have never used
QTChart
, but I am sure that this code can NOT work.You should know that Qt is an asynchronous framework and requires that event loop of thread can be called regularly, so that events and signals/slots can be handled.
So please avoid forever loops, this will lock the event queue of the thread!
For your code, I would recommend you to use a
QTimer
.
Something like this:auto myTimer = new QTimer(this); connect(myTimer, &QTimer::timeout, this, [this]() { series->clear(); for(int j=0; j <= 15; j++) { graph[j] = (j == 15) ? VIBRATION : graph[j+1]; series->append(QPointF(j, graph[j])); } chart->update(); }); myTimer->setInterval(500); myTimer->start();
-
@KroMignon thanks ! it's resolve my problem !
i was thinking that create a specifique thread to refresh my chart will be the best solution, but the wil block all signal and slot of the QTCHART if i understand your explanation, but if i want to to this function in specific thread, should i create and remove the thread every time ? beacause i want to refresh th chart every 100ms and i have anothers (signal -slot) that work with timer ...Best regards
-
@jawad_soft said in QTChart draws line from first to last point:
i was thinking that create a specifique thread to refresh my chart
This is also a very bad idea, all graphical items must be in main thread (cf. https://doc.qt.io/qt-5/thread-basics.html).
but the wil block all signal and slot of the QTCHART if i understand your explanation
No, what locks the thread event loop, is the forever loop (eg.
while(1) {... }
orQThread::sleep()
, etc. ).but if i want to to this function in specific thread, should i create and remove the thread every time ?
No, why create/destroy thread? But do you really need to use an extra thread ?!?
-
@KroMignon Thanks for reply,
i don't really need another thread from the main thread, i'm just asking my self how QT manage all QTIMER when we use many of theme if they have all fast interval ! and maybe in my case if i have one thread manage to refresh data and another that refresh graphic, maybe that will let all the QT APP work well !
again thanks for your help