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. "index out of range"
Qt 6.11 is out! See what's new in the release blog

"index out of range"

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.4k Views 1 Watching
  • 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.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    Hello everyone.
    First of all thanks a lot for helping me.

    I am trying to get information from a .txt file.
    I have to get the first four words of each line.

    This is my code:

        if(file.open(QFile::ReadOnly))
        {
            std::cout<<("1)");
    
            ui->textEdit->setPlainText(file.readAll()); //Mostramos el texto en un a ventana
            std::cout<<("2");
    
            /*************************
             pinta las líneas
            *************************/
                std::cout<<("Estoy en print_lines)")<< std::endl;
    
                QString texto = ui->textEdit->toPlainText();
    
                QStringList lines = texto.split("\n", QString::KeepEmptyParts); //Split the text in lines and save each line in Array called "lines"
                QStringList words;                                                                               //Por tanto ya tenemos todas las líneas en un array y podemos acceder a ellas como : linea1=myStringList(1);
    
    
                int num_lines = lines.size(); //number of lines that text has. (it always takes one line more that really it is)
    
                QString x_start[num_lines];
                QString y_start[num_lines];
                QString x_end[num_lines];
                QString y_end[num_lines];
    
                QString lines_split[num_lines];
    
    
                for (int i=0;i<num_lines-1;i++)
                {
                    lines_split[i] = lines.at(i);
                    words = lines_split[i].split(" ", QString::SkipEmptyParts); //Split each line in words
                    x_start[i] = words.at(0); //Take the first word
                    y_start[i] = words.at(1); //Take the second word
                    x_end[i] = words.at(2); //Take the third word
                    y_end[i] = words.at(3); //Take the fourth word
    
                    std::cout<<i << " " << x_end[i].toStdString()<< std::endl;
                }
    }
    

    }
    }

    It compiles fine but it drops an error that it is the next:

    ASSERT failure in QList<T>::at: "index out of range", file ../../../Qt5.6.0/5.6/gcc_64/include/QtCore/qlist.h, line 531
    The program has unexpectedly finished.

    And it enters in for just once...

    Could everybody help me?

    Thanks a lot.

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      You should debug your code to see what happens.

      One note: this line is wrong

      for (int i=0;i<num_lines-1;i++)
      // it should be
      for (int i=0;i<num_lines;i++)
      // else you skip last line
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      AlvaroSA 1 Reply Last reply
      0
      • Chris KawaC Online
        Chris KawaC Online
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        Hi, welcome to devnet.

        You split the original file contents using the QString::KeepEmptyParts option. This means that if there are any empty lines they will be stored as well. There's probably also a new line character at the end, that's why you think that it's one item more than there is. It's probably an empty item at the end.

        The above is the problem, because then you split each line and silently assume there are always at least 4 elements. If the line is empty the split will produce an empty list and referencing its 0,1,2,3 element is the out of range error.

        To correct the code use QString::SkipEmptyParts in your initial split of the file contents. This will make sure you've got no empty strings in the list.
        Then, after you split each line, make sure it actually has at least 4 elements and append the values to a list instead of using an index:

        QStringList x_start;
        QStringList y_start;
        QStringList x_end;
        QStringList y_end;
        
        for (int i = 0; i < num_lines; ++i) //go all the way, don't skip the last item
        {
           words = lines.at(i).split(" ", QString::SkipEmptyParts);
           if (words.size() >= 4)
           {
              x_start << words.at(0); //Take the first word
              y_start << words.at(1); //Take the second word
              x_end   << words.at(2); //Take the third word
              y_end   << words.at(3); //Take the fourth word
              std::cout << i << " " << x_end.last().toStdString() << std::endl;
           }
        }
        
        
        AlvaroSA 1 Reply Last reply
        1
        • Chris KawaC Chris Kawa

          Hi, welcome to devnet.

          You split the original file contents using the QString::KeepEmptyParts option. This means that if there are any empty lines they will be stored as well. There's probably also a new line character at the end, that's why you think that it's one item more than there is. It's probably an empty item at the end.

          The above is the problem, because then you split each line and silently assume there are always at least 4 elements. If the line is empty the split will produce an empty list and referencing its 0,1,2,3 element is the out of range error.

          To correct the code use QString::SkipEmptyParts in your initial split of the file contents. This will make sure you've got no empty strings in the list.
          Then, after you split each line, make sure it actually has at least 4 elements and append the values to a list instead of using an index:

          QStringList x_start;
          QStringList y_start;
          QStringList x_end;
          QStringList y_end;
          
          for (int i = 0; i < num_lines; ++i) //go all the way, don't skip the last item
          {
             words = lines.at(i).split(" ", QString::SkipEmptyParts);
             if (words.size() >= 4)
             {
                x_start << words.at(0); //Take the first word
                y_start << words.at(1); //Take the second word
                x_end   << words.at(2); //Take the third word
                y_end   << words.at(3); //Take the fourth word
                std::cout << i << " " << x_end.last().toStdString() << std::endl;
             }
          }
          
          
          AlvaroSA Offline
          AlvaroSA Offline
          AlvaroS
          wrote on last edited by
          #4

          @Chris-Kawa Thanks a lot for answering!
          now it works fine!!

          1 Reply Last reply
          0
          • jsulmJ jsulm

            You should debug your code to see what happens.

            One note: this line is wrong

            for (int i=0;i<num_lines-1;i++)
            // it should be
            for (int i=0;i<num_lines;i++)
            // else you skip last line
            
            AlvaroSA Offline
            AlvaroSA Offline
            AlvaroS
            wrote on last edited by
            #5

            @jsulm Yes my friend I want to skip last line because .size takes size of lines+1 but I do not know why...

            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