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?
Forum Update on Monday, May 27th 2025

[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.
  • F Offline
    F Offline
    Franzk
    wrote on last edited by
    #2

    Store isChecked() in a setting. You can read it as a boolean:

    @settings.setValue("myCheckBox", myCheckBox->isChecked());

    myCheckBox->setChecked(settings.value("myCheckBox", false /* default value */).toBool());@

    If you use the tristate version you should use checkedState.

    "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #3

      QSettings uses QVariant and QVariant supports bool.

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

        Ok thanks a lot! It works!
        Should i change toString() to toBool() when importing the settings?

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #5

          If you expect a boolean, yes you should.

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

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

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

            You mind me asking why on earth you are storing the buttons text propery to QSettings?

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

              Andre i have a button that when you press it you select an audio file.. When you click ok the text of the button changes to the path of the audio file.. So when you close the app and reopen i need the button to have the path of the sound that was previously selected ! :)

              P.S Thanks Franzk

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

                That sounds like a weird interface to me... What does a button with a file name on it do? Play it? No, in your case, it allows to (presumably) select another one. Not very intuitive IMHO. Also, from a programming perspective, not the nicest separation on concerns. Why not store the file name name where it makes sense (some class variable or better yet, property) at runtime and store that to QSettings when you need to. From that, you can simply re-set the text for the button. If you ever decide you don't want that file name on the button after all, there is only one piece of code to change...

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

                  I Just think this is the right thing to do..! And it looks perfect at my app! So i will keep it like this.

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

                    What if the .conf file doesn't exist? ( First time running the program )
                    Even if the default check state of a checkbox is checked and the .conf doesn't exist
                    @ui->preview->setChecked(settings.value("preview", false).toBool());@
                    will make it unchecked...

                    So should i check for file existence?

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

                      If the file does not exist, you will have to provide a default anyways. So just adjust the default value of the value("name", default) method call. If you change it from false to true your checkbox is checked by default.

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

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

                        So you mean something like:
                        @ui->preview->setChecked(settings.value("preview", true).toBool());@
                        ? I am not sure...

                        1 Reply Last reply
                        0
                        • 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

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved