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 Tuesday, 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 2 Watching
  • 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.
  • raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on 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

    M4RZB4NiM the_T 2 Replies Last reply
    3
    • raven-worxR raven-worx

      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.

      M4RZB4NiM Offline
      M4RZB4NiM Offline
      M4RZB4Ni
      wrote on last edited by
      #11

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

      Thanks
      M4RZB4Ni

      beeckscheB raven-worxR 2 Replies Last reply
      0
      • raven-worxR raven-worx

        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.

        the_T Offline
        the_T Offline
        the_
        wrote on 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
        • M4RZB4NiM M4RZB4Ni

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

          beeckscheB Offline
          beeckscheB Offline
          beecksche
          wrote on 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
          • M4RZB4NiM M4RZB4Ni

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

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on 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

            M4RZB4NiM 1 Reply Last reply
            2
            • raven-worxR raven-worx

              @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( ... );
              
              M4RZB4NiM Offline
              M4RZB4NiM Offline
              M4RZB4Ni
              wrote on 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

              raven-worxR 1 Reply Last reply
              0
              • M4RZB4NiM M4RZB4Ni

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

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on 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

                M4RZB4NiM 1 Reply Last reply
                1
                • raven-worxR raven-worx

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

                  M4RZB4NiM Offline
                  M4RZB4NiM Offline
                  M4RZB4Ni
                  wrote on 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 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 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.

                      M4RZB4NiM 1 Reply Last reply
                      1
                      • M Mitchell

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

                        M4RZB4NiM Offline
                        M4RZB4NiM Offline
                        M4RZB4Ni
                        wrote on last edited by
                        #20

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

                        Thanks
                        M4RZB4Ni

                        1 Reply Last reply
                        0

                        • Login

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