Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why am I using ui->graphicsView->setChart(chart); The program is compiled, but when it is running, it directly "ends abnormally"
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 350 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lee_Corl
    wrote on last edited by
    #1

    This is my ui
    d7c7cd43-674e-422f-9192-529a5411801c-image.png
    When I choose to comment out this sentence, the program shows an exception, but it runs without an error4eebb613-05a4-49ff-be8e-941c3790d6c7-image.png
    But when I didn't comment out this sentence, the program didn't work properly939ede82-0522-4464-900a-0ddd8d4b82a1-image.png

    #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();
        }
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by SGaist
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        L Offline
        L Offline
        Lee_Corl
        wrote on last edited by
        #3

        @SGaist ohhh,Thank you very much.It's OK!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 :-)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved