Windows not responding on reading file from txt
-
wrote on 2 Jan 2021, 13:35 last edited by
Hi everyone. I have 2 different txt file. One is for writing and the other one is reading. Writing part is working but when I clicked the read button windows not responding. Can somebody help me in that issue? Here is my code:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #include <QTextStream> #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_2_clicked() //write { QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt"); if(!file.open(QFile::WriteOnly | QFile::Text )) { QMessageBox::warning(this, "title", "file not open"); } QTextStream out (&file); QString text = ui->plainTextEdit->toPlainText(); out << text; file.flush(); file.close(); } void MainWindow::on_pushButton_clicked() //read { QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt"); if(!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, "title", "file cannot open"); } QTextStream stream(&file); QString line = stream.readLine(); while (!line.isNull()) { continue; } line = stream.readLine(); ui -> plainTextEdit->setPlainText(line); file.close(); }
-
wrote on 2 Jan 2021, 13:48 last edited by
@suslucoder said in Windows not responding on reading file from txt:
QString line = stream.readLine(); while (!line.isNull()) { continue; }
It seems to me you have an endless loop in there...
-
@suslucoder said in Windows not responding on reading file from txt:
QString line = stream.readLine(); while (!line.isNull()) { continue; }
It seems to me you have an endless loop in there...
wrote on 2 Jan 2021, 13:50 last edited by@JohanSolo actually i want to do this --> continue reading until the line is null. How can i fix it?
-
Lifetime Qt Championwrote on 2 Jan 2021, 13:50 last edited by mrjj 1 Feb 2021, 13:53
Hi
The reason it stops responding is this if sentenceQString line = stream.readLine(); while (!line.isNull()) { <<< since we just read a line above, it wont ever be be true and loop will loop forever continue; }
you want logic to look like
QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); while (!in.atEnd()) { QString line = in.readLine(); ... put line to plainTextEdit } inputFile.close(); }
-
Hi
The reason it stops responding is this if sentenceQString line = stream.readLine(); while (!line.isNull()) { <<< since we just read a line above, it wont ever be be true and loop will loop forever continue; }
you want logic to look like
QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); while (!in.atEnd()) { QString line = in.readLine(); ... put line to plainTextEdit } inputFile.close(); }
wrote on 2 Jan 2021, 14:02 last edited by@mrjj thank u. It works but it only reads the last line. I want to read every line and show the screen one by one
-
@mrjj thank u. It works but it only reads the last line. I want to read every line and show the screen one by one
Lifetime Qt Championwrote on 2 Jan 2021, 14:08 last edited by mrjj 1 Feb 2021, 14:10hi
it does read all of the files but if you do
ui -> plainTextEdit->setPlainText(line);
you then replace all text with the line you just read, over and over. so it only show the last line in the end.so you want to do
ui -> plainTextEdit->appendPlainText(line);
https://doc.qt.io/qt-5/qplaintextedit.html#appendPlainText
to add the lines as you read them.
ps. you dont have to read it line by line if all you want is to read it all and put in plainText.
https://doc.qt.io/qt-5/qtextstream.html#readAllQFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); QString all = in.readAll(); ui -> plainTextEdit->setPlainText(all); }
-
hi
it does read all of the files but if you do
ui -> plainTextEdit->setPlainText(line);
you then replace all text with the line you just read, over and over. so it only show the last line in the end.so you want to do
ui -> plainTextEdit->appendPlainText(line);
https://doc.qt.io/qt-5/qplaintextedit.html#appendPlainText
to add the lines as you read them.
ps. you dont have to read it line by line if all you want is to read it all and put in plainText.
https://doc.qt.io/qt-5/qtextstream.html#readAllQFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); QString all = in.readAll(); ui -> plainTextEdit->setPlainText(all); }
wrote on 2 Jan 2021, 14:20 last edited by@mrjj I got it. Thank you so much it works. If I add a timer to code, cann it writes one line to the screen every 20 seconds
-
@mrjj I got it. Thank you so much it works. If I add a timer to code, cann it writes one line to the screen every 20 seconds
Lifetime Qt Championwrote on 2 Jan 2021, 15:02 last edited by mrjj 1 Feb 2021, 15:03@suslucoder
Hi
Well not directly with that code as we loop until all is added.But you could do (pseudo code. didn't compile it)
QStringList allLines; // this goes in .h as a class member int curIndex = 0; // this goes in .h as a class member QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); while (!in.atEnd()) { QString line = in.readLine(); allLines.append(line); // add to list } inputFile.close(); } QTimer *timer = new timer(this); connect(timer, &QTimer::timeout, [this]() { QString line = allLines[curIndex]; // get a line from list ui -> plainTextEdit->setPlainText(line); // add the line if (curIndex < allLines.size() - 1 ) // if not at end of allLines curIndex++; // raise index }); timer->start(20000);
-
@suslucoder
Hi
Well not directly with that code as we loop until all is added.But you could do (pseudo code. didn't compile it)
QStringList allLines; // this goes in .h as a class member int curIndex = 0; // this goes in .h as a class member QFile inputFile(fileName); if (inputFile.open(QIODevice::ReadOnly)) { QTextStream in(&inputFile); while (!in.atEnd()) { QString line = in.readLine(); allLines.append(line); // add to list } inputFile.close(); } QTimer *timer = new timer(this); connect(timer, &QTimer::timeout, [this]() { QString line = allLines[curIndex]; // get a line from list ui -> plainTextEdit->setPlainText(line); // add the line if (curIndex < allLines.size() - 1 ) // if not at end of allLines curIndex++; // raise index }); timer->start(20000);
wrote on 2 Jan 2021, 16:31 last edited by@mrjj thank you so much
-
@mrjj thank you so much
@suslucoder
Np, i hope its not to much cheating if its an assignment :) -
@suslucoder
Np, i hope its not to much cheating if its an assignment :)wrote on 2 Jan 2021, 16:43 last edited by@mrjj hahah im newly graduated from university, I want to improve myself in Qt. That's why I think of different scenarios to challenge myself.
-
@mrjj hahah im newly graduated from university, I want to improve myself in Qt. That's why I think of different scenarios to challenge myself.
@suslucoder
Oh all good then. :)
Thats a good idea as one mostly get better at coding, by coding. -
@suslucoder
Oh all good then. :)
Thats a good idea as one mostly get better at coding, by coding.wrote on 2 Jan 2021, 16:55 last edited by -
wrote on 2 Jan 2021, 19:13 last edited by
@mrjj Hey, i got ASSERT failure in QList<T>::operator[]: "index out of range" ERROR. I cannot understand why. Can you help me?
-
@mrjj Hey, i got ASSERT failure in QList<T>::operator[]: "index out of range" ERROR. I cannot understand why. Can you help me?
Lifetime Qt Championwrote on 2 Jan 2021, 19:17 last edited by mrjj 1 Feb 2021, 19:18@suslucoder
hi
its means you try to access some index in a list and that index is too big.
I would guess on curIndex if it gets bigger than the allLines.size()
or if allLines is empty.
Did you load the file to the list before starting the timer ?its a lambda, yes, but you can also use a normal slot to do the same.
-
@suslucoder
hi
its means you try to access some index in a list and that index is too big.
I would guess on curIndex if it gets bigger than the allLines.size()
or if allLines is empty.
Did you load the file to the list before starting the timer ?its a lambda, yes, but you can also use a normal slot to do the same.
wrote on 2 Jan 2021, 19:31 last edited by@mrjj ``` I couldnt find my mistake :(
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #include <QTextStream> #include <QMessageBox> #include <QTimer> #include <QList> #include <qlist.h> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), SLOT(on_pushButton_clicked())); QString line = allLines[curIndex]; ui->plainTextEdit->setPlainText(line); if(curIndex < allLines.size() - 1 ) { curIndex++; } timer->start(20000); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_2_clicked() //write { QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt"); if(!file.open(QFile::WriteOnly | QFile::Text )) { QMessageBox::warning(this, "title", "file not open"); } QTextStream out (&file); QString text = ui->plainTextEdit->toPlainText(); out << text; file.flush(); file.close(); } void MainWindow::on_pushButton_clicked() //read { QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt"); // QMessageBox::warning(this, "title", "file not open"); // QTextStream in (&file); // while (!in.atEnd()) { // QString text = in.readLine(); // } //QString text = in.readLine(); if(file.open(QIODevice::ReadOnly)) { QTextStream in (&file); while (!in.atEnd()) { // QString line = in.readLine(); // ui -> plainTextEdit->setPlainText(all); QString line = in.readLine(); allLines.append(line); } } file.close(); } // file.close();
-
@mrjj ``` I couldnt find my mistake :(
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFile> #include <QTextStream> #include <QMessageBox> #include <QTimer> #include <QList> #include <qlist.h> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), SLOT(on_pushButton_clicked())); QString line = allLines[curIndex]; ui->plainTextEdit->setPlainText(line); if(curIndex < allLines.size() - 1 ) { curIndex++; } timer->start(20000); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_2_clicked() //write { QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt"); if(!file.open(QFile::WriteOnly | QFile::Text )) { QMessageBox::warning(this, "title", "file not open"); } QTextStream out (&file); QString text = ui->plainTextEdit->toPlainText(); out << text; file.flush(); file.close(); } void MainWindow::on_pushButton_clicked() //read { QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt"); // QMessageBox::warning(this, "title", "file not open"); // QTextStream in (&file); // while (!in.atEnd()) { // QString text = in.readLine(); // } //QString text = in.readLine(); if(file.open(QIODevice::ReadOnly)) { QTextStream in (&file); while (!in.atEnd()) { // QString line = in.readLine(); // ui -> plainTextEdit->setPlainText(all); QString line = in.readLine(); allLines.append(line); } } file.close(); } // file.close();
Lifetime Qt Championwrote on 2 Jan 2021, 19:42 last edited by mrjj 1 Feb 2021, 19:44Hi
WellMainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), SLOT(on_pushButton_clicked()));
/// all this should be in the slot -> on_pushButton_clicked if that how you load it
QString line = allLines[curIndex]; /// this will CRASH as you didnt fill list yet
ui->plainTextEdit->setPlainText(line);
if(curIndex < allLines.size() - 1 ) {
curIndex++;
}timer->start(20000); /// do not start timer before you have loaded into list..
So it was mostly the order of the code
-
Hi
WellMainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), SLOT(on_pushButton_clicked()));
/// all this should be in the slot -> on_pushButton_clicked if that how you load it
QString line = allLines[curIndex]; /// this will CRASH as you didnt fill list yet
ui->plainTextEdit->setPlainText(line);
if(curIndex < allLines.size() - 1 ) {
curIndex++;
}timer->start(20000); /// do not start timer before you have loaded into list..
So it was mostly the order of the code
wrote on 2 Jan 2021, 20:23 last edited by@mrjj im so confused about the order of my code. I tried all combinations that come to my mind, but when i clicked the read button my application was ended forcefully :(
-
@mrjj im so confused about the order of my code. I tried all combinations that come to my mind, but when i clicked the read button my application was ended forcefully :(
Lifetime Qt Championwrote on 2 Jan 2021, 22:17 last edited by mrjj 1 Feb 2021, 22:18@suslucoder
Ok
You want it likeMainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), SLOT(Timer_Slot())); // NOTE new slot for timer } // new slot for timer.. also put in .h under public slots: void MainWindow::Timer_Slot() { QString line = allLines[curIndex]; ui->plainTextEdit->setPlainText(line); if (curIndex < allLines.size() - 1 ) { curIndex++; } } void MainWindow::on_pushButton_clicked() //read { QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt"); if (file.open(QIODevice::ReadOnly)) { QTextStream in (&file); while (!in.atEnd()) { QString line = in.readLine(); allLines.append(line); } } file.close(); curIndex=0; // make sure we start from start timer->start(20000); // we have read the lines, start timer }
-
@suslucoder
Ok
You want it likeMainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), SLOT(Timer_Slot())); // NOTE new slot for timer } // new slot for timer.. also put in .h under public slots: void MainWindow::Timer_Slot() { QString line = allLines[curIndex]; ui->plainTextEdit->setPlainText(line); if (curIndex < allLines.size() - 1 ) { curIndex++; } } void MainWindow::on_pushButton_clicked() //read { QFile file("C:/Users/ilknu/Documents/QFileDemo/abc.txt"); if (file.open(QIODevice::ReadOnly)) { QTextStream in (&file); while (!in.atEnd()) { QString line = in.readLine(); allLines.append(line); } } file.close(); curIndex=0; // make sure we start from start timer->start(20000); // we have read the lines, start timer }
wrote on 4 Jan 2021, 06:36 last edited by@mrjj It works. Thank you for your help.
1/21