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. [SOLVED] saving a variable to state
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] saving a variable to state

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 5.3k 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.
  • EddyE Offline
    EddyE Offline
    Eddy
    wrote on last edited by
    #3

    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

    Qt Certified Specialist
    www.edalsolutions.be

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kalster
      wrote on last edited by
      #4

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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #5

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #6

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

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #7

            [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);
            @

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kalster
              wrote on last edited by
              #8

              changing the code to int did not work.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #9

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

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kalster
                  wrote on last edited by
                  #10

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

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #11

                    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?

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kalster
                      wrote on last edited by
                      #12

                      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

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kalster
                        wrote on last edited by
                        #13

                        [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

                        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