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. Initializing a custom QSettings file
Qt 6.11 is out! See what's new in the release blog

Initializing a custom QSettings file

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.5k 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.
  • B Offline
    B Offline
    Bruce_Dubbs
    wrote on last edited by
    #1

    I am trying to create an application that can have multiple instances. I have a class where I want to manage settings uniquely for each instance. If I do something like:

    class SM_Settings : public QObject
    {
      Q_OBJECT
    
      private:
        QString      string; 
        QSettings    settings;
    }
    
    SM_Settings::SM_Settings( const QString value ) : string( value )
    {
      settings = QSettings( string );
    }
    

    I get an error error: use of deleted function ‘QSettings& QSettings::operator=(const QSettings&)’
    6 | settings = QSettings( "test" );
    note: in expansion of macro ‘Q_DISABLE_COPY’
    211 | Q_DISABLE_COPY(QSettings)

    I want to use the QSettings variable in virtually every function in the class. Is there a way to do that other than defining a QSettings variable in every function?

    Note that this class will only be instantiated once per application instance, but each instance will have a different value passed during instantiation.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Since QSettings is a QObject and QObjects can't be copied you are not allowed to copy a QSettings object also.
      Why not simply initialize it in the initializer list instead?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • B Offline
        B Offline
        Bruce_Dubbs
        wrote on last edited by
        #3

        Excellent. That worked. What I really wanted was to initialize with "organization/" + value, but I can work around that when instantiating the class.

        Pl45m4P 1 Reply Last reply
        0
        • B Bruce_Dubbs has marked this topic as solved on
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can also add the string in the initialize list

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • B Bruce_Dubbs

            Excellent. That worked. What I really wanted was to initialize with "organization/" + value, but I can work around that when instantiating the class.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #5

            @Bruce_Dubbs said in Initializing a custom QSettings file:

            What I really wanted was to initialize with "organization/" + value, but I can work around that when instantiating the class.

            Like @Christian-Ehrlicher said:

            class SM_Settings : public QObject
            {
              Q_OBJECT
               
               SM_Settings(const QString &value, QObject *parent = nullptr);
            
             private:
                QString      string; 
                QSettings    settings;
            }
            
            SM_Settings::SM_Settings( const QString &value, QObject *parent )
              : QObject(parent)
              , string(value)
              , settings(string){}
            

            Btw, it's always to good practise to pass strings as reference. And don't forget to init the underlaying QObject properly.

            Just curious: Why is you class a QObject and even contains the Q_OBJECT macro?!
            Unless you have lot of code removed for this snippet above, you don't need any of it.
            Does your class need to use Qt Signals?


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bruce_Dubbs
              wrote on last edited by
              #6

              Actually I've learned a few things better here in a couple of posts. For my application and this class I did not need to inherit QObject or use Q_OBJECT at all. Indeed, I didn't need to save 'value' at all. I just needed to initialize settings properly. I thank you for your comments.

              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