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. QString Split() issue.
Forum Updated to NodeBB v4.3 + New Features

QString Split() issue.

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 1.7k 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.
  • JonBJ JonB

    @Uberlinc

    tmp = line.split(",");
    qDebug() << line << tmp.length()  << tmp;
    
    U Offline
    U Offline
    Uberlinc
    wrote on last edited by
    #7

    @JonB said in QString Split() issue.:

    @Uberlinc

    tmp = line.split(",");
    qDebug() << line << tmp.length()  << tmp;
    
    22:05:08: Starting /home/linc/Documents/Workspace/Qt/build-SimpleFile-Desktop-Debug/SimpleFile...
    "20210107,20,44,20,31,13,34" 7 ("20210107", "20", "44", "20", "31", "13", "34")
    "20210114,23,24,36,28,5,8" 7 ("20210114", "23", "24", "36", "28", "5", "8")
    "20210121,23,33,44,8,38,22" 7 ("20210121", "23", "33", "44", "8", "38", "22")
    "20210128,18,6,16,30,17,34" 7 ("20210128", "18", "6", "16", "30", "17", "34")
    "20210204,29,8,20,3,4,12" 7 ("20210204", "29", "8", "20", "3", "4", "12")
    "20210211,11,28,37,38,5,9" 7 ("20210211", "11", "28", "37", "38", "5", "9")
    "20210218,34,4,32,25,40,3" 7 ("20210218", "34", "4", "32", "25", "40", "3")
    "20210225,26,34,25,31,38,5" 7 ("20210225", "26", "34", "25", "31", "38", "5")
    "20210304,31,3,41,28,12,2" 7 ("20210304", "31", "3", "41", "28", "12", "2")
    "20210311,37,39,41,28,26,19" 7 ("20210311", "37", "39", "41", "28", "26", "19")
    "20210318,2,11,38,43,38,23" 7 ("20210318", "2", "11", "38", "43", "38", "23")
    "20210325,45,7,8,37,14,12" 7 ("20210325", "45", "7", "8", "37", "14", "12")
    "" 1 ("")
    ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h, line 552
    
    JonBJ J.HilkJ 2 Replies Last reply
    0
    • U Uberlinc

      @JonB said in QString Split() issue.:

      @Uberlinc

      tmp = line.split(",");
      qDebug() << line << tmp.length()  << tmp;
      
      22:05:08: Starting /home/linc/Documents/Workspace/Qt/build-SimpleFile-Desktop-Debug/SimpleFile...
      "20210107,20,44,20,31,13,34" 7 ("20210107", "20", "44", "20", "31", "13", "34")
      "20210114,23,24,36,28,5,8" 7 ("20210114", "23", "24", "36", "28", "5", "8")
      "20210121,23,33,44,8,38,22" 7 ("20210121", "23", "33", "44", "8", "38", "22")
      "20210128,18,6,16,30,17,34" 7 ("20210128", "18", "6", "16", "30", "17", "34")
      "20210204,29,8,20,3,4,12" 7 ("20210204", "29", "8", "20", "3", "4", "12")
      "20210211,11,28,37,38,5,9" 7 ("20210211", "11", "28", "37", "38", "5", "9")
      "20210218,34,4,32,25,40,3" 7 ("20210218", "34", "4", "32", "25", "40", "3")
      "20210225,26,34,25,31,38,5" 7 ("20210225", "26", "34", "25", "31", "38", "5")
      "20210304,31,3,41,28,12,2" 7 ("20210304", "31", "3", "41", "28", "12", "2")
      "20210311,37,39,41,28,26,19" 7 ("20210311", "37", "39", "41", "28", "26", "19")
      "20210318,2,11,38,43,38,23" 7 ("20210318", "2", "11", "38", "43", "38", "23")
      "20210325,45,7,8,37,14,12" 7 ("20210325", "45", "7", "8", "37", "14", "12")
      "" 1 ("")
      ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h, line 552
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #8

      @Uberlinc So? How many elements in the list does the output show when it ASSERTS?

      1 Reply Last reply
      0
      • U Uberlinc

        @JonB said in QString Split() issue.:

        @Uberlinc

        tmp = line.split(",");
        qDebug() << line << tmp.length()  << tmp;
        
        22:05:08: Starting /home/linc/Documents/Workspace/Qt/build-SimpleFile-Desktop-Debug/SimpleFile...
        "20210107,20,44,20,31,13,34" 7 ("20210107", "20", "44", "20", "31", "13", "34")
        "20210114,23,24,36,28,5,8" 7 ("20210114", "23", "24", "36", "28", "5", "8")
        "20210121,23,33,44,8,38,22" 7 ("20210121", "23", "33", "44", "8", "38", "22")
        "20210128,18,6,16,30,17,34" 7 ("20210128", "18", "6", "16", "30", "17", "34")
        "20210204,29,8,20,3,4,12" 7 ("20210204", "29", "8", "20", "3", "4", "12")
        "20210211,11,28,37,38,5,9" 7 ("20210211", "11", "28", "37", "38", "5", "9")
        "20210218,34,4,32,25,40,3" 7 ("20210218", "34", "4", "32", "25", "40", "3")
        "20210225,26,34,25,31,38,5" 7 ("20210225", "26", "34", "25", "31", "38", "5")
        "20210304,31,3,41,28,12,2" 7 ("20210304", "31", "3", "41", "28", "12", "2")
        "20210311,37,39,41,28,26,19" 7 ("20210311", "37", "39", "41", "28", "26", "19")
        "20210318,2,11,38,43,38,23" 7 ("20210318", "2", "11", "38", "43", "38", "23")
        "20210325,45,7,8,37,14,12" 7 ("20210325", "45", "7", "8", "37", "14", "12")
        "" 1 ("")
        ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h, line 552
        
        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #9

        @Uberlinc said in QString Split() issue.:

        "" 1 ("")

        looks like you have an empty line in your file.
        I would suggest simply skipping, if your don't get the required amount of entries after the split

        strTmp = linest.split(",");
        
        if(strTmp.size() < 7){
            qDebug() << "Invalid linest:" << linest;
            return;
        }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        U 1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @Uberlinc said in QString Split() issue.:

          "" 1 ("")

          looks like you have an empty line in your file.
          I would suggest simply skipping, if your don't get the required amount of entries after the split

          strTmp = linest.split(",");
          
          if(strTmp.size() < 7){
              qDebug() << "Invalid linest:" << linest;
              return;
          }
          
          U Offline
          U Offline
          Uberlinc
          wrote on last edited by Uberlinc
          #10

          @J-Hilk said in QString Split() issue.:

          looks like you have an empty line in your file.
          I would suggest simply skipping, if your don't get the required amount of entries after the split

          I did see that, and so I checked the file.
          Yes, there was a blank line at the end of the file.
          So, I removed it.
          Definitely not there now.

          The same error occurred.

          Weird?

          Here is the code to read the file.
          Could the error be here instead?

          void MainWindow::readInfile(QString filename)
          {
              QFile file(filename);
              if(!file.open(QFile::ReadOnly |
                            QFile::Text))
              {
                  QMessageBox::about(this, "Error", "Could not open file for reading");
                  return;
              }
          
              QTextStream stream(&file);
              QString line = stream.readLine();
              while (!line.isNull())
              {
                  // process information
                  line = stream.readLine();
                  loadVec(line);
              }
          
          
              QMessageBox::about(this, "Done", "File contents read in!");
              file.close();
          }
          
          jsulmJ J.HilkJ 2 Replies Last reply
          0
          • U Uberlinc

            @J-Hilk said in QString Split() issue.:

            looks like you have an empty line in your file.
            I would suggest simply skipping, if your don't get the required amount of entries after the split

            I did see that, and so I checked the file.
            Yes, there was a blank line at the end of the file.
            So, I removed it.
            Definitely not there now.

            The same error occurred.

            Weird?

            Here is the code to read the file.
            Could the error be here instead?

            void MainWindow::readInfile(QString filename)
            {
                QFile file(filename);
                if(!file.open(QFile::ReadOnly |
                              QFile::Text))
                {
                    QMessageBox::about(this, "Error", "Could not open file for reading");
                    return;
                }
            
                QTextStream stream(&file);
                QString line = stream.readLine();
                while (!line.isNull())
                {
                    // process information
                    line = stream.readLine();
                    loadVec(line);
                }
            
            
                QMessageBox::about(this, "Done", "File contents read in!");
                file.close();
            }
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #11

            @Uberlinc said in QString Split() issue.:

            Could the error be here instead?

            Please first check the string you're splitting!
            This is first thing to do in such a situation...
            You also should ALWAYS check the indexes when handling data! Not just think it will be exactly what you expect.

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

            U 1 Reply Last reply
            0
            • goloviznin.kG Offline
              goloviznin.kG Offline
              goloviznin.k
              wrote on last edited by
              #12

              Hey, maybe this will help you somehow:

              tmp = line.split(",", QString::SkipEmptyParts);
              		for (int i = 0; i < tmp.count(); ++i) 
              		{
              			arrstuff[i] = tmp[i].toInt();
              			std::cout << arrstuff[i] << " ";
              		}
              
              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Uberlinc said in QString Split() issue.:

                Could the error be here instead?

                Please first check the string you're splitting!
                This is first thing to do in such a situation...
                You also should ALWAYS check the indexes when handling data! Not just think it will be exactly what you expect.

                U Offline
                U Offline
                Uberlinc
                wrote on last edited by
                #13

                @jsulm said in QString Split() issue.:

                Please first check the string you're splitting!
                This is first thing to do in such a situation...

                I did.
                Please see the qDebug output above.
                It definitely contains commas.
                I have removed any and all spaces/carriage returns at the end of the file.
                From what I can see, the lines are indeed being read in as per QDebug output above.

                Thanks.

                jsulmJ 1 Reply Last reply
                0
                • U Uberlinc

                  @J-Hilk said in QString Split() issue.:

                  looks like you have an empty line in your file.
                  I would suggest simply skipping, if your don't get the required amount of entries after the split

                  I did see that, and so I checked the file.
                  Yes, there was a blank line at the end of the file.
                  So, I removed it.
                  Definitely not there now.

                  The same error occurred.

                  Weird?

                  Here is the code to read the file.
                  Could the error be here instead?

                  void MainWindow::readInfile(QString filename)
                  {
                      QFile file(filename);
                      if(!file.open(QFile::ReadOnly |
                                    QFile::Text))
                      {
                          QMessageBox::about(this, "Error", "Could not open file for reading");
                          return;
                      }
                  
                      QTextStream stream(&file);
                      QString line = stream.readLine();
                      while (!line.isNull())
                      {
                          // process information
                          line = stream.readLine();
                          loadVec(line);
                      }
                  
                  
                      QMessageBox::about(this, "Done", "File contents read in!");
                      file.close();
                  }
                  
                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by J.Hilk
                  #14

                  @Uberlinc line.isNull() != line.isEmpty()

                  also you're reading the line twice and not checking against empty with the 2nd read...

                  QTextStream stream(&file);
                      while (!stream.atEnd())
                      {
                          // process information
                          QString line = stream.readLine();
                          if(!line.isEmpty())
                             loadVec(line);
                      }
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  2
                  • U Uberlinc

                    @jsulm said in QString Split() issue.:

                    Please first check the string you're splitting!
                    This is first thing to do in such a situation...

                    I did.
                    Please see the qDebug output above.
                    It definitely contains commas.
                    I have removed any and all spaces/carriage returns at the end of the file.
                    From what I can see, the lines are indeed being read in as per QDebug output above.

                    Thanks.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    @Uberlinc said in QString Split() issue.:

                    Please see the qDebug output above

                    Where?
                    You posted new code but no new debug output as far as I can see.
                    Please post the string you're trying to split. You expect it to have at least 5 commas, right?
                    Does the string in question contain 5 commas?

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

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      Uberlinc
                      wrote on last edited by
                      #16

                      I think I've sorted it now.
                      It was an error in the file read loop.

                      Thank you for your assistance.

                      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