Why am I using ui->graphicsView->setChart(chart); The program is compiled, but when it is running, it directly "ends abnormally"
-
This is my ui

When I choose to comment out this sentence, the program shows an exception, but it runs without an error
But when I didn't comment out this sentence, the program didn't work properly
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QTimer> // 定时器 #include <QTime> // 用于设置随机数种子 #include <qdebug.h> // 用于调试输出 #include <QMessageBox> float PID_Work(float Error, float kp, float ki, float kd); #define Ts (1000) MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { QTimer *tim = new QTimer(this); QChart *chart = new QChart(); chart->setTitle("PID算法"); chart->legend()->setVisible(true); /* 坐标轴 */ QValueAxis *AxisX = new QValueAxis(this); QValueAxis *AxisY = new QValueAxis(this); // chart->setAxisX(AxisX); // chart->setAxisY(AxisY); chart->addAxis(AxisX, Qt::AlignBottom); chart->addAxis(AxisY, Qt::AlignLeft); AxisX->setRange(0, 100); AxisY->setRange(0, 100); AxisX->setTitleText("时间/ms"); AxisY->setTitleText("温度/°C"); // /* 曲线 */ QSplineSeries *series = new QSplineSeries(this); chart->addSeries(series); series->attachAxis(AxisX); series->attachAxis(AxisY); series->setPointLabelsClipping(false); series->setName("PID曲线"); QPen pen; pen.setColor(QColor(0, 0, 255)); series->setPen(pen); if (!ui->graphicsView) { qDebug() << "graphicsView is not initialized!"; return; } ui->graphicsView->setChart(chart); connect(this, &MainWindow::StartChart, this, [=](){ tim->start(Period); }); connect(tim, &QTimer::timeout, this, [=](){ static int i = 0; float ret = PID_Work((GoalNum - CurrentNum), KP, KI, KD); series->append(i * Period, ret); CurrentNum += ret; qDebug()<<"OUTPUT = "<< PID_output; qDebug()<<"CurrentTemp = "<< CurrentNum; }); } MainWindow::~MainWindow() { delete ui; } float PID_Work(float Error, float kp, float ki, float kd) { static float lastError = 0.0f; static float Errorsum = 0.0f; // 设置随机数种子 qsrand(QTime::currentTime().msec()); /* 生成 0 到 30 之间的随机浮点数 用于假设环境中存在的干扰*/ float randomFloat = (qrand() / static_cast<float>(RAND_MAX)) * 30; qDebug() << "生成的随机浮点数是:" << randomFloat; Errorsum += Error; float derivative = (Error - lastError) / Ts; // 计算微分项 lastError = Error; // 更新 lastError 在计算完微分项之后 return kp * Error + ki * Errorsum + kd * derivative - randomFloat; } void MainWindow::on_pushButton_clicked() { static bool ok = true; static bool tmp; QString str = ui->GoalTemp->text(); GoalNum = str.toFloat(&tmp); ok = ok & tmp; str = ui->InitTemp->text(); CurrentNum = str.toFloat(&tmp); ok = ok & tmp; str = ui->Period->text(); Period = str.toInt(&tmp); ok = ok & tmp; str = ui->KP->text(); KP = str.toFloat(&tmp); ok = ok & tmp; str = ui->KI->text(); KI = str.toFloat(&tmp); ok = ok & tmp; str = ui->KD->text(); KD = str.toFloat(&tmp); ok = ok & tmp; if(!ok) { QMessageBox::warning(this, "错误警告", "输入错误"); }else { emit StartChart(); } } -
Hi and welcome to devnet,
You deleted the setupUi call which means that your widget is not properly initialized before you start using its components.
-
Hi and welcome to devnet,
You deleted the setupUi call which means that your widget is not properly initialized before you start using its components.
-
Great !
Then please mark the thread as solved using the Topic Tools button or the three dotted menu beside the answer you deem correct so other forum users may know a solution has been found :-)