Automatically crashed my application at the time of using series->append() function in timer slot



  • I developed a simple QChart application for dynamically update values in every second using Qtimer concept. and this is my code...
    mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include<QtCharts>
    
    using namespace QtCharts;
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    public slots:
        void handleTimeout();
    
    private:
        QChart *chart;
        QChartView *chartView;
        QTimer *m_timer;
        //QSplineSeries *m_series;
        QLineSeries *m_series;
        QStringList m_titles;
        QValueAxis *m_axis;
        qreal m_step;
        qreal m_x;
        qreal m_y;
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    main.cpp:

    
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow window;
        window.show();
    
        return a.exec();
    }
    

    mainwindow.cpp:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include<QChart>
    #include<QChartView>
    #include<QLineSeries>
    #include<QAbstractSeries>
    #include<QGridLayout>
    #include<QTimer>
    
    #include<QtCharts/QChartView>
    #include<QtCharts>
    #include<QDebug>
    
    using namespace QtCharts;
    
    qreal x1=0;
    qreal z1=0;
    int i=0;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
           m_series = new QLineSeries();
    
           // create Chart Object (QWidget)
           chart = new QChart();
           chart->legend()->hide();
           chart->addSeries(m_series );
           chart->createDefaultAxes();
           chart->setTitle("Histogramm");
    
    
         QValueAxis *xAxis = new QValueAxis;
         QValueAxis *yAxis = new QValueAxis;
         chart->setAxisX(xAxis, m_series );
         chart->setAxisY(yAxis, m_series );
         xAxis->setMax(5);
         yAxis->setMax(5);
    
         m_timer = new QTimer();
    
         connect(m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout()));
         m_timer->setInterval(1000);
    
           // create a view inside the Chart
         QChartView *chartView = new QChartView(chart, this);
         ui->verticalLayout->addWidget(chartView);
    
        chart->setAnimationOptions(QChart::AllAnimations);
    
           m_timer->start();
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::handleTimeout()
    {
        if(i>10)
          {
            m_timer->stop();
          }
        else {
               x1 += 1;
               z1 += 0.5;
                 i += 1;
               m_series->append(x1,z1);
               ui->label->setText(QString::number(i));
             }
    
       QChartView *chartView = new QChartView(chart, this);
       ui->verticalLayout->addWidget(chartView);
    }
    
    

    if the code doesn't works, when i was using "m_series->append()" function in timer slot and application automatically crashed.


  • Qt Champions 2018

    I don't see a place where you initialize m_series.



  • @Christian-Ehrlicher
    sorry i was forgot my actual code now i was updated.


  • Moderators

    @Sekhar
    @Christian-Ehrlicher point still stands, where do you initialize m_series? in your constructor your initialize series, clearly a different variable



  • problem solved. Thanks for your guidance.


  • Qt Champions 2018

    @Sekhar Then please mark the topic as solved, thx


Log in to reply