Reputation: 14712
So im writing a QT application that will read values from a serial port and display them in a graph during runtime. I managed to update my QChart during runtime with just a random generated value to try out updating in real time and it all works fine.
But my application slows down the more and more i append until it gets completely unusable.
I do understand that the list containing my points grows, but after a 100 points or so it really really slows down, thats really fast, it feels like i have some sort of memory leak?
I know the usual answer is "Don't use QCharts" but im a beginner at both C++ and QT so this is what i'm using for simplicity.
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QGridLayout>
#include <QLabel>
#include <QDebug>
QT_CHARTS_USE_NAMESPACE
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
series = new QLineSeries();
chart = new QChart();
chart->legend()->hide();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axes(Qt::Vertical).back()->setRange(-10, 10);
chart->axes(Qt::Horizontal).back()->setRange(0, 100);
chart->setContentsMargins(0, 0, 0, 0);
chart->setBackgroundRoundness(0);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
ui->setupUi(this);
QLabel *label = new QLabel();
label->setText("Hello World");
QGridLayout *layout = new QGridLayout;
QWidget * central = new QWidget();
setCentralWidget(central);
centralWidget()->setLayout(layout);
layout->addWidget(chartView, 0, 0);
clock = 0;
SerialPortReader *reader = new SerialPortReader(this);
connect(reader, SIGNAL(onReadValue(int)), this, SLOT(onReadValue(int)));
reader->run();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onReadValue(int value){
++clock;
series->append(clock + 30, value);
chart->axes(Qt::Horizontal).back()->setRange(0 + clock, 100 + clock);
}
SerialPortReader.cpp
#include "serialportreader.h"
#include "mainwindow.h"
#include <QtCore>
#include <QRandomGenerator>
#include <QDebug>
SerialPortReader::SerialPortReader(QObject *parent) : QThread(parent)
{
this->parent = parent;
this->randomGenerator = QRandomGenerator::global();
}
void SerialPortReader::run() {
QTimer *timer = new QTimer(this);
timer->start(100);
connect(timer, SIGNAL(timeout()), this, SLOT(readValue()));
}
void SerialPortReader::readValue() {
int value = randomGenerator->bounded(-10, 10);
emit onReadValue(value);
}
I was just wondering if anyone has any suggestion to what could be wrong? or if there is a anything i can do, except for changing chart-lib.
Upvotes: 0
Views: 2291
Reputation: 14712
After tinkering around i found out that the culprit wasn't actually a memory leak it was the:
chartView->setRenderHint(QPainter::Antialiasing);
As more and more data was presented the slower it got to do all the Antialiasing.
When i removed this everything suddenly went very smooth.
Upvotes: 1