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. QTextStream crashing program when adding items to combobox
QtWS25 Last Chance

QTextStream crashing program when adding items to combobox

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtextstream
8 Posts 3 Posters 2.3k 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.
  • G Offline
    G Offline
    gabor53
    wrote on 6 Feb 2016, 17:32 last edited by
    #1

    Hi,
    I use QTextStream to populate a combobox:

            QFile inputFile("C:/Programming/Projects/Folkfriends/combo.txt");
    
            if(inputFile.open (QIODevice::ReadOnly | QIODevice::Text))
            {
                QTextStream in(&inputFile);
                while(!in.atEnd ())
                {
                    QString Material = in.readLine ();
                    Material_Combo->addItem (Material)	;
                }
            }
    

    Any idea why the while loop crashes the program? (It gives a message like "Stopped working)?

    Thank you.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      michelson
      wrote on 6 Feb 2016, 20:23 last edited by
      #2

      For me it seems valid, but if i were you I would try peeking what exactly in.readLine() is returning every time (mayby the file is corrupted?).

      QDebug() << "Read string: " <<  Material;
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 6 Feb 2016, 20:47 last edited by
        #3

        Hi,

        Maybe a silly question but, did you initialize Material_Combo correctly ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gabor53
          wrote on 7 Feb 2016, 04:14 last edited by
          #4

          Hi,
          I initialized like this:

          QComboBox *Material_Combo = new QComboBox;
          

          I checked what in.readLine() returns and found this:

          Read Matarial string: "Wood"
          Read Matarial string: "Metal"
          Read Matarial string: ""
          Read Matarial string: ""
          Read Matarial string: ""
          Read Matarial string: ""
          Read Matarial string: ""
          Read Matarial string: ""
          Read Matarial string: ""
          Read Matarial string: ""

          The empty strings are keep going until I terminate the program. There are 2 lines in the txt file, Wood and Metal. I think it can't find the end of the file maybe, but I don't know why. Thank you for any ideas.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gabor53
            wrote on 7 Feb 2016, 04:28 last edited by
            #5

            I think it is the while loop. I added this section to the program line by line and the only time, when the program crashed is when I added the while loop. Still don't know though why it crashes.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              gabor53
              wrote on 7 Feb 2016, 04:54 last edited by
              #6

              If I take away the ! sign and just use

              while(in.atEnd())
              

              the program does not crash though the loop reads nothing. Still don't kn ow why....

              1 Reply Last reply
              0
              • M Offline
                M Offline
                michelson
                wrote on 7 Feb 2016, 10:19 last edited by
                #7

                Mayby try this one:

                QComboBox *Material_Combo = new QComboBox(this);
                do
                {
                    QString line = in.readLine();
                    QDebug() << "line: " << line;
                    Material_Combo->addItem(line);
                } while (!line.isNull());
                

                It is the same but diffrent... Still the same :D I mean i dont know why it crashes when you use atEnd()however it seems that the file is corrupted since the end of file is not detected (file encoding thing possible..?).

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 7 Feb 2016, 21:22 last edited by
                  #8

                  Or, since it's a text file:

                  QFile inputFile("C:/Programming/Projects/Folkfriends/combo.txt");
                  if (!inputFile.open(QIODevice::ReadOnly | QIODevice::Text))
                       return;
                  
                  while (!inputFile.atEnd()) {
                      QByteArray material = inputFile.readLine();
                      Material_Combo->addItem (material)  ;
                  }
                  

                  ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  8/8

                  7 Feb 2016, 21:22

                  • Login

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