Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Chart cannot read datas from txt



  • It will create a chart, but there is no data on it. Datas on my txt cannot seen on chart. How can I handle with it? Do I use another timer or am i wrong any other part of it?

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QtCharts/QLineSeries>
    #include <QTimer>
    #include <QTextStream>
    #include <QtCharts>
    #include <QChart>
    #include <QString>
    
    
    
    QT_CHARTS_USE_NAMESPACE
    
    using namespace QtCharts;
    
    
    
    QLineSeries *series = new QLineSeries();
    QTimer *timer = new QTimer();
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
      connect(timer, SIGNAL(timeout()), SLOT(Timer_Slot()));
    
        initChartView();
    
    }
    
    void MainWindow::Timer_Slot(){
        QString line = allLines[curIndex];
        ui->plainTextEdit->setPlainText(line);
        if(curIndex < allLines.size() -1 ) {
            curIndex++;
        }
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
    
        QFile file("C:/Users/ilknu/Documents/SnDeneme/deneme.txt");
    
        if (file.open(QIODevice::ReadOnly)) {
              QTextStream in (&file);
              while (!in.atEnd()) {
              QString line = in.readLine();
              allLines.append(line);
    
          QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                for(const QString &entry : list) {
                double num = entry.toDouble();
                qDebug()<<num;
                     series->append(num, num);
                    }
    
    
    
                }
                }
        file.close();
        curIndex=0;
        timer->start(300);
    }
    
    void MainWindow::initChartView(){
    
    
    
       QChart *chart = new QChart();
          chart->addSeries(series);
          chart->legend()->hide();
          chart->setTitle("deneme");
    
          QValueAxis *axisX = new QValueAxis();
          axisX->setTitleText("Points");
          axisX->setLabelFormat("yyyy");
          axisX->setTickCount(series->count());
          chart->addAxis(axisX, Qt::AlignBottom);
          series->attachAxis(axisX);
    
          QLogValueAxis *axisY = new QLogValueAxis();
          axisY->setTitleText("Values");
          axisY->setLabelFormat("%i");
          axisY->setBase(8);
          chart->addAxis(axisY, Qt::AlignLeft);
          series->attachAxis(axisY);
    
          QChartView *chartView = new QChartView(chart);
          chartView->setRenderHint(QPainter::Antialiasing);
    
          connect(series, &QLineSeries::hovered, this, &MainWindow::on_pushButton_clicked);
          this->setCentralWidget(chartView);
    
    }
    

  • Lifetime Qt Champion

    @suslucoder said in Chart cannot read datas from txt:

    qDebug()<<num;
    series->append(num, num);
    }

    And where in your code do you add series to the chart?!



  • @jsulm chart->addSeries(series);

    is it wrong?


  • Lifetime Qt Champion

    @suslucoder said in Chart cannot read datas from txt:

    chart->addSeries(series);

    I think you need to do this after populating series with your data from the file.



  • @jsulm for doing it chart should be declared globally. When i declared it global, it causes a crash . any options?


  • Lifetime Qt Champion

    @suslucoder said in Chart cannot read datas from txt:

    chart should be declared globally

    Why do you think so?
    You just need to declare QChart *chart as member variable instead of a local variable in initChartView()...



  • @jsulm you mean in header file, right?


  • Lifetime Qt Champion

    @suslucoder Yes, inside your class declaration as private member variable...



  • @jsulm I've tried. It says Series not in the chart. Please addSeries to chart first.


Log in to reply