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.
  • U Uberlinc

    @jsulm

    The data is read in from a file and it separated by commas.

    If I reference strTmp[0], it correctly returns the first element of the the QString value linest.

    The example below shows a shorter version that uses the exact same methodology and it works.
    I can reference arrstuff[0] - arrstuff[4] happily without error.

    In the second example (the one that works) instead of using individual variables to hold the data (e.g. one1, two2, etc) I simply place them in an array of int.

    It has no problem with this.

    I only used QRegExp because I found this in my code that works, so added it to my main code to see if it makes a difference.
    It didn't.

    Can you see the flaw in the top piece of code?

    Thanks.

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

    @Uberlinc said in QString Split() issue.:

    Can you see the flaw in the top piece of code?

    No.
    You did not tell what EXACT input string you're splitting?
    Why don't you check that? It probably does not contain any ','

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

    U 1 Reply Last reply
    1
    • jsulmJ jsulm

      @Uberlinc said in QString Split() issue.:

      Can you see the flaw in the top piece of code?

      No.
      You did not tell what EXACT input string you're splitting?
      Why don't you check that? It probably does not contain any ','

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

      @jsulm

      Here is the file that I'm reading in from at its entirety:

      20210101,23,27,44,1,41,18
      20210107,20,44,20,31,13,34
      20210114,23,24,36,28,5,8
      20210121,23,33,44,8,38,22
      20210128,18,6,16,30,17,34
      20210204,29,8,20,3,4,12
      20210211,11,28,37,38,5,9
      20210218,34,4,32,25,40,3
      20210225,26,34,25,31,38,5
      20210304,31,3,41,28,12,2
      20210311,37,39,41,28,26,19
      20210318,2,11,38,43,38,23
      20210325,45,7,8,37,14,12

      1 Reply Last reply
      0
      • 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 Online
          JonBJ Online
          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