Read line by line in a specific time interval
-
Hi guys,
I am trying to transfer the content of a text file to be displayed to the textEdit widget inside my GUI.
How can I make it display at the text edit one line at a time, instead of displaying all the content of the text file into the textEdit widget at one time?
By using "readLine" it can only display the first line of the text file content. How can I make it display the second line of the content after, let say 2 second?
I am a new guy learning Qt Programming, so please be gentle to me~~ ^^
-
Hi guys,
I am trying to transfer the content of a text file to be displayed to the textEdit widget inside my GUI.
How can I make it display at the text edit one line at a time, instead of displaying all the content of the text file into the textEdit widget at one time?
By using "readLine" it can only display the first line of the text file content. How can I make it display the second line of the content after, let say 2 second?
I am a new guy learning Qt Programming, so please be gentle to me~~ ^^
@Shahmisufi
Hi...Try this,
QFile inputFile(fileName);
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
}
inputFile.close();
} -
@Shahmisufi
Hi...Try this,
QFile inputFile(fileName);
if (inputFile.open(QIODevice::ReadOnly))
{
QTextStream in(&inputFile);
while (!in.atEnd())
{
QString line = in.readLine();
}
inputFile.close();
}@Venkatesh-V thank you...
I have tried the code, but it will not display at the textEdit widget one at a time. Instead, it will read it line by line and display it inside the textedit widget all in one time.
It does not able to display line by line between intervals
-
@Venkatesh-V thank you...
I have tried the code, but it will not display at the textEdit widget one at a time. Instead, it will read it line by line and display it inside the textedit widget all in one time.
It does not able to display line by line between intervals
For that you need to store all the lines in QList. after that take a Qtimer instance set it 2 second interval and start the timer, once the timer emits timeout connect to another slot there you can take one value at a time from the list and set to textedit.
-
For that you need to store all the lines in QList. after that take a Qtimer instance set it 2 second interval and start the timer, once the timer emits timeout connect to another slot there you can take one value at a time from the list and set to textedit.
This post is deleted! -
@Venkatesh-V thank you...
I have tried the code, but it will not display at the textEdit widget one at a time. Instead, it will read it line by line and display it inside the textedit widget all in one time.
It does not able to display line by line between intervals
//Header File QList<QString> LineList; int count = 0; //SourceFile { QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); while (!in.atEnd()) { QString line = in.readLine(); LineList.apppend(line); } inputFile.close(); } QTimer *timer = new QTimer; timer->setInterval(2000); timer->start(); connect(timer,SIGNAL(timeOut(),this,SLOT(readLine())); } void readLine() { textEdit->setText(textEdit->text()+LineList.at(count)); count++; }
-
Alternative, less memory hungry but needs the file not to be deleted until the process finished:
QFile* inputFile=new QFile(fileName,textEdit); if (!inputFile.open(QIODevice::ReadOnly)){ inputFile->deleteLater(); return; } QTimer* inputTimer= new QTimer(textEdit); auto readSingleLine = [=]()->void{ QTextStream in(inputFile); if(in.atEnd()){ inputFile->deleteLater(); inputTimer->deleteLater(); return; } const QString line = in.readLine(); textEdit->moveCursor(QTextCursor::End); textEdit->textCursor().insertText(line); //not sure if you need to manually add the newline here }; QObject::connect(inputTimer,&QTimer::timeout,textEdit,readSingleLine); readSingleLine(); inputTimer->start(2000); //2 seconds
-
//Header File QList<QString> LineList; int count = 0; //SourceFile { QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); while (!in.atEnd()) { QString line = in.readLine(); LineList.apppend(line); } inputFile.close(); } QTimer *timer = new QTimer; timer->setInterval(2000); timer->start(); connect(timer,SIGNAL(timeOut(),this,SLOT(readLine())); } void readLine() { textEdit->setText(textEdit->text()+LineList.at(count)); count++; }
@Venkatesh-V , thank you. will update to you once I've run the program
-
//Header File QList<QString> LineList; int count = 0; //SourceFile { QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); while (!in.atEnd()) { QString line = in.readLine(); LineList.apppend(line); } inputFile.close(); } QTimer *timer = new QTimer; timer->setInterval(2000); timer->start(); connect(timer,SIGNAL(timeOut(),this,SLOT(readLine())); } void readLine() { textEdit->setText(textEdit->text()+LineList.at(count)); count++; }
@Venkatesh-V Dear sir I have tried your code, but when I run i the issue highlighted is
" 'Class QTextEdit' has no member named 'text' "
for the code line below :
textEdit->setText(textEdit->text()+LineList.at(count));
-
@Venkatesh-V Dear sir I have tried your code, but when I run i the issue highlighted is
" 'Class QTextEdit' has no member named 'text' "
for the code line below :
textEdit->setText(textEdit->text()+LineList.at(count));
-
@Venkatesh-V Dear sir I have tried your code, but when I run i the issue highlighted is
" 'Class QTextEdit' has no member named 'text' "
for the code line below :
textEdit->setText(textEdit->text()+LineList.at(count));
Hi,
Sorry, as @m-sue said use toPlainText() method to get current text of textEdit. this will resolve your query.
-
//Header File QList<QString> LineList; int count = 0; //SourceFile { QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); while (!in.atEnd()) { QString line = in.readLine(); LineList.apppend(line); } inputFile.close(); } QTimer *timer = new QTimer; timer->setInterval(2000); timer->start(); connect(timer,SIGNAL(timeOut(),this,SLOT(readLine())); } void readLine() { textEdit->setText(textEdit->text()+LineList.at(count)); count++; }
@Venkatesh-V dear sir, I have some error at this line
void readLine() { textEdit->setText(textEdit->toPlainText()+LineList.at(count)); count++; }
- 'textEdit' was not declared in this scope
- 'LineList' was not declared in this scope'count' was not declared in this scope
- 'count' was not declared in this scope
So, i have changed from
void readLine()
to
void Speedometer::readLine()
hence, its executable, but then it doesn't respond as we wanted. It does not display any text inside the TextEdit widget.
-
@Venkatesh-V dear sir, I have some error at this line
void readLine() { textEdit->setText(textEdit->toPlainText()+LineList.at(count)); count++; }
- 'textEdit' was not declared in this scope
- 'LineList' was not declared in this scope'count' was not declared in this scope
- 'count' was not declared in this scope
So, i have changed from
void readLine()
to
void Speedometer::readLine()
hence, its executable, but then it doesn't respond as we wanted. It does not display any text inside the TextEdit widget.
Hi,
have you declared readLine() in public slots section?//In Header File
public slots:
void readLine();