Edit line in .ini file from Qt Creator[SOLVED]



  • I am trying to edit a line in .ini file from:
    @[values]
    1=8.56
    2=15
    3=10@

    to
    @[values]
    1=8.56
    2=40
    3=10@

    the code I have is:
    @QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
    settings.beginGroup("values");
    const QStringList childKeys = settings.childKeys();
    QHash<QString, QString> values;
    foreach (const QString &childKey, childKeys) {
    values.insert(childKey, settings.value(childKey).toString());
    if (childKey.toInt() == ui->comboBox->currentIndex()+1) {
    ui->calnumber->setText(settings.value(childKey).toString());

              QFile file&#40;"/home/test/Documents/Wave/signalgenerator.ini"&#41;;
              file.open(QIODevice::ReadWrite| QIODevice::Text&#41;;
    
              QTextStream write(&file);
              while (!write.atEnd()){
                  QString line = write.readLine();
                  if(line.contains(ui->comboBox->currentIndex())) {
                      QIODevice * dev = write.device();
                                      QIODevice::Offset pos = dev->at();
                                      dev->at(pos - line.length() - 1);
                                      write <<childKey.toString()<<"40";
                  }
    
                  break;
          }
    
          file.close();
       }
    
     }
     settings.endGroup();@
    

    I feel like I am wayy off. Any help would be appreciated.


  • Moderators

    Yes, you are way off.

    You know that if you already know the name of the item in the settings, you can just access it, right? You don't have to manually edit the file. You don't have to iterate through the file. Ever.

    In your case, you'd just use:
    @
    QSettings settings("/home/test/Documents/Wave/signalgenerator.ini",
    QSettings::IniFormat);
    settings.setValue("values/2",40);
    @

    Please read "this":http://doc.qt.nokia.com/4.7-snapshot/qsettings.html document.



  • [quote author="mlong" date="1314380468"]Yes, you are way off.

    You know that if you already know the name of the item in the settings, you can just access it, right? You don't have to manually edit the file. You don't have to iterate through the file. Ever.

    In your case, you'd just use:
    @
    QSettings settings("/home/test/Documents/Wave/signalgenerator.ini",
    QSettings::IniFormat);
    settings.setValue("values/2",40);
    @

    Please read "this":http://doc.qt.nokia.com/4.7-snapshot/qsettings.html document.

    [/quote]
    Wow, thanks alot. that saved me a whole lot of trouble.


  • Moderators

    No problem! Remember, the docs are your friend!

    Just for completeness sake, you can also get the value by using
    @
    QSettings settings("/home/test/Documents/Wave/signalgenerator.ini",
    QSettings::IniFormat);
    double value = settings.value("values/2",somedefaultvalue).toDouble()
    @
    (The default value is a good idea, in case the settings file doesn't exist or the key can't be read.)

    Again, you typically don't need to iterate over the file.



  • Thanks again mlong.
    I am now having problems adding code to my .ini file in the place I want it to be added. I am trying to change it from:
    @[values]
    1=8.56
    2=10

    [def]
    def=1@

    to this:
    @[values]
    1=8.56
    2=10
    3=10

    [def]
    def=1@

    but instead, I'm getting this:
    @[values]
    1=8.56
    2=10

    [def]
    def=1

    3=10@

    I want the line 3=10 to be placed under [values], not under [def]. What am I doing wrong?

    The code I have is below. The code takes the count of values in my combo box : @ui->comboBox->count();@ and increases it by 1 (called "i" in the code. Then takes in a value written in a lineEdit called "newCalNumberAdd" and writes the line to the .ini file: @write<<"\n"<<i<<"="<<ui->newCalNumberAdd->text();@

    @void wave::on_addboardButton_clicked()
    {
    QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
    settings.beginGroup("values");
    const QStringList childKeys = settings.childKeys();
    QHash<QString, QString> values;
    foreach (const QString &childKey, childKeys) {
    values.insert(childKey, settings.value(childKey).toString());
    if (childKey.toInt() == ui->comboBox->currentIndex()+1) {
    ui->calnumber->setText(settings.value(childKey).toString());

             QFile file&#40;"/home/test/Documents/Wave/signalgenerator.ini"&#41;;
             file.open(QIODevice::ReadWrite| QIODevice::Text);
    
             QTextStream write(&file);
             QString line = write.readLine();
    
             while (!line.isNull()){
                 int i = ui->comboBox->count();
                 i += childKey.toInt();
                 write<<"\n"<<i<<"="<<ui->newCalNumberAdd->text();
                 ui->comboBox->addItem("Board"+QString::number(i));
                 break;
         }
    
          file.close();
      }
    
    }
    settings.endGroup();
    

    }@


  • Moderators

    Why are you trying to use QFile to write your .ini file? Please explain your reasoning.



  • Either use QFile or (probably better choice) QSettings. Mixing them is showing that you are confused about how to use either of them.


  • Moderators

    [quote author="Andre" date="1314647554"]Either use QFile or (probably better choice) QSettings. Mixing them is showing that you are confused about how to use either of them.[/quote]

    That was already mentioned above. :-)



  • Yes, thanks guys. Thanks mlong. I used QSettings.
    Solution:
    @void wave::on_addboardButton_clicked()
    {

            QMap<QString,double> map;
            QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat );
            settings.beginGroup( "values" );
            foreach( QString childKey, settings.childKeys() ){
                 if (childKey.toInt() == ui->comboBox->currentIndex()+1) {
                    int i = ui->comboBox->count();
                    i += childKey.toInt();
                    map[ QString::number(i) ] = ui->newCalNumberAdd->text().toDouble();
                    ui->comboBox->addItem("Board"+QString::number(i));
                }
    
    
                QMapIterator<QString,double> it(map);
                while( it.hasNext() ){
                    it.next();
                    settings.setValue( it.key(), it.value() );
                }
    
    }
    settings.endGroup();
    

    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.