Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Read data from txt and plot it in real time

    General and Desktop
    file plotting realtime
    3
    9
    642
    Loading More Posts
    • 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
      deleted286 last edited by

      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 Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by Christian Ehrlicher

        @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 has to stay free or it will die.

        1 Reply Last reply Reply Quote 0
        • D
          deleted286 last edited by

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

          1 Reply Last reply Reply Quote 0
          • Christian Ehrlicher
            Christian Ehrlicher Lifetime Qt Champion last edited by

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

            Qt has to stay free or it will die.

            D 1 Reply Last reply Reply Quote 3
            • D
              deleted286 @Christian Ehrlicher last edited by

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

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @deleted286 last edited by 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 1 Reply Last reply Reply Quote 0
                • D
                  deleted286 @JonB last edited by

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

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @deleted286 last edited by

                    @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 Reply Quote 2
                    • D
                      deleted286 last edited by

                      Okey thank u

                      1 Reply Last reply Reply Quote 0
                      • First post
                        Last post