QTextStream crashing program when adding items to combobox
-
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.
-
Hi,
Maybe a silly question but, did you initialize Material_Combo correctly ?
-
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.
-
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..?). -
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) ; }
?