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> 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; //}
-
@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?
-
I'm trying to learn c++ and qt also. I've only been working for 2 days.
-
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
YourMainWindow
is derived fromQMainWindow
. Yet within its constructor you create an instance of/local variableQMainWindow
. This does not make sense. -
@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 haveQMainWindow 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 declareMainWindow mainWindow;
-
Okey thank u