Results 1 to 4 of 4

Thread: having problems adding text to .ini file in the correct place

  1. #1
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default having problems adding text to .ini file in the correct place

    I am having problems saving a line to my .ini file but it is not being sve din the place I want it to. I am trying to change it from:
    Qt Code:
    1. [values]
    2. 1=8.56
    3. 2=10
    4.  
    5. [def]
    6. def=1
    To copy to clipboard, switch view to plain text mode 

    to this:
    Qt Code:
    1. [values]
    2. 1=8.56
    3. 2=10
    4. 3=10
    5.  
    6. [def]
    7. def=1
    To copy to clipboard, switch view to plain text mode 

    but instead, I'm getting this:
    Qt Code:
    1. [values]
    2. 1=8.56
    3. 2=10
    4.  
    5. [def]
    6. def=1
    7.  
    8. 3=10
    To copy to clipboard, switch view to plain text mode 

    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 :
    Qt Code:
    1. ui->comboBox->count();
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. write<<"\n"<<i<<"="<<ui->newCalNumberAdd->text();
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. void wave::on_addboardButton_clicked()
    2. {
    3. QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
    4. settings.beginGroup("values");
    5. const QStringList childKeys = settings.childKeys();
    6. QHash<QString, QString> values;
    7. foreach (const QString &childKey, childKeys) {
    8. values.insert(childKey, settings.value(childKey).toString());
    9. if (childKey.toInt() == ui->comboBox->currentIndex()+1) {
    10. ui->calnumber->setText(settings.value(childKey).toString());
    11.  
    12. QFile file("/home/test/Documents/Wave/signalgenerator.ini");
    13. file.open(QIODevice::ReadWrite| QIODevice::Text);
    14.  
    15. QTextStream write(&file);
    16. QString line = write.readLine();
    17.  
    18. while (!line.isNull()){
    19. int i = ui->comboBox->count();
    20. i += childKey.toInt();
    21. write<<"\n"<<i<<"="<<ui->newCalNumberAdd->text();
    22. ui->comboBox->addItem("Board"+QString::number(i));
    23. break;
    24. }
    25.  
    26. file.close();
    27. }
    28.  
    29. }
    30. settings.endGroup();
    31. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by duma; 29th August 2011 at 18:11.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: having problems adding text to .ini file in the correct place

    .ini files are not meant or designed to be "written" in. QSettings is a class designed to save data in .ini files.

  3. #3
    Join Date
    Aug 2011
    Posts
    35
    Thanks
    5

    Default Re: having problems adding text to .ini file in the correct place

    Quote Originally Posted by mvuori View Post
    .ini files are not meant or designed to be "written" in. QSettings is a class designed to save data in .ini files.
    Yes, that is what I meant though. I want to save this line in the .ini file. But it is not being placed where I want. i am using QSettings

  4. #4
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: having problems adding text to .ini file in the correct place

    The problem is you open INI file in ReadWrite mode and write to it! this appends data to the end of file, you should seek to appropriated position and then write to it!

    BUT you don't need that way, just use QSettings for writing data to ini file:
    Qt Code:
    1. void wave::on_addboardButton_clicked()
    2. {
    3. QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
    4. settings.beginGroup("values");
    5. const QStringList childKeys = settings.childKeys();
    6. QHash<QString, QString> values;
    7. foreach (const QString &childKey, childKeys) {
    8. values.insert(childKey, settings.value(childKey).toString());
    9.  
    10. if (childKey.toInt() == ui->comboBox->currentIndex()+1) {
    11. ui->calnumber->setText(settings.value(childKey).toString());
    12.  
    13. int i = ui->comboBox->count()+childKey.toInt();
    14. settings.setValue(QString::number(i), ui->newCalNumberAdd->text());
    15. ui->comboBox->addItem("Board"+QString::number(i));
    16. }
    17. }
    18.  
    19. settings.endGroup();
    20. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 23rd May 2011, 14:36
  2. Problems saving a text file
    By aarelovich in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2009, 14:39
  3. Replies: 12
    Last Post: 30th May 2009, 14:29
  4. Replies: 3
    Last Post: 21st April 2009, 05:25
  5. How correct to place widgets in QMdiSubWindow
    By sawerset in forum Qt Programming
    Replies: 0
    Last Post: 15th April 2009, 23:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.