QCustomPlot Discussion and Comments

How can I plot my datas reading from a file Return to overview


Hi. I want to plot datas which are read from txt file. How can i do it? What should be the next step?


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
#include <QTimer>
#include <QList>
#include <qlist.h>
#include <QDebug>
#include <QtCharts/QLineSeries>
#include <QtCharts/QChart>
#include <QtCharts/QDateTimeAxis>
#include <QDateTime>
#include <QtCharts/QValueAxis>
#include <QtCharts/QChartView>
#include <QLine>
#include <QString>


QT_CHARTS_USE_NAMESPACE
using namespace QtCharts;

QTimer *timer = new QTimer();

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(timer, SIGNAL(timeout()), SLOT(Timer_Slot()));

}

void MainWindow::Timer_Slot() {

   QString line = allLines[curIndex];
  ui->plainTextEdit->setPlainText(line);
  if (curIndex < allLines.size() - 1 ) {
          curIndex++;
   }

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_2_clicked()  //write
{
    QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt");
    if(!file.open(QFile::WriteOnly | QFile::Text )) {

        QMessageBox::warning(this, "title", "file not open");


    }

    QTextStream out (&file);
    QString text = ui->plainTextEdit->toPlainText();
    out << text;
    file.flush();
    file.close();


}

void MainWindow::on_pushButton_clicked()  //read
{

   //  QLineSeries *series = new QLineSeries();

    QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt");
     if (file.open(QIODevice::ReadOnly)) {
         QTextStream in (&file);
         while (!in.atEnd()) {
             //QString line = in.readLine();
             //allLines.append(line);

             QString line = in.readLine();
             allLines.append(line);

             QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
             for(const QString &entry : list) {
             double num = entry.toDouble();

          qDebug()<<num;
             }
 }}
  file.close();
  curIndex=0;
  timer->start(200);

}
   0  

create a QVector for your values and a QVector for your keys and pass them into setData on the graph

Can you show me?

https://www.qcustomplot.com/index.php/tutorials/basicplotting

except you fill in the values of x and y with the values you got from your file.

You could use https://github.com/BGodding/SmartPlot as an example