Unsolved Adding combobox current text to a text file
-
Hi,
I have the following code to add the text the user typed into the combobox://Adding Material Combobox Material_Label->setText ("Material: "); Material_Label->setFont (g); Material_Combo->setMaxVisibleItems (8); Material_Combo->setEditable (true); material = Material_Combo->currentText (); Material_Combo->addItem (material); Material_Combo->setMaximumWidth (200); Material_Combo->setStyleSheet ("QComboBox {" "border-radius: 3px;" "background-color:rgb(202, 255, 227);" "padding: 1px 18px 1px 3px;" "font: 16px ;" "border-radius: 10px;" "border: 1px solid gray;}" ); connect(Material_Combo,SIGNAL(currentIndexChanged(int)),this,SLOT(processMaterial())); 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); } } void Additem::processMaterial() { qDebug()<< "Material SIGNAL works!"; material = Material_Combo->currentText (); qDebug() << "Material: " << material; QFile file("C:/Programming/Projects/Qcombobox2/combo.txt"); if(!file.open (QIODevice::ReadOnly | QIODevice::Text)) return; while(!file.atEnd ()) { QByteArray line = file.readLine (); //process_line(line); if(material == line) { qDebug() <<"We are not adding anything to the file!"; file.close (); return; } else { Material_Combo->addItem (material); QFile add_to_file("C:/Programming/Projects/Folkfriends/combo.txt"); if(add_to_file.open (QIODevice::WriteOnly | QIODevice::Text |QIODevice::Append)) { QTextStream stream(&add_to_file); stream << material << "\n"; stream.flush (); add_to_file.close (); } } } }
It does add the item but after a blank line and 5 times. So the entry looks like this:
Wood
Metal
Metal
Metal
Metal
MetalPlease help me to figure out what to do to add it only once without the blank line.
Thank you. -
The current index changes when adding an item. I think that process Material slot procedure is reentrant when using add item in it. Probably the problem issue.