QSettings doesn't load the value
-
Hi,
My header file:
#ifndef DIALOGTEST_H #define DIALOGTEST_H #include <QDialog> class QPushButton; class QComboBox; class QVBoxLayout; class QHBoxLayout; class QLabel; class DialogTest : public QDialog { Q_OBJECT public: DialogTest(QWidget *parent = nullptr); private slots: void saveBtnClicked(); void itemChanged(int); void init(); void load(); private: QComboBox* cboBox = nullptr; QHBoxLayout* topHLayout = nullptr; QHBoxLayout* middleHLayout = nullptr; QHBoxLayout* bottomHLayout = nullptr; QVBoxLayout* mainLayout = nullptr; QLabel* select = nullptr; QLabel* ItemSelected = nullptr; QPushButton* save = nullptr; }; #endif // DIALOGTEST_HThe implementation:
#include "DialogTest.h" #include <QPushButton> #include <QComboBox> #include <QHBoxLayout> #include <QVBoxLayout> #include <QMessageBox> #include <QSettings> #include <QLabel> #include <QDebug> DialogTest::DialogTest(QWidget *parent) : QDialog(parent) { select = new QLabel(tr("Select an Item")); cboBox = new QComboBox; ItemSelected = new QLabel(tr("Selected Item")); save = new QPushButton(tr("&Save")); topHLayout = new QHBoxLayout; topHLayout->addWidget(select); topHLayout->addWidget(cboBox); middleHLayout = new QHBoxLayout; middleHLayout->addWidget(ItemSelected); middleHLayout->addStretch(); bottomHLayout = new QHBoxLayout; bottomHLayout->addStretch(); bottomHLayout->addWidget(save); mainLayout = new QVBoxLayout; mainLayout->addLayout(topHLayout); mainLayout->addLayout(middleHLayout); mainLayout->addLayout(bottomHLayout); setLayout(mainLayout); init(); load(); connect(save, &QPushButton::clicked, this, &DialogTest::saveBtnClicked); connect(cboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(itemChanged(int))); } void DialogTest::saveBtnClicked() { QSettings settings; settings.setValue("settings", cboBox->currentIndex()); QMessageBox::information(this, "Save", "Your item is saved, Please re-open the application"); } void DialogTest::itemChanged(int index) { ItemSelected->setText(QString::number(index) + " = " + cboBox->currentText()); } void DialogTest::init() { cboBox->clear(); for (int i=0; i<10; i++) cboBox->addItem("Item number " + QString::number(i)); } void DialogTest::load() { QSettings settings; QVariant value = settings.value("settings",0); bool ok; int index = value.toInt(&ok); if(!ok) { QMessageBox::critical(this, "Error", "Error in loading the file"); return; } if(index < cboBox->count()) cboBox->setCurrentIndex(index); else cboBox->setCurrentIndex(0); }The problem is that the program can't return the value and it's always "Item number 0"!
-
If you want to have your connected widgets working as expected, you should connect them before loading your saved settings.
-
Hi,
My header file:
#ifndef DIALOGTEST_H #define DIALOGTEST_H #include <QDialog> class QPushButton; class QComboBox; class QVBoxLayout; class QHBoxLayout; class QLabel; class DialogTest : public QDialog { Q_OBJECT public: DialogTest(QWidget *parent = nullptr); private slots: void saveBtnClicked(); void itemChanged(int); void init(); void load(); private: QComboBox* cboBox = nullptr; QHBoxLayout* topHLayout = nullptr; QHBoxLayout* middleHLayout = nullptr; QHBoxLayout* bottomHLayout = nullptr; QVBoxLayout* mainLayout = nullptr; QLabel* select = nullptr; QLabel* ItemSelected = nullptr; QPushButton* save = nullptr; }; #endif // DIALOGTEST_HThe implementation:
#include "DialogTest.h" #include <QPushButton> #include <QComboBox> #include <QHBoxLayout> #include <QVBoxLayout> #include <QMessageBox> #include <QSettings> #include <QLabel> #include <QDebug> DialogTest::DialogTest(QWidget *parent) : QDialog(parent) { select = new QLabel(tr("Select an Item")); cboBox = new QComboBox; ItemSelected = new QLabel(tr("Selected Item")); save = new QPushButton(tr("&Save")); topHLayout = new QHBoxLayout; topHLayout->addWidget(select); topHLayout->addWidget(cboBox); middleHLayout = new QHBoxLayout; middleHLayout->addWidget(ItemSelected); middleHLayout->addStretch(); bottomHLayout = new QHBoxLayout; bottomHLayout->addStretch(); bottomHLayout->addWidget(save); mainLayout = new QVBoxLayout; mainLayout->addLayout(topHLayout); mainLayout->addLayout(middleHLayout); mainLayout->addLayout(bottomHLayout); setLayout(mainLayout); init(); load(); connect(save, &QPushButton::clicked, this, &DialogTest::saveBtnClicked); connect(cboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(itemChanged(int))); } void DialogTest::saveBtnClicked() { QSettings settings; settings.setValue("settings", cboBox->currentIndex()); QMessageBox::information(this, "Save", "Your item is saved, Please re-open the application"); } void DialogTest::itemChanged(int index) { ItemSelected->setText(QString::number(index) + " = " + cboBox->currentText()); } void DialogTest::init() { cboBox->clear(); for (int i=0; i<10; i++) cboBox->addItem("Item number " + QString::number(i)); } void DialogTest::load() { QSettings settings; QVariant value = settings.value("settings",0); bool ok; int index = value.toInt(&ok); if(!ok) { QMessageBox::critical(this, "Error", "Error in loading the file"); return; } if(index < cboBox->count()) cboBox->setCurrentIndex(index); else cboBox->setCurrentIndex(0); }The problem is that the program can't return the value and it's always "Item number 0"!
@qcoderpro said in QSettings doesn't load the value:
QSettings settings;Since you don't set organization name etc., https://doc.qt.io/qt-5/qsettings.html#QSettings-4
If QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName() has not been previously called, the QSettings object will not be able to read or write any settings, and status() will return AccessError.
Check
status(), you should see an error on your calls. -
Hi,
Did you properly call the various application related information setters like shown QSettings details.
-
thanks, I use:
QSettings settings("MySoft", "Star Runner");And now it works. But when the program is re-run and it returns the value saved for the combobox, the label won't change while the slot itemChanged(int index) must be called by the second connection, populating the label!
-
If you want to have your connected widgets working as expected, you should connect them before loading your saved settings.
-
If you want to have your connected widgets working as expected, you should connect them before loading your saved settings.