我开始QtCharts
在我的应用程序中使用。我正在考虑的图表是折线图,使用对象QChart
和QLineSeries
。由于所有点都是动态添加的,我使用信号/槽系统来更新图表:
QLineSeries* serie = new QLineSeries(this);
connect(serie, SIGNAL(pointAdded(int)), this, SLOT(onPointAdded(int)));
void MyChart::onPointAdded(int index) {
// Delete the first items if the number of points has reached a threshold
while (serie->points().length() >= threshold)
serie->remove(0);
}
当在(对象)onPointAdded
中添加一个点时调用该函数。我给出的代码片段删除了图中的第一个点,例如图中的点数始终是固定的(开头除外)。serie
QLineSeries
serie
当我在 中运行此代码时Release
,没有问题。但是,当我运行它Debug
并且点数达到阈值时,我收到以下错误消息:
此对话框不会停止程序,但每次添加一个点(并达到阈值)时,都会在前一个对话框的顶部出现一个新对话框。
以下是重现错误的最少代码:
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QChart>
#include <QLineSeries>
#include <QMainWindow>
#include <QValueAxis>
#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
QT_CHARTS_USE_NAMESPACE
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
QChart* chart = nullptr;
QLineSeries* serie = nullptr;
int threshold = 5;
private slots:
void onAddPointButtonClicked();
void onPointAdded(int index);
};
#endif // MAINWINDOW_H
主窗口.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
chart = new QChart;
serie = new QLineSeries(this);
connect(ui->bt_addPoint, SIGNAL(clicked()), this, SLOT(onAddPointButtonClicked()));
connect(serie, SIGNAL(pointAdded(int)), this, SLOT(onPointAdded(int)));
chart->legend()->hide();
chart->addSeries(serie);
ui->graphicsView->setChart(chart);
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::onAddPointButtonClicked() {
serie->append(0, 1);
}
void MainWindow::onPointAdded(int index) {
while (serie->points().length() >= threshold)
serie->remove(0);
}
我使用UI 表单来生成图形界面。这个接口包含一个QChartView
和一个QPushButton
(动态添加点)。
我的 Qt 版本是5.11.2,并且该错误是使用MSVC 2017 64-bits产生的。需要插件QtCharts才能使用QChart
,QChartView
和QLineSeries
.
我想知道是否可以解决此问题或禁用 Qt 调试对话框消息。