I think the problem might be that you are not configuring QSettings correctly. See this part of the documentation:
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;
To copy to clipboard, switch view to plain text mode
Do this at app startup time; in main() is an OK place, or in your MainWindow() constructor. Just be sure to do it before using QSettings for the first time. If you are running on Windows, QSettings will use the Registry by default. If you don't want that, then call
QSettings::setDefaultFormat( QSettings::IniFormat );
To copy to clipboard, switch view to plain text mode
(again, before using any QSettings in your app).
And as Lesiok said, once this is done, you can use QSettings from anywhere in your app, even multiple places at the same time, and a change made in one place will be visible in another (the next time you ask for the value that was changed).
This is convenient, but using a QMap< QString, QVariant > might be faster. Simply interface this to restore the map at startup time from QSettings, then save it back when the app closes. The major downside of this is that if you app crashes (yours might; none of mine ever do
) you lose any changes that were not saved.
And if you want to use the QMap<> approach, then the way to do it is to make it a "global variable". I do this by deriving a new class from QApplication and adding a call to retrieve it:
{
// ...
public:
const MySettings & settings() const { return settings; } // retrieve for read-only use
MySettings & settings() { return settings; } // retrieve for read / write access
private:
MySettings settings;
};
MyApplication
::MyApplication( int argc,
char * * argv
) : QApplication( argc, argv
){
settings.restore(); // you'll write this to retrieve from QSettings
}
~MyApplication::MyApplication()
{
settings.save(); // you'll write this too.
}
typedef QMap< QString, QVariant > MySettings;
class MyApp : public QApplication
{
// ...
public:
const MySettings & settings() const { return settings; } // retrieve for read-only use
MySettings & settings() { return settings; } // retrieve for read / write access
private:
MySettings settings;
};
MyApplication::MyApplication( int argc, char * * argv ) : QApplication( argc, argv )
{
QCoreApplication::setOrganizationName("MySoft");
QCoreApplication::setOrganizationDomain("mysoft.com");
QCoreApplication::setApplicationName("Star Runner");
QSettings::setDefaultFormat( QSettings::IniFormat );
settings.restore(); // you'll write this to retrieve from QSettings
}
~MyApplication::MyApplication()
{
settings.save(); // you'll write this too.
}
To copy to clipboard, switch view to plain text mode
And if you want to, you could make MySettings a QObject-based class that would emit a signal every time a value was changed so that the app could persist the data to QSettings at that time. You would be protected then against crashes.
Bookmarks