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. ASSERT failure in QList<T>::operator[]: "index out of range"
Qt 6.11 is out! See what's new in the release blog

ASSERT failure in QList<T>::operator[]: "index out of range"

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 477 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.
  • D Offline
    D Offline
    deleted286
    wrote on last edited by
    #1

    Read data and appear on the screen every 20 seconds. When i run I have index out of range error.
    What my mistake?

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QTextStream>
    #include <QMessageBox>
    #include <QTimer>
    
    
    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);
    
              allLines.append(line); //listeye ekliyoruz
    
    
          }
    
    
          file.close();  }
    
      //  file.close();
    
    }
    
    JonBJ A 2 Replies Last reply
    0
    • D deleted286

      Read data and appear on the screen every 20 seconds. When i run I have index out of range error.
      What my mistake?

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QFile>
      #include <QTextStream>
      #include <QMessageBox>
      #include <QTimer>
      
      
      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);
      
                allLines.append(line); //listeye ekliyoruz
      
      
            }
      
      
            file.close();  }
      
        //  file.close();
      
      }
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @suslucoder
      Your mistake is that you have an index out of range, as the message tells you!

      And since the only place you use a QList with an index is:

      QString line = allLines[curIndex];
      

      So why don't you debug or print out curIndex on the line above that? At which point, my guess is you discover first time it's an uninitialized member variable...?

      1 Reply Last reply
      3
      • D deleted286

        Read data and appear on the screen every 20 seconds. When i run I have index out of range error.
        What my mistake?

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QFile>
        #include <QTextStream>
        #include <QMessageBox>
        #include <QTimer>
        
        
        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);
        
                  allLines.append(line); //listeye ekliyoruz
        
        
              }
        
        
              file.close();  }
        
          //  file.close();
        
        }
        
        A Offline
        A Offline
        Anonymous_Banned275
        wrote on last edited by Anonymous_Banned275
        #3

        @suslucoder IT may help if you post the actual error message - especially its location .
        Another suggestion - when dealing with data - perhaps adding code to tell when the condition is not true may also help.

        if( data)
        process data.....
        else
        no data to process

        1 Reply Last reply
        0

        • Login

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