Unsolved Read line by line in a specific time interval
-
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! -
//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
-
@Venkatesh-V , thank you. will update to you once I've run the program
-
@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.
-
@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();