简体   繁体   English

QChart在QT应用中的速度变慢

[英]QChart slows down in QT application

So im writing a QT application that will read values from a serial port and display them in a graph during runtime. 因此,我编写了一个QT应用程序,该应用程序将从串行端口读取值并在运行时将其显示在图形中。 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. 我设法在运行时使用随机生成的值更新了QChart,以尝试实时更新,并且一切正常。

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? 我确实知道包含我的分数的列表会增加,但是在100分数左右之后,它的确真的变慢了,那真的很快,感觉就像我有某种内存泄漏吗?

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. 我知道通常的答案是“不要使用QCharts”,但是我是C ++和QT的初学者,所以这是我为了简单起见使用的。

MainWindow.cpp 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 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. 还是有什么我可以做的,除了更改chart-lib。

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. 当我删除此时,一切突然变得非常顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM