Initializing a custom QSettings file
-
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.
-
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? -
Excellent. That worked. What I really wanted was to initialize with "organization/" + value, but I can work around that when instantiating the class.
-
-
You can also add the string in the initialize list
-
@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? -
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.