Windows not responding on reading file from txt
-
@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
Np, i hope its not to much cheating if its an assignment :) -
@suslucoder
Oh all good then. :)
Thats a good idea as one mostly get better at coding, by coding. -
@mrjj Hey, i got ASSERT failure in QList<T>::operator[]: "index out of range" ERROR. I cannot understand why. Can you help me?
-
@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.
-
@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();
-
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
-
@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 said in Windows not responding on reading file from txt:
It works
so please don't forget to mark your post as solved!