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
QtWS25 Last Chance

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 30 Dec 2020, 12:15 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 Dec 2020, 12:23 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 30 Dec 2020, 12:26 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
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 30 Dec 2020, 12:27 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 30 Dec 2020, 12:43
          3
          • C Christian Ehrlicher
            30 Dec 2020, 12:27

            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 30 Dec 2020, 12:43 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

            J 1 Reply Last reply 30 Dec 2020, 13:01
            0
            • D deleted286
              30 Dec 2020, 12:43

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

              J Offline
              J Offline
              JonB
              wrote on 30 Dec 2020, 13:01 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 30 Dec 2020, 13:07
              0
              • J JonB
                30 Dec 2020, 13:01

                @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 30 Dec 2020, 13:07 last edited by
                #7

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

                J 1 Reply Last reply 30 Dec 2020, 13:23
                0
                • D deleted286
                  30 Dec 2020, 13:07

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 30 Dec 2020, 13:23 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 30 Dec 2020, 13:41 last edited by
                    #9

                    Okey thank u

                    1 Reply Last reply
                    0

                    5/9

                    30 Dec 2020, 12:43

                    • Login

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