Important: Please read the Qt Code of Conduct -

Read data from txt and plot it in real time

  • I have an txt file. I want to do this in order;
    First read the data from txt file and than plot it in an order. Here is my code, it doesnt work but i dont know why. Im so new on qt platform. I'm open to any suggestions.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QMainWindow>
    #include <QTimer>
    #include <QtCharts/QLineSeries>
    #include <QtCore/QFile>
    #include <QtCore/QTextStream>
    #include <QtCore/QDebug>
    #include <QtCharts/QValueAxis>
    #include <QtCharts/QChartView>
    #include <QtCore/QDateTime>
    #include <QtCharts/QDateTimeAxis>
    using namespace QtCharts;
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        QTimer *t1 = new QTimer(this);
        connect(t1, SIGNAL(timeout()), this, SLOT(sunSpot()));
        QTimer *t2 = new QTimer(this);
        connect(t2, SIGNAL(timeout()), this, SLOT(chart()));
    QLineSeries *series = new QLineSeries();
    QFile sunSpots(":sun");
    if (! | QIODevice::Text)) {
    QTextStream stream(&sunSpots);
    while (!stream.atEnd()) {
        QString line = stream.readLine(); //satır satır okuma yap
        if (line.startsWith("#") || line.startsWith(":")) //# veya : ile başlayıyorsa devam et, hiçbir şey yapma
        QStringList values = line.split(QLatin1Char(' '), Qt::SkipEmptyParts); //boşlukları ayırıyor ve atıyor
        QDateTime momentInTime;
        momentInTime.setDate(QDate(values[0].toInt(), values[1].toInt() , 15)); //?
        qDebug()<< momentInTime;
        series->append(momentInTime.toMSecsSinceEpoch(), values[2].toDouble());
    sunSpots.close(); //açtığımız txt dosyasını kapat
    QChart *chart = new QChart();
    QDateTimeAxis *axisX = new QDateTimeAxis; //eksenleri ayarlıyoruz. QLineSeries
       axisX->setTickCount(10); //x ekseninde gösterilecek veri sayısı.
       axisX->setFormat("MMM yyyy"); //format ay ve yıl olacak.
       axisX->setTitleText("Date"); //x ekseninin altndaki başlık date olacak.
       chart->addAxis(axisX, Qt::AlignBottom);
       series->attachAxis(axisX); //seriye eklediğimiz bilgileri x eksenine koyuyoruz.
       QValueAxis *axisY = new QValueAxis;
       axisY->setTitleText("Sunspots count");
       chart->addAxis(axisY, Qt::AlignLeft);
        QChartView *chartView = new QChartView(chart);
      QMainWindow window;
      window.resize(820, 600);;
    //    delete ui;

  • Lifetime Qt Champion

    @suslucoder said in Read data from txt and plot it in real time:

    QMainWindow window;
    window.resize(820, 600);;

    c++ basics - how long do you think does this object lives?

  • I'm trying to learn c++ and qt also. I've only been working for 2 days.

  • Lifetime Qt Champion

    Then I would not start with such a complex problem but go for a c++ book and learn the basics. Sorry.

  • @Christian-Ehrlicher You are free to not help. I'm sure there will be others willing to help. Thanks for your suggestion

  • @suslucoder
    Your MainWindow is derived from QMainWindow. Yet within its constructor you create an instance of/local variable QMainWindow. This does not make sense.

  • @JonB I got it thank you. Is there any easy way to read data from txt and plot it?

  • @suslucoder
    Well, this has nothing at all to do with reading text or plotting. First you need to get a sensible main window going.

    At a guess you intend your MainWindow to be the main window; you don't want to create some other main window instance. So where you have

      QMainWindow window;
      window.resize(820, 600);;

    you probably want to do this on your this instance:

      resize(820, 600);

    And then your C++ main() is the place to declare

    MainWindow mainWindow;

  • Okey thank u