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. Edit line in .ini file from Qt Creator[SOLVED]
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 8.4k Views 1 Watching
  • 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.
  • O Offline
    O Offline
    ogopa
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        ogopa
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          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.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            ogopa
            wrote on last edited by
            #5

            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();
            

            }@

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

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

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  [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. :-)

                  Software Engineer
                  My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                  1 Reply Last reply
                  0
                  • O Offline
                    O Offline
                    ogopa
                    wrote on last edited by
                    #9

                    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();
                    

                    }@

                    1 Reply Last reply
                    0

                    • Login

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