August 5, 2011

kalster kalster
Lab Rat
315 posts

[SOLVED] saving the result of a check box

 

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

15 replies

August 5, 2011

cincirin cincirin
Ant Farmer
467 posts

Sure. You can save check-box state as

  1. QSettings::setValue("checkstate", QCheckBox::isChecked())

and load it as

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

August 5, 2011

kalster kalster
Lab Rat
315 posts

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.

August 5, 2011

koahnig koahnig
Dinosaur Breeder
2908 posts

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

The code looks more like:

  1. QSettings settings;
  2. QCheckBox cbox;
  3. .
  4. .
  5. .
  6. settings.setValue("checkstate", cbox.isChecked());

and load it as

  1. .
  2. .
  3. .
  4. cboxsetChecked(settings.value("checkstate").toBool());
  5. .

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

August 5, 2011

cincirin cincirin
Ant Farmer
467 posts

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

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

August 5, 2011

kalster kalster
Lab Rat
315 posts

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

cincirin wrote:
make sure you have initialized application & organization name first

I don’t understand that part of your quote.

August 5, 2011

cincirin cincirin
Ant Farmer
467 posts

Can you show us your UI file ?
Also, see QSettings basic usage [doc.qt.nokia.com]

August 5, 2011

kalster kalster
Lab Rat
315 posts

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

  1. #include "tipoftheday.h"
  2. #include "ui_tipoftheday.h"
  3. #include "mainwindow.h"
  4. #include <QDesktopWidget>
  5. #include <QSettings>
  6. #include <QCheckBox>
  7.  
  8.  
  9. TipOfTheDay::TipOfTheDay(QWidget *Child) :
  10.     QDialog(Child, Qt::CustomizeWindowHint |
  11.      Qt::WindowCloseButtonHint ),
  12.     ui(new Ui::TipOfTheDay)
  13. {
  14.     ui->setupUi(this);
  15.     this->setFixedSize(width(), height());
  16.  
  17.     QSettings settings;
  18.     QCheckBox checkbox;
  19.         checkbox.setChecked(settings.value("checkstate").toBool()); // load check-box state
  20. }
  21.  
  22. TipOfTheDay::~TipOfTheDay()
  23. {
  24.     delete ui;
  25. }
  26.  
  27.  
  28. void TipOfTheDay::on_checkBox_clicked()
  29. {
  30.     QSettings settings;
  31.     QCheckBox checkbox;
  32.     settings.setValue("checkstate", checkbox.isChecked()); // save check-box state
  33. }

August 5, 2011

cincirin cincirin
Ant Farmer
467 posts

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

August 5, 2011

kalster kalster
Lab Rat
315 posts

QComboBox* comboBox = ui->checkBox

August 5, 2011

cincirin cincirin
Ant Farmer
467 posts

Then …

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

August 5, 2011

1+1=10 1+1=10
Ant Farmer
643 posts

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.

August 5, 2011

Gerolf Gerolf
Hobby Entomologist
3287 posts

Hi kalster,

youi have a C++ error here:

  1. void TipOfTheDay::on_checkBox_clicked()
  2. {
  3.     QSettings settings;
  4.     QCheckBox checkbox;
  5.     settings.setValue("checkstate", checkbox.isChecked()); // save check-box state
  6. }

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

  1. TipOfTheDay::TipOfTheDay(QWidget *Child) :
  2.     QDialog(Child, Qt::CustomizeWindowHint |
  3.      Qt::WindowCloseButtonHint ),
  4.     ui(new Ui::TipOfTheDay)
  5. {
  6.     QSettings settings;
  7.     ui->checkBox->setChecked(settings.value("checkstate").toBool()); // load check-box state
  8. }
  9.  
  10. void TipOfTheDay::on_checkBox_clicked()
  11. {
  12.     QSettings settings;
  13.     settings.setValue("checkstate", ui->checkBox->isChecked()); // save check-box state
  14. }

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.

 Signature 

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

August 5, 2011

kalster kalster
Lab Rat
315 posts

  1. QCheckBox* checkbox = ui->checkBox;

this code worked. thank you everyone for your help. I marked this topic as solved.

August 5, 2011

Volker Volker
Ant Farmer
5428 posts

Use “ui->checkBox” directly. There’s no need to store the pointe in another variable.

August 6, 2011

kalster kalster
Lab Rat
315 posts
Volker wrote:
Use “ui->checkBox” directly. There’s no need to store the pointe in another variable.

ok I did. thank you

 
  ‹‹ [SOLVED] save and restore window / dialog positions      Segmentation Fault (Core Dumped) ››

You must log in to post a reply. Not a member yet? Register here!