Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] Problem with QSetting



  • Hello everybody;
    I have a problem with QSetting in read mode.

    This is my code for read from ini file:

    @

    QSettings obSettings("file.ini", QSettings::IniFormat);
    obSettings.beginGroup("General");
    QString address = obSettings.value("Serever","").toString();
    obSettings.endGroup();

    qDebug() << "Server address = " << address;
    @

    But result is empty !!!

    Result : Server address = ""

    Someine can help me for solve this problem ??? :)



  • Hello,

    1. is your file.ini empty?

    2. the method
      obSettings.value( key, defaultValue ).toString();

    if the key doesn't exists it returns your provided default, in this case an empty string.. (maybe a type in your group or key name?)



  • Did you misspell "server" ?


  • Lifetime Qt Champion

    Hi,

    Except the possible typo with Serever, did you save it first once ?



  • this is my ini file content :

    [General]
    Server=192.168.1.1



  • did you check the typo in

    QString address = obSettings.value(" Serever ","").toString();



  • Yes I have a mistake in mytype server ... serever no server.

    but does not work again :(



  • This is my code see again please :

    @
    QSettings obSettings("database.ini", QSettings::IniFormat);
    obSettings.beginGroup("General");
    QString server = obSettings.value("Server", "").toString();
    obSettings.endGroup();

    qDebug() << "Server = " << server;
    @


  • Lifetime Qt Champion

    Where is database.ini located ?



  • [quote author="SGaist" date="1398287969"]Where is database.ini located ?[/quote]

    No specific path !

    I can write into this ini file with below code :

    QSettings settings("database.ini", QSettings::IniFormat);
    settings.setValue("Server", "192.168.1.1");

    but for read.... I don't know why doesn't work.


  • Lifetime Qt Champion

    Just a wild idea:

    try with foo in place of General for the group name



  • [quote author="SGaist" date="1398288372"]Just a wild idea:

    try with foo in place of General for the group name [/quote]

    Can you show me with example ? :)


  • Lifetime Qt Champion

    Before that, just saw something:

    @
    QSettings settings(“database.ini”, QSettings::IniFormat); settings.setValue(“Server”, “192.168.1.1”);
    @

    You don't begin the group, is that a missing line ?



  • [quote author="SGaist" date="1398289288"]Before that, just saw something:

    @
    QSettings settings(“database.ini”, QSettings::IniFormat); settings.setValue(“Server”, “192.168.1.1”);
    @

    You don't begin the group, is that a missing line ?[/quote]

    but this is not for Read from ini file ! I don't have problem with setValue :)
    My main problem is reading from file :(

    You just show me how can I read from ini file , maybe I have a little mistake.
    Of course my ini file content is :

    [General]
    Server=192.168.1.1

    Why I can't read 192.168.1.1 from server!?


  • Lifetime Qt Champion

    The thing is: your writing to the init file and your reading must match.
    Using :

    @
    QSettings settings(“database.ini”, QSettings::IniFormat); settings.setValue(“Server”, “192.168.1.1”);
    @

    to write, then you must use

    @
    QSettings settings(“database.ini”, QSettings::IniFormat);
    QString server = settings.value(“Server”, “”).toString();
    @

    to read it back

    [edit: corrected my code]



  • No ! error... in QString server = settings.value(“Server”, “”);

    I changed it to below format with QVariant

    @
    QSettings settings("database.ini", QSettings::IniFormat);
    QVariant server = settings.value("Server", "");

    qDebug() << "Server = " << server.toString();
    @

    Result is :

    Server = "192.168.1.1"

    Good work :)
    Thank you very much. ;)


  • Lifetime Qt Champion

    You're welcome !

    Since you have your settings working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)



  • Yes of course !
    title is changed :)


Log in to reply