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 the result of a check box

[SOLVED] saving the result of a check box

Scheduled Pinned Locked Moved General and Desktop
16 Posts 6 Posters 8.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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    the qt library seems powerful. I am able to save the state and geometry of my window and dialogs. what is the best way to save the result of a check box? Can I save its state or do I need to save it to a file or some other method

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on last edited by
      #2

      Sure. You can save check-box state as
      @
      QSettings::setValue("checkstate", QCheckBox::isChecked())
      @

      and load it as

      @
      QCheckBox::setChecked(QSettings::value("checkstate").toBool())
      @

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

        I am getting two errors with your code above. error: cannot call member function 'bool QAbstractButton::isChecked() const' without object. error: cannot call member function 'QVariant QSettings::value(const QString&, const QVariant&) const' without object.

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

          Those are non-static functions. You need objects and initialize them.

          The code looks more like:
          @
          QSettings settings;
          QCheckBox cbox;
          .
          .
          .
          settings.setValue("checkstate", cbox.isChecked());
          @

          and load it as

          @
          .
          .
          .
          cboxsetChecked(settings.value("checkstate").toBool());
          .
          @

          However, you still need to initialize the "settings" and "cbox".

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cincirin
            wrote on last edited by
            #5

            This code was just an example. You can not use it in normal application (it could use if that functions were static).
            Here is an compilable example (not tested):
            @
            QSettings settings; // make sure you have initialized application & organization name first
            QCheckBox* checkBox; // your check box
            settings.setValue("checkstate", checkBox->isChecked(); // save check-box state
            checkBox->setChecked(settings.value("checkstate").toBool()) // load check-box state
            @

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

              QCheckBox* checkbox; is crashing my compiled program. do i need to put something in the header?

              [quote author="cincirin" date="1312528415"]make sure you have initialized application & organization name first[/quote]

              I don't understand that part of your quote.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cincirin
                wrote on last edited by
                #7

                Can you show us your UI file ?
                Also, see "QSettings basic usage":http://doc.qt.nokia.com/stable/qsettings.html#basic-usage

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

                  I fixed the code. now it does not give me an error but it still does not save the state of the check box

                  @#include "tipoftheday.h"
                  #include "ui_tipoftheday.h"
                  #include "mainwindow.h"
                  #include <QDesktopWidget>
                  #include <QSettings>
                  #include <QCheckBox>

                  TipOfTheDay::TipOfTheDay(QWidget *Child) :
                  QDialog(Child, Qt::CustomizeWindowHint |
                  Qt::WindowCloseButtonHint ),
                  ui(new Ui::TipOfTheDay)
                  {
                  ui->setupUi(this);
                  this->setFixedSize(width(), height());

                  QSettings settings;
                  QCheckBox checkbox;
                      checkbox.setChecked(settings.value("checkstate").toBool()); // load check-box state
                  

                  }

                  TipOfTheDay::~TipOfTheDay()
                  {
                  delete ui;
                  }

                  void TipOfTheDay::on_checkBox_clicked()
                  {
                  QSettings settings;
                  QCheckBox checkbox;
                  settings.setValue("checkstate", checkbox.isChecked()); // save check-box state
                  }@

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    cincirin
                    wrote on last edited by
                    #9

                    You need to init "checkbox" variable first. Therefore, show us your UI file, an we help you. We need the name of your combo box variable.
                    In pseudocode would look like this: QComboBox* comboBox = ui->yourComboBox

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

                      QComboBox* comboBox = ui->checkBox

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        cincirin
                        wrote on last edited by
                        #11

                        Then ...
                        @
                        QSettings settings; // make sure you have initialized application & organization name first
                        QCheckBox* checkBox = ui->checkBox; // your check box
                        settings.setValue("checkstate", checkBox->isChecked(); // save check-box state
                        checkBox->setChecked(settings.value("checkstate").toBool()) // load check-box state
                        @

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          dbzhang800
                          wrote on last edited by
                          #12

                          Please remove line 18 and line 31, which define another two top windows, although you cannot see them.

                          Change the name "checkbox" in line 19 and line 32 the the name of your QCheckBox which shown in your Widget.

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

                            Hi kalster,

                            youi have a C++ error here:

                            @
                            void TipOfTheDay::on_checkBox_clicked()
                            {
                            QSettings settings;
                            QCheckBox checkbox;
                            settings.setValue("checkstate", checkbox.isChecked()); // save check-box state
                            }
                            @

                            you create a local QCheckbox on the stack, which will never show up anywhere and try to store it's state. But instead, you have to reference to the one from the UI

                            @
                            TipOfTheDay::TipOfTheDay(QWidget *Child) :
                            QDialog(Child, Qt::CustomizeWindowHint |
                            Qt::WindowCloseButtonHint ),
                            ui(new Ui::TipOfTheDay)
                            {
                            QSettings settings;
                            ui->checkBox->setChecked(settings.value("checkstate").toBool()); // load check-box state
                            }

                            void TipOfTheDay::on_checkBox_clicked()
                            {
                            QSettings settings;
                            settings.setValue("checkstate", ui->checkBox->isChecked()); // save check-box state
                            }
                            @

                            This is standard in C++. A new variable will always create a new object, unless it's not a reference (XXX&) or a pointer. Some specials are here for singletons or monostates, but that's nothing for a UI.

                            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
                              #14

                              @QCheckBox* checkbox = ui->checkBox;@
                              this code worked. thank you everyone for your help. I marked this topic as solved.

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

                                Use "ui->checkBox" directly. There's no need to store the pointe in another variable.

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

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

                                  [quote author="Volker" date="1312543101"]Use "ui->checkBox" directly. There's no need to store the pointe in another variable.[/quote]

                                  ok I did. thank you

                                  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