簡體   English   中英

QChart在QT應用中的速度變慢

[英]QChart slows down in QT application

因此,我編寫了一個QT應用程序,該應用程序將從串行端口讀取值並在運行時將其顯示在圖形中。 我設法在運行時使用隨機生成的值更新了QChart,以嘗試實時更新,並且一切正常。

但是我的應用程序使我添加的速度越來越慢,直到完全無法使用為止。

我確實知道包含我的分數的列表會增加,但是在100分數左右之后,它的確真的變慢了,那真的很快,感覺就像我有某種內存泄漏嗎?

我知道通常的答案是“不要使用QCharts”,但是我是C ++和QT的初學者,所以這是我為了簡單起見使用的。

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);
}

我只是想知道是否有人對可能出什么問題有任何建議? 還是有什么我可以做的,除了更改chart-lib。

修補之后,我發現罪魁禍首實際上不是內存泄漏,而是:

chartView->setRenderHint(QPainter::Antialiasing);

隨着提供的數據越來越多,所有抗鋸齒處理的速度也越來越慢。

當我刪除此時,一切突然變得非常順利。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM