Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Save Settings In Qt?
Forum Update on Monday, May 27th 2025

How to Save Settings In Qt?

Scheduled Pinned Locked Moved Solved General and Desktop
savingloadingqt 5.4.1
20 Posts 5 Posters 22.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Mitchell
    19 May 2016, 16:35

    @M4RZB4Ni
    It is a key value pair. So you are saving the value of "hidden" to "lineEdit". So i use this for a program where the user can connect to multiple servers.

    Example:

    QSettings mySettings;
    	mySettings.setValue("server",p_Ui->serverSelector->currentIndex());
    

    So here i set the value of "server" to the value that is in my serverSelector( This is a QComboBox). I can then set member variables to be = to the correct server. And when i launch the program i do:

    QSettings mySettings;
    	m_pUi->serverSelector->setCurrentIndex(mySettings.value("server").toInt());
    

    this will load the value that i stored int mySettings.value("server"); This way i can preload the ui elements to what they had picked last time the program ran.

    M Offline
    M Offline
    M4RZB4Ni
    wrote on 20 May 2016, 08:55 last edited by
    #9

    @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());
    }
    

    Thanks
    M4RZB4Ni

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 20 May 2016, 09:06 last edited by
      #10

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      M T 2 Replies Last reply 20 May 2016, 09:21
      3
      • R raven-worx
        20 May 2016, 09:06

        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.

        M Offline
        M Offline
        M4RZB4Ni
        wrote on 20 May 2016, 09:21 last edited by
        #11

        @raven-worx
        what i must do Exactly?
        can you write a sample code for me?

        Thanks
        M4RZB4Ni

        B R 2 Replies Last reply 20 May 2016, 09:31
        0
        • R raven-worx
          20 May 2016, 09:06

          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.

          T Offline
          T Offline
          the_
          wrote on 20 May 2016, 09:30 last edited by
          #12

          @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.

          @M4RZB4Ni

          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

          -- No support in PM --

          1 Reply Last reply
          1
          • M M4RZB4Ni
            20 May 2016, 09:21

            @raven-worx
            what i must do Exactly?
            can you write a sample code for me?

            B Offline
            B Offline
            beecksche
            wrote on 20 May 2016, 09:31 last edited by
            #13

            @M4RZB4Ni

            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!

            1 Reply Last reply
            2
            • M M4RZB4Ni
              20 May 2016, 09:21

              @raven-worx
              what i must do Exactly?
              can you write a sample code for me?

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 20 May 2016, 09:33 last edited by raven-worx
              #14

              @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( ... );
              

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              M 1 Reply Last reply 20 May 2016, 09:52
              2
              • R raven-worx
                20 May 2016, 09:33

                @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( ... );
                
                M Offline
                M Offline
                M4RZB4Ni
                wrote on 20 May 2016, 09:52 last edited by
                #15

                @raven-worx
                @beecksche
                @the_
                @Mitchell
                tnx
                but if i want to save settings in a ini or xml file what i must do?

                Thanks
                M4RZB4Ni

                R 1 Reply Last reply 20 May 2016, 09:57
                0
                • M M4RZB4Ni
                  20 May 2016, 09:52

                  @raven-worx
                  @beecksche
                  @the_
                  @Mitchell
                  tnx
                  but if i want to save settings in a ini or xml file what i must do?

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 20 May 2016, 09:57 last edited by
                  #16

                  @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.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  M 1 Reply Last reply 20 May 2016, 16:22
                  1
                  • R raven-worx
                    20 May 2016, 09:57

                    @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.

                    M Offline
                    M Offline
                    M4RZB4Ni
                    wrote on 20 May 2016, 16:22 last edited by M4RZB4Ni
                    #17

                    @raven-worx
                    @Mitchell

                    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?

                    Thanks
                    M4RZB4Ni

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Mitchell
                      wrote on 20 May 2016, 17:08 last edited by
                      #18

                      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.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        Mitchell
                        wrote on 20 May 2016, 17:24 last edited by
                        #19

                        @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.

                        M 1 Reply Last reply 20 May 2016, 20:25
                        1
                        • M Mitchell
                          20 May 2016, 17:24

                          @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.

                          M Offline
                          M Offline
                          M4RZB4Ni
                          wrote on 20 May 2016, 20:25 last edited by
                          #20

                          @Mitchell
                          @the_
                          @raven-worx
                          Thanks form all
                          my Problem solved!
                          thanks a lot :)

                          Thanks
                          M4RZB4Ni

                          1 Reply Last reply
                          0

                          18/20

                          20 May 2016, 17:08

                          • Login

                          • Login or register to search.
                          18 out of 20
                          • First post
                            18/20
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved