How to Save Settings In Qt?
-
@Mitchell
hi,
i wrote this codes but settings dose not saved!
what is the problem?Secretary::Secretary(QWidget *parent) : QWidget(parent), ui(new Ui::Secretary) { ui->setupUi(this); ui->comboBox->setCurrentIndex(Settings.value("server").toInt()); } Secretary::~Secretary() { ui->comboBox->setCurrentIndex(Settings.value("server").toInt()); delete ui; } void Secretary::on_comboBox_currentIndexChanged(int index) { if(ui->comboBox->currentIndex()==2){ ui->pushButton_3->setDisabled(true); }else if(ui->comboBox->currentIndex()==1){ ui->pushButton_3->hide(); }else if(ui->comboBox->currentIndex()==0){ if(ui->lineEdit_56->text()==NULL){ ui->pushButton_8->setDisabled(true); } } Settings.setValue("server",ui->comboBox->currentIndex()); }
-
i am not 100% sure, but i think you need to provide at least the organizational name (see QSettings constructors).
If you leave it out using the default constructor - like you are doing - you shoudl at least set it on the QApplication as a fallback.Because using the QSettings default constructor uses the native format, which on Windows is the Registry.
-
@raven-worx
what i must do Exactly?
can you write a sample code for me? -
@raven-worx
Correct.
QSettings documentation says: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.
for example:
//main.cpp int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setApplicationDisplayName("My Tool"); a.setOrganizationDomain("www.myurl.com"); a.setOrganizationName("My Company"); a.setApplicationName("My Application"); a.setApplicationVersion("version x"); //continue with your code }
If (on windows) you use the default format, wich is
QSettings::NativeFormat
the settings are stored in the Windows Registry in \HKEY_CURRENT_USER\Software\My Company\My Application -
Copied from: http://doc.qt.io/qt-5/qsettings.html
When creating a QSettings object, you must pass the name of your company or organization as well as the name of your application. For example, if your product is called Star Runner and your company is called MySoft, you would construct the QSettings object as follows:
QSettings settings("MySoft", "Star Runner");
If you use QSettings from many places in your application, you might want to specify the organization name and the application name using QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(), and then use the default QSettings constructor:
QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setOrganizationDomain("mysoft.com"); QCoreApplication::setApplicationName("Star Runner"); ... QSettings settings;
I set the organization name and application name in my main entry function.
You can also group your entries:
If you want to save or restore many settings with the same prefix, you can specify the prefix using beginGroup() and call endGroup() at the end. Here's the same example again, but this time using the group mechanism:
settings.beginGroup("mainwindow"); settings.setValue("size", win->size()); settings.setValue("fullScreen", win->isFullScreen()); settings.endGroup(); settings.beginGroup("outputpanel"); settings.setValue("visible", panel->isVisible()); settings.endGroup();
Hope this helps!
-
@M4RZB4Ni
for example in your main() method:qApp->setOrganizationName("My Company Inc."); qApp->setOrganizationDomain("mycompany.com"); qApp->setApplicationName("My Application");
or alternatively create one global static QSettings object (to avoid object creation everytime you want to access QSettings):
// in .h-File class MainWindow : public QMainWidnow { Q_OBJECT public: static QSettings Settings; }; // beginning of .cpp-File QSettings MainWindow::Settings = QSettings("My Company Inc.", "My Application");
Then use
MainWindow::Settings.setValue( ... ); MainWindow::Settings.value( ... );
-
@raven-worx
@beecksche
@the_
@Mitchell
tnx
but if i want to save settings in a ini or xml file what i must do? -
@M4RZB4Ni said:
but if i want to save settings in a ini or xml file what i must do?
read the docs in the first place.
See the QSettings constructors. -
i wrote this codes but dose not work!
QSettings settings("Mobtakeran Fanavri KabooK","Kabook Physiothrapy"); Secretary::Secretary(QWidget *parent) : QWidget(parent), ui(new Ui::Secretary) { ui->setupUi(this); ui->comboBox->setCurrentIndex(settings.value("comboBox").toInt()); } Secretary::~Secretary() { QCoreApplication::setOrganizationName("Mobtakeran Fanavri KabooK"); QCoreApplication::setOrganizationName("WWW.M4RZB4Ni.IR"); QCoreApplication::setApplicationName("Kabook Physiothrapy"); delete ui; } void Secretary::on_comboBox_currentIndexChanged(int index) { settings.beginGroup("comboBox"); if(ui->comboBox->currentIndex()==2){ ui->pushButton_3->setDisabled(true); }else if(ui->comboBox->currentIndex()==1){ ui->pushButton_3->hide(); settings.setValue("comboBox",ui->comboBox->currentIndex()); }else if(ui->comboBox->currentIndex()==0){ if(ui->lineEdit_56->text()==NULL){ ui->pushButton_8->setDisabled(true); } } settings.endGroup(); }
What i must do?
-
look at what @the_ as well as @raven-worx. They both said that you need to set the organizationName in MAIN().
You are setting it in the destructor of your class. There is zero reason to have it there.From @raven-worx
or example in your main() method:qApp->setOrganizationName("My Company Inc.");
qApp->setOrganizationDomain("mycompany.com");
qApp->setApplicationName("My Application");From @the_
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationDisplayName("My Tool");
a.setOrganizationDomain("www.myurl.com");
a.setOrganizationName("My Company");
a.setApplicationName("My Application");
a.setApplicationVersion("version x");
//continue with your code
}This will set up the QSettings before you use it in any of the other classes you use. The purpose behind this is so it has something to name itself. So on mac if you go to $HOME/Library/Preferences/com.orgname.appname.plist
This is where you will actually be saving the settings to. So you need to set the OrganizationName before you use QSettings at all. SO the best place is in Main because it runs first. I am not sure where it gets saved to on windows you could probably do a google search to find that.
-
@M4RZB4Ni
I made a full example code for you so you can see one that works.
It has a button and a label. When you click the button it will hide or show the label. And when you close and reopen the program the label will be in the same state as when you closed the program.main.cpp
#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication app(argc, argv); app.setOrganizationDomain("sampleCompany.com"); app.setOrganizationName(QLatin1String("sample Company")); app.setApplicationName(QLatin1String("Example Application")); MainWindow w; w.show(); return app.exec(); }
mainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
mainWindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QSettings> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QSettings mySettings; //set default status of all ui elements bool value = mySettings.value("label").toBool(); if (value == true) { ui->label->show(); } else { ui->label->hide(); } } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QSettings mySettings; if (ui->label->isVisible()) { ui->label->hide(); mySettings.setValue("label", false); } else { ui->label->show(); mySettings.setValue("label", true); } }
Very minimal example that works. Hope this will clear everything up for you.