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. Read data from txt and plot it in real time

Read data from txt and plot it in real time

Scheduled Pinned Locked Moved Unsolved General and Desktop
fileplottingrealtime
9 Posts 3 Posters 1.6k Views
  • 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.
  • D Offline
    D Offline
    deleted286
    wrote on last edited by
    #1

    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>
    
    QT_CHARTS_USE_NAMESPACE
    
    using namespace QtCharts;
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    
        QTimer *t1 = new QTimer(this);
        connect(t1, SIGNAL(timeout()), this, SLOT(sunSpot()));
        t1->start(0);
    
        QTimer *t2 = new QTimer(this);
        connect(t2, SIGNAL(timeout()), this, SLOT(chart()));
    
        t2->start(5);
    
    
    QLineSeries *series = new QLineSeries();
    
    
    QFile sunSpots(":sun");
    
    if (!sunSpots.open(QIODevice::ReadOnly | 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
            continue;
        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->setLabelFormat("%i");
       axisY->setTitleText("Sunspots count");
       chart->addAxis(axisY, Qt::AlignLeft);
       series->attachAxis(axisY);
    
        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
    
    
      QMainWindow window;
      window.setCentralWidget(chartView);
      window.resize(820, 600);
      window.show();
    
    
    }
    
    
    
    //MainWindow::~MainWindow()
    //{
    
    
    //    delete ui;
    
    //}
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

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

      QMainWindow window;
      window.setCentralWidget(chartView);
      window.resize(820, 600);
      window.show();

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted286
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          D 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

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

            D Offline
            D Offline
            deleted286
            wrote on last edited by
            #5

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

            JonBJ 1 Reply Last reply
            0
            • D deleted286

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

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

              D 1 Reply Last reply
              0
              • JonBJ JonB

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

                D Offline
                D Offline
                deleted286
                wrote on last edited by
                #7

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

                JonBJ 1 Reply Last reply
                0
                • D deleted286

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @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.setCentralWidget(chartView);
                    window.resize(820, 600);
                    window.show();
                  

                  you probably want to do this on your this instance:

                    setCentralWidget(chartView);
                    resize(820, 600);
                    show();
                  

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

                  MainWindow mainWindow;
                  
                  1 Reply Last reply
                  2
                  • D Offline
                    D Offline
                    deleted286
                    wrote on last edited by
                    #9

                    Okey thank u

                    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