Read line by line in a specific time interval
-
wrote on 2 Jun 2017, 01:59 last edited by
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~~ ^^
wrote on 2 Jun 2017, 03:42 last edited by@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();
}wrote on 2 Jun 2017, 04:05 last edited by@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
wrote on 2 Jun 2017, 04:14 last edited byFor 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.
wrote on 2 Jun 2017, 05:59 last edited byThis 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
wrote on 2 Jun 2017, 06:02 last edited by VRonin 6 Feb 2017, 07:06//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++; }
-
wrote on 2 Jun 2017, 07:16 last edited by VRonin 6 Mar 2017, 10:53
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++; }
wrote on 5 Jun 2017, 07:33 last edited by@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++; }
wrote on 5 Jun 2017, 08:05 last edited by@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));
wrote on 5 Jun 2017, 08:18 last edited by -
@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));
wrote on 5 Jun 2017, 09:11 last edited byHi,
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++; }
wrote on 6 Jun 2017, 02:50 last edited by@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.
wrote on 6 Jun 2017, 03:56 last edited byHi,
have you declared readLine() in public slots section?//In Header File
public slots:
void readLine();
4/13