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 create a class based on QSettings
Forum Updated to NodeBB v4.3 + New Features

How to create a class based on QSettings

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 3.9k Views 1 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.
  • K Offline
    K Offline
    KelvinSP
    wrote on last edited by KelvinSP
    #1

    I'm trying to create a class based on QSettings, so I can create a constructor with default values, for example:

    #ifndef MYSETTINGS_H
    #define MYSETTINGS_H
    
    #include <QSettings>
    
    class MySettings : public QSettings
    {
        Q_OBJECT
    public:
        MySettings(QString fileName = "",
                   QSettings::Format format = QSettings::IniFormat,
                   QObject *parent = 0);
    
    signals:
    
    public slots:
    
    };
    
    #endif // MYSETTINGS_H
    

    CPP:

    #include "mysettings.h"
    #include <QStandardPaths>
    
    MySettings::MySettings(QString fileName, QSettings::Format format, QObject *parent)
    {
        if( fileName.isEmpty() )
        {
            QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
            fileName = path + "/Settings.ini";
        }
    }
    

    So I can use it as follows:

    MySettings settings();
    
    settings.beginGroup("Test");
    settings.setValue("First Value", 1);
    settings.endGroup();
    

    Instead of:

    QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
    QSettings settings2(path + "/Settings2.ini", QSettings::IniFormat);
    
    settings2.beginGroup("Test2");
    settings2.setValue("Second Value", 2);
    settings2.endGroup();
    

    Is it possible?

    Thanks

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      yes that should be possible.

      Anyway, docs says
      " Constructing and destroying a QSettings object is very fast."

      So you could also just use a function to return a QSettings and in that way hide the
      StandardPaths::writableLocation(QStandardPaths::ConfigLocation); etc

      K 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        yes that should be possible.

        Anyway, docs says
        " Constructing and destroying a QSettings object is very fast."

        So you could also just use a function to return a QSettings and in that way hide the
        StandardPaths::writableLocation(QStandardPaths::ConfigLocation); etc

        K Offline
        K Offline
        KelvinSP
        wrote on last edited by KelvinSP
        #3

        @mrjj thanks.

        Do you mean something like this?

        QSettings *mySettings()
        {
            QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
            QSettings *settings = new QSettings(path + "/Settings.ini", QSettings::IniFormat);
            return settings;
        }
        
        QSettings *ptSettings = mySettings();
        ptSettings->beginGroup("Test");
        ptSettings->setValue("Value", 3);
        ptSettings->endGroup();
        
        delete ptSettings;
        

        It seems to work.

        1 Reply Last reply
        0
        • hskoglundH Online
          hskoglundH Online
          hskoglund
          wrote on last edited by hskoglund
          #4

          Hi, also, instead of inheriting from QSettings, you could construct your own class with an encapsulated QSettings*, say something like this:

          class MySettings
          {
              QSettings* pSettings;
          
              MySettings() : MySettings(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/Settings.ini") { }
              MySettings(QString sIniFileName) { pSettings = new QSettings(sIniFileName,QSettings::IniFormat); }
              ~MySettings() { delete pSettings; }
          
              void writeInt(QString sKeyName, int nValue) { pSettings->setValue(sKeyName,nValue); }
          }
          

          And to use it, easiest is to declare it as a class instance, say in your MainWindow.h:

          MySettings settings;
          

          and then use it like this:

          settings.writeInt("Test/Value",3);
          
          K 1 Reply Last reply
          1
          • P Offline
            P Offline
            patrik08
            wrote on last edited by patrik08
            #5

            I have a class name document_session as instance:

            /// Header pointer & static:
            
            QPointer<DOC> DOC::_self = 0L;
            
            DOC *DOC::self(QObject *parent) {
              if (!_self)
                _self = new DOC(parent);
              return _self;
            }
            void DOC::setValue(const QString name, QVariant data) {
              QSettings session_s(QString("OasiClub"), QString("OasiEditor"));
              session_s.setValue(name, data);
            }
            
            QVariant DOC::value(const QString name) {
              QSettings session_s(QString("OasiClub"), QString("OasiEditor"));
              return session_s.value(name);
            }
            
            /// after read e write:
            const int uservoice = DOC::self(this)->value("MyVoicePref").toInt();
            

            sample:
            https://github.com/pehohlva/QOASIS/blob/master/src/app/doc_session.cpp
            https://github.com/pehohlva/QOASIS/blob/master/src/app/doc_session.h

            https://github.com/pehohlva/QOASIS
            https://sourceforge.net/projects/oasidoc/

            1 Reply Last reply
            0
            • hskoglundH hskoglund

              Hi, also, instead of inheriting from QSettings, you could construct your own class with an encapsulated QSettings*, say something like this:

              class MySettings
              {
                  QSettings* pSettings;
              
                  MySettings() : MySettings(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/Settings.ini") { }
                  MySettings(QString sIniFileName) { pSettings = new QSettings(sIniFileName,QSettings::IniFormat); }
                  ~MySettings() { delete pSettings; }
              
                  void writeInt(QString sKeyName, int nValue) { pSettings->setValue(sKeyName,nValue); }
              }
              

              And to use it, easiest is to declare it as a class instance, say in your MainWindow.h:

              MySettings settings;
              

              and then use it like this:

              settings.writeInt("Test/Value",3);
              
              K Offline
              K Offline
              KelvinSP
              wrote on last edited by
              #6

              @hskoglund thanks. I liked this approach. I will try it out.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KelvinSP
                wrote on last edited by
                #7

                Just with the purpose of helping someone else, I have implemented the following class based on the @hskoglund answer:

                Header:

                #ifndef MYSETTINGS_H
                #define MYSETTINGS_H
                
                #include <QVariant>
                #include <QSettings>
                #include <QStandardPaths>
                
                class MySettings
                {
                public:
                    MySettings(QString fileName = "", QSettings::Format format = QSettings::IniFormat);
                    ~MySettings();
                
                    void setValue(const QString &key, const QVariant &value);
                    QVariant value(const QString &key, const QVariant &defaultValue = QVariant());
                
                    void beginGroup(const QString &prefix);
                    void endGroup();
                
                private:
                    QSettings *pSettings_;
                };
                
                #endif // MySettings_H
                

                Code:

                #include "mysettings.h"
                
                MySettings::MySettings(QString fileName, QSettings::Format format)
                {
                    if( fileName.isEmpty() )
                    {
                        QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
                        fileName = path + "/Settings.ini";
                    }
                
                    pSettings_ = new QSettings(fileName, format);
                }
                
                MySettings::~MySettings()
                {
                    delete pSettings_;
                }
                
                void MySettings::setValue(const QString &key, const QVariant &value)
                {
                    pSettings_->setValue(key, value);
                }
                
                QVariant MySettings::value(const QString &key, const QVariant &defaultValue)
                {
                    return pSettings_->value(key, defaultValue);
                }
                
                void MySettings::beginGroup(const QString &prefix)
                {
                    pSettings_->beginGroup(prefix);
                }
                
                void MySettings::endGroup()
                {
                    pSettings_->endGroup();
                }
                
                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  patrik08
                  wrote on last edited by
                  #8

                  @KelvinSP said in How to create a class based on QSettings:

                  Settings.ini

                  hi... only window work on Settings.ini...
                  Cross Compatible ?

                  mrjjM jsulmJ K 3 Replies Last reply
                  0
                  • P patrik08

                    @KelvinSP said in How to create a class based on QSettings:

                    Settings.ini

                    hi... only window work on Settings.ini...
                    Cross Compatible ?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @patrik08
                    Hi
                    http://doc.qt.io/qt-5/qsettings.html#Format-enum
                    "on Unix, this means textual configuration files in INI format."

                    so .ini is not only windows it seems :)

                    1 Reply Last reply
                    1
                    • P patrik08

                      @KelvinSP said in How to create a class based on QSettings:

                      Settings.ini

                      hi... only window work on Settings.ini...
                      Cross Compatible ?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @patrik08 Registry is "Windows only". Ini files can be used on any platform as they are just files.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • P patrik08

                        @KelvinSP said in How to create a class based on QSettings:

                        Settings.ini

                        hi... only window work on Settings.ini...
                        Cross Compatible ?

                        K Offline
                        K Offline
                        KelvinSP
                        wrote on last edited by KelvinSP
                        #11

                        @patrik08 as @jsulm mentioned INI files can be used on any platform. I personally use it on Windows and OS X.

                        1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          patrik08
                          wrote on last edited by
                          #12

                          ok...
                          super...
                          QSettings::setDefaultFormat (QSettings::IniFormat )
                          any time..

                          1 Reply Last reply
                          0
                          • JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @KelvinSP said in How to create a class based on QSettings:

                            Just with the purpose of helping someone else, I have implemented the following class based on the @hskoglund answer

                            Just my 2 cents, but I don't understand why you would choose to encapsulate in this case rather than derive. Most of your code seems to act on the encapsulated QSettings object anyway, so I would see derivation as more natural (e.g. with derivation you can override or call protected members, with encapsulation you cannot). Not meant as a criticism, there are often pros & cons, up to you.

                            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