[SOLVED] saving the result of a check box
-
wrote on 5 Aug 2011, 07:05 last edited by
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.
-
wrote on 5 Aug 2011, 07:13 last edited by
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".
-
wrote on 5 Aug 2011, 07:13 last edited by
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
@ -
wrote on 5 Aug 2011, 07:42 last edited by
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.
-
wrote on 5 Aug 2011, 07:50 last edited by
Can you show us your UI file ?
Also, see "QSettings basic usage":http://doc.qt.nokia.com/stable/qsettings.html#basic-usage -
wrote on 5 Aug 2011, 07:55 last edited by
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
}@ -
wrote on 5 Aug 2011, 08:00 last edited by
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 -
wrote on 5 Aug 2011, 08:02 last edited by
QComboBox* comboBox = ui->checkBox
-
wrote on 5 Aug 2011, 08:05 last edited by
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
@ -
wrote on 5 Aug 2011, 08:07 last edited by
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.
-
wrote on 5 Aug 2011, 08:09 last edited by
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.
-
wrote on 5 Aug 2011, 08:19 last edited by
@QCheckBox* checkbox = ui->checkBox;@
this code worked. thank you everyone for your help. I marked this topic as solved. -
wrote on 5 Aug 2011, 11:18 last edited by
Use "ui->checkBox" directly. There's no need to store the pointe in another variable.
-
wrote on 6 Aug 2011, 01:44 last edited by
[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
12/16