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. Adding combobox current text to a text file
Qt 6.11 is out! See what's new in the release blog

Adding combobox current text to a text file

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.2k 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 last edited by
    #1

    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
    Metal

    Please help me to figure out what to do to add it only once without the blank line.
    Thank you.

    1 Reply Last reply
    0
    • O Offline
      O Offline
      Olivier Ronat
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1

      • Login

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