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] How to save checkbox/radiobutton states with QSettings?

[Solved] How to save checkbox/radiobutton states with QSettings?

Scheduled Pinned Locked Moved General and Desktop
22 Posts 5 Posters 17.0k Views
  • 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.
  • G Offline
    G Offline
    goetz
    wrote on last edited by
    #13

    Oh come on! Just try it out! That's quicker than writing a comment here and waiting for an answer!

    http://www.catb.org/~esr/faqs/smart-questions.html

    1 Reply Last reply
    0
    • L Offline
      L Offline
      Leon
      wrote on last edited by
      #14

      So for the checkboxes that are checked by default i should have true and for the ones that are not checked false? Right?

      EDIT: I checked before adding the comment

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #15

        [quote author="Leon" date="1309953483"]So for the checkboxes that are checked by default i should have true and for the ones that are not checked false? Right?

        EDIT: I checked before adding the comment[/quote]

        Sounds logical? no?

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • L Offline
          L Offline
          Leon
          wrote on last edited by
          #16

          It works for me so propably yes! :)
          Out of topic.. What about a slider?
          What if i want to have as default value 6?
          @ui->timerSlider->setValue(settings.value("timeSlider").toInt());@

          EDIT: If the file doesn't exist the value of the slider will be set to 1

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #17

            Then pass 6 as the default. Surprising?

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • L Offline
              L Offline
              Leon
              wrote on last edited by
              #18

              I edited my answer..
              If the file doesn’t exist the value of the slider will be set to 1 even if the default one is 6

              1 Reply Last reply
              0
              • L Offline
                L Offline
                Leon
                wrote on last edited by
                #19

                What i did is this:
                @QFile timeslide ( QDir::homePath()+"/.config/Program/MainWindow.conf" );
                if (timeslide.exists()) ui->timerSlider->setValue(settings.value("timeSlider").toInt());@

                If there is another way tell me!

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

                  Let's reformat that code a bit to make it more readable:

                  @
                  QFile timeslide ( QDir::homePath()+"/.config/Program/MainWindow.conf" );
                  if (timeslide.exists()) {
                  ui->timerSlider->setValue(settings.value("timeSlider").toInt());
                  }
                  @

                  That makes it obvious that line 3 is not executed if the file does not exist. Hence, the value of the slider will not change. If you want to set the slider to some value indepent of the existance of the file, then you need to make sure you execute that line in a code path that doesn't only execute if the file exists.

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    Leon
                    wrote on last edited by
                    #21

                    I am reformating it at my code not here at the forum!
                    No i just want for the first time the default value to be 6.. After that the file will exist so if the user change it eg to value 4 the next time it will be value 4..
                    Ok thanks again!

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #22

                      This works perfectly for me:

                      @
                      QSettings testSettings;
                      int sliderVal = testSettings.value("slider/value", 6).toInt();
                      qDebug() << "sliderVal=" << sliderVal;
                      ui->horizontalSlider->setValue(sliderVal);
                      @

                      Be sure NOT to call or trigger any slot connected to signal valueChanged() before you have set the default value.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      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