Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Windows not responding on reading file from txt
QtWS25 Last Chance

Windows not responding on reading file from txt

Scheduled Pinned Locked Moved Solved General and Desktop
file readqt creatorqfile
21 Posts 4 Posters 4.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mrjj
    2 Jan 2021, 15:02

    @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);
    
    
    
    
    
    
    D Offline
    D Offline
    deleted286
    wrote on 2 Jan 2021, 16:31 last edited by
    #9

    @mrjj thank you so much

    M 1 Reply Last reply 2 Jan 2021, 16:38
    0
    • D deleted286
      2 Jan 2021, 16:31

      @mrjj thank you so much

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 2 Jan 2021, 16:38 last edited by
      #10

      @suslucoder
      Np, i hope its not to much cheating if its an assignment :)

      D 1 Reply Last reply 2 Jan 2021, 16:43
      0
      • M mrjj
        2 Jan 2021, 16:38

        @suslucoder
        Np, i hope its not to much cheating if its an assignment :)

        D Offline
        D Offline
        deleted286
        wrote on 2 Jan 2021, 16:43 last edited by
        #11

        @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.

        M 1 Reply Last reply 2 Jan 2021, 16:46
        0
        • D deleted286
          2 Jan 2021, 16:43

          @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.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 2 Jan 2021, 16:46 last edited by
          #12

          @suslucoder
          Oh all good then. :)
          Thats a good idea as one mostly get better at coding, by coding.

          D 1 Reply Last reply 2 Jan 2021, 16:55
          0
          • M mrjj
            2 Jan 2021, 16:46

            @suslucoder
            Oh all good then. :)
            Thats a good idea as one mostly get better at coding, by coding.

            D Offline
            D Offline
            deleted286
            wrote on 2 Jan 2021, 16:55 last edited by
            #13

            @mrjj I didnt understand the idea behind --> connect(timer, &QTimer::timeout, this

            Cant we do this using signal and slot?

            There is an error like expected body of lambda expression

            D 1 Reply Last reply 2 Jan 2021, 19:13
            0
            • D deleted286
              2 Jan 2021, 16:55

              @mrjj I didnt understand the idea behind --> connect(timer, &QTimer::timeout, this

              Cant we do this using signal and slot?

              There is an error like expected body of lambda expression

              D Offline
              D Offline
              deleted286
              wrote on 2 Jan 2021, 19:13 last edited by
              #14

              @mrjj Hey, i got ASSERT failure in QList<T>::operator[]: "index out of range" ERROR. I cannot understand why. Can you help me?

              M 1 Reply Last reply 2 Jan 2021, 19:17
              0
              • D deleted286
                2 Jan 2021, 19:13

                @mrjj Hey, i got ASSERT failure in QList<T>::operator[]: "index out of range" ERROR. I cannot understand why. Can you help me?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 2 Jan 2021, 19:17 last edited by mrjj 1 Feb 2021, 19:18
                #15

                @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.

                D 1 Reply Last reply 2 Jan 2021, 19:31
                0
                • M mrjj
                  2 Jan 2021, 19:17

                  @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.

                  D Offline
                  D Offline
                  deleted286
                  wrote on 2 Jan 2021, 19:31 last edited by
                  #16

                  @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();
                  
                  M 1 Reply Last reply 2 Jan 2021, 19:42
                  0
                  • D deleted286
                    2 Jan 2021, 19:31

                    @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();
                    
                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 2 Jan 2021, 19:42 last edited by mrjj 1 Feb 2021, 19:44
                    #17

                    Hi
                    Well

                    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()));
                    

                    /// 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

                    D 1 Reply Last reply 2 Jan 2021, 20:23
                    0
                    • M mrjj
                      2 Jan 2021, 19:42

                      Hi
                      Well

                      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()));
                      

                      /// 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

                      D Offline
                      D Offline
                      deleted286
                      wrote on 2 Jan 2021, 20:23 last edited by
                      #18

                      @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 :(

                      M 1 Reply Last reply 2 Jan 2021, 22:17
                      0
                      • D deleted286
                        2 Jan 2021, 20:23

                        @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 :(

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 2 Jan 2021, 22:17 last edited by mrjj 1 Feb 2021, 22:18
                        #19

                        @suslucoder
                        Ok
                        You want it like

                        MainWindow::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
                        }
                        
                        
                        D 1 Reply Last reply 4 Jan 2021, 06:36
                        1
                        • M mrjj
                          2 Jan 2021, 22:17

                          @suslucoder
                          Ok
                          You want it like

                          MainWindow::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
                          }
                          
                          
                          D Offline
                          D Offline
                          deleted286
                          wrote on 4 Jan 2021, 06:36 last edited by
                          #20

                          @mrjj It works. Thank you for your help.

                          P 1 Reply Last reply 4 Jan 2021, 11:31
                          0
                          • D deleted286
                            4 Jan 2021, 06:36

                            @mrjj It works. Thank you for your help.

                            P Offline
                            P Offline
                            Pablo J. Rogina
                            wrote on 4 Jan 2021, 11:31 last edited by
                            #21

                            @suslucoder said in Windows not responding on reading file from txt:

                            It works

                            so please don't forget to mark your post as solved!

                            Upvote the answer(s) that helped you solve the issue
                            Use "Topic Tools" button to mark your post as Solved
                            Add screenshots via postimage.org
                            Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                            1 Reply Last reply
                            1

                            18/21

                            2 Jan 2021, 20:23

                            • Login

                            • Login or register to search.
                            18 out of 21
                            • First post
                              18/21
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved