[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
-
Sure. You can save check-box state as
@
QSettings::setValue("checkstate", QCheckBox::isChecked())
@and load it as
@
QCheckBox::setChecked(QSettings::value("checkstate").toBool())
@ -
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.
-
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".
-
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
@ -
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.
-
Can you show us your UI file ?
Also, see "QSettings basic usage":http://doc.qt.nokia.com/stable/qsettings.html#basic-usage -
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
}@ -
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 -
QComboBox* comboBox = ui->checkBox
-
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
@ -
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.
-
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.
-
@QCheckBox* checkbox = ui->checkBox;@
this code worked. thank you everyone for your help. I marked this topic as solved. -
Use "ui->checkBox" directly. There's no need to store the pointe in another variable.
-
[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