[SOLVED] saving a variable to state



  • is it possible to store the state of a variable?



  • This is a weird question, right?
    Sure, you can,. write it to a file, to a QSettings, to a database, wherever you want.


  • Moderators

    Do you mean storing the value in a variable and when you stop/start your program, it still knows that value?

    In that case have a look at "QSettings":http://developer.qt.nokia.com/doc/qt-4.7/qsettings.html.
    There is also an "example":http://developer.qt.nokia.com/doc/qt-4.7/qsettings.html



  • [quote author="Gerolf" date="1312610870"]This is a weird question, right?
    Sure, you can,. write it to a file, to a QSettings, to a database, wherever you want.[/quote]

    no this is not a weird question because i am new here and i did not know it could be done :)



  • I read the docs and tried to code it and i cant get it to work. here is my code.

    @char tt =1;
    settings.setValue("variable", tt);@



  • [quote author="kalster" date="1312611661"][quote author="Gerolf" date="1312610870"]This is a weird question, right?
    Sure, you can,. write it to a file, to a QSettings, to a database, wherever you want.[/quote]

    no this is not a weird question because i am new here and i did not know it could be done :)[/quote]

    It is weird if you look at your last posts "Saving the result of a check box":http://developer.qt.nokia.com/forums/viewthread/8485/ What is the difference of a value of a widget to the value of a variable?



  • [quote author="kalster" date="1312614055"]I read the docs and tried to code it and i cant get it to work. here is my code.

    @
    char tt =1;
    settings.setValue("variable", tt);
    @
    [/quote]

    Ok, so your problem is to store a variable of type char which is used as byte (number) not as character. Here comes your problem:

    • setValue gets a QVariant. QVariant has some implicit constructors which can create the QVariant from bool, int, QString, QChar, ...
    • QChar can be constructed by a char, but it interprets the value as a character, and decimal 1 is not a valid printable character.

    That's why it does not work as expected.

    This should work:

    @
    int tt =1;
    settings.setValue("variable", tt);
    @



  • changing the code to int did not work.



  • What does not work?
    What did you see, what did you expect?
    How do you check, whether it works or not?



  • I have config.ini stored in the settings function. I was expecting to see variable from my settings.setValue stored in the config.ini file when i ran the code. I seen nothing. I therefore assumed the code was not working correctly.

    @QSettings settings("config.ini", QSettings::IniFormat);
    settings.setValue("variable", tt);@



  • I can confirm that the code you posted creates a config.ini in the working directory containing the value for your variable.

    Is your file not created or just empty?
    Do you have enough permissions to write to the directory / file?



  • I had two ini files being created when i should have had one. somewhere else in the code one ini file was created as myconfig.ini and the other as config.ini for the tt variable. I was looking in myconfig.ini when i should have been looking in config.ini.

    the code works. thank you



  • [quote author="Gerolf" date="1312617901"]Ok, so your problem is to store a variable of type char which is used as byte (number) not as character. Here comes your problem:

    • setValue gets a QVariant. QVariant has some implicit constructors which can create the QVariant from bool, int, QString, QChar, ...
    • QChar can be constructed by a char, but it interprets the value as a character, and decimal 1 is not a valid printable character.

    That's why it does not work as expected.[/quote]

    I don't understand what you are saying because the code works when i use char tt=1; it seems that i had the code correct right from the beginning. I just got mixed up with the two ini files that were created.

    nevertheless, the code works great. thank you Gerolf for your help


Log in to reply
 

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