PDA

View Full Version : a QSettings global variable



ilpaso
19th February 2011, 17:34
hi all,
I'd like to use a QSettings variable in all the functions of a class.
I declared inside the header file myapp.h:


public:
QSettings settings;


and in the constructor inside myapp.cpp



this->settings = QSettings("./settings/settings.ini", QSettings::IniFormat);


the compiler returns the error:
qsettings.h:304: error: ‘QSettings& QSettings::operator=(const QSettings&)’ is private

any ideas?
thank you

ilpaso

kornicameister
19th February 2011, 18:10
QSettings is a class which inherits QObject, so if you declare it like this you will get this error because somewhere in the class body private section Q_DISABLE_COPY macro exists which prevents from copying which eventually can happen and what you are trying to do in your code...

if you want to avoid this error simply follow the advice from docs which tells to declare QObject derived classes as pointers or use member methods of QSettings class to set all information you want

take a look here
http://doc.qt.nokia.com/stable/qobject.html#no-copy-constructor-or-assignment-operator

ilpaso
20th February 2011, 09:30
QSettings is a class which inherits QObject, so if you declare it like this you will get this error because somewhere in the class body private section Q_DISABLE_COPY macro exists which prevents from copying which eventually can happen and what you are trying to do in your code...

if you want to avoid this error simply follow the advice from docs which tells to declare QObject derived classes as pointers or use member methods of QSettings class to set all information you want

take a look here
http://doc.qt.nokia.com/stable/qobject.html#no-copy-constructor-or-assignment-operator

Thank you kornicameister for your help.
I tried to use member methods of QSettings class to set all information but I can't set the filename.
I'd like to use a .ini file in build directory. There is the constructor
QSettings ( const QString & fileName, Format format, QObject * parent = 0 )
but I do not find a method to set the fileName.

Thank you.
Regards

ilpaso

squidge
20th February 2011, 13:14
I declared inside the header file myapp.h:


public:
QSettings settings;


and in the constructor inside myapp.cpp



this->settings = QSettings("./settings/settings.ini", QSettings::IniFormat);



Have you tried to initialise QSettings in your constructor like so ?



myClass::myClass(args) : settings("./settings/settings.ini", QSettings::IniFormat)
{
// function body
}


Or you can do it preferred way and use pointer in your header file and initialise using new:



myClass::myClass(args) : settings(new QSettings("./settings/settings.ini", QSettings::IniFormat))
{
// function body
}

ilpaso
20th February 2011, 15:20
Thank you very much squidge! An easy solution for a simple problem (but not for me!)
It works!

Bye
ilpaso