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
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
Yes, me! What exactly is your problem?
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
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.
this is what i have done
Qt Code:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #include <QTimer> #include <QTextStream> #include <QString> ui(new Ui::MainWindow) { ui->setupUi(this); timer->start(2000); connect(timer,SIGNAL(timeout()),this,SLOT(DegerGetir())); } MainWindow::~MainWindow() { delete ui; } void MainWindow:: DegerGetir() { { qFatal("Dosya acilamadi!");return; } while ( !line.isNull ()) { ui->textBrowser->append(line); line = in.readLine () ; } file.close(); }To copy to clipboard, switch view to plain text mode
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
Last edited by Lykurg; 11th September 2011 at 12:44. Reason: missing [code] tags
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.
#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
Bookmarks