PDA

View Full Version : File Problem



oonursrc
9th September 2011, 15:09
Does anyone know how to read a .txt file line by line and print each line to a label by using timer?
Label will be updated when new line comes.

Thank you :)

Lykurg
9th September 2011, 15:14
Yes, me! What exactly is your problem?

oonursrc
9th September 2011, 15:31
Im new in Qt. I have already tried many ways to solve this.

I want to read a txt file line by line and i want to print each line with label
For example: a. txt includes
3.2
4.3
5.4
by using timer or sleep func. a label should be updated
1. sec -- label.text= 3.2
2.sec --- label.text =4.3
etc

sorry for my bad english :)

Lykurg
9th September 2011, 17:11
So, then show us what you have so far. You wont get a ready to use code. Try to solve your problem in smaller steps and see what you really don't know. E.g. Can you read the file and store each line of text in a QStringList? etc.

oonursrc
11th September 2011, 13:19
this is what i have done



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTimer>
#include <QTextStream>
#include <QString>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

QTimer *timer = new QTimer(this);
timer->start(2000);
connect(timer,SIGNAL(timeout()),this,SLOT(DegerGet ir()));
}

MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow:: DegerGetir()
{
QFile file ( "C:/Users/Onur/Desktop/Deneme2.txt" ) ;
if ( !file.open ( QIODevice::ReadOnly | QIODevice::Text ))
{
qFatal("Dosya acilamadi!");return;
}
QTextStream in ( &file ) ;
QString line = in.readLine () ;
while ( !line.isNull ())
{
ui->textBrowser->append(line);
line = in.readLine () ;
}

file.close();

}


the file is opened again and again and program prints elements of file to QTextBrowser. but i want to print line by line with QLabel.

Thank you for your interest :)

Lykurg
11th September 2011, 13:49
Ok, seems you know almost everything ;-) So try to do:
- Read the file once at startup and cache it's content in a QStringList.
- create a private variable storing the current index of the QStringList.
- start the timer like you did and call a slot which, takes one item out of the QStringList and set this text to a label. (according to the index, and don't forget to count it)

..or does the content of the file change over time?

Best.

oonursrc
11th September 2011, 20:39
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTimer>
#include <QTextStream>
#include <QString>
#include <QVector>

QVector<QString> Veriler;
int i=0;
int count=0;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
DegerGetir();
}

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

void MainWindow::Ekle()
{
//ui->textBrowser->append(line);
if(i<count)
{
//ui->textBrowser->append(Veriler.at(i));
ui->lbldeger->setText(Veriler.at(i));
i++;
}

}

void MainWindow:: DegerGetir()
{
QTimer *timerEkle = new QTimer(this);
QFile file ( "C:/Users/Onur/Desktop/Deneme2.txt" ) ;
if ( !file.open ( QIODevice::ReadOnly | QIODevice::Text ))
{
qFatal("Dosya acilamadi!");return;
}

QTextStream in ( &file ) ;
//QString line = in.readLine () ;
QString line;
do
{
//ui->textBrowser->append(line);
line = in.readLine () ;
Veriler.append(line);
count++;
}while ( !line.isNull ());

file.close();

timerEkle->start(2000);
connect(timerEkle,SIGNAL(timeout()),this,SLOT(Ekle ()));
}




Thank you for your help. It really helps to me. I did this and it works nice :)