qt - Using QSettings in a global static class -
my task create qsettings
wrapper class (wrapping needed qml) can reach everywhere model, too.
the obvious choice static global instance of class. problem approach it's destroyed after main
, after qapplication
destroyed, giving me following warning:
qapplication::qappname: please instantiate qapplication object first
here simplified, sample code showing simple wrapper, , destruction phases:
#include <qcoreapplication> #include <qdebug> #include <qglobalstatic> #include <qsettings> #include <qtimer> class settings: public qobject { public: ~settings() { qdebug() << "~settings"; } qsettings settings; }; q_global_static(settings, globalsettings) int main(int argc, char *argv[]) { qcoreapplication app(argc, argv); qobject::connect(&app, &qcoreapplication::abouttoquit, [](){qdebug() << "~qcoreapplication abouttoquit";}); qobject::connect(&app, &qcoreapplication::abouttoquit, [](){globalsettings->settings.setvalue("hi", 2);}); qobject::connect(&app, &qcoreapplication::destroyed, [](){qdebug() << "~qcoreapplication destroyed";}); qtimer::singleshot(0, &app, slot(quit())); return app.exec(); }
it prints out following:
~qcoreapplication abouttoquit ~qcoreapplication destroyed ~settings qapplication::qappname: please instantiate qapplication object first
my question is: providing, in program qsettings
used after qcoreapplication::abouttoquit
emitted before qcoreapplication::destroyed
, how can avoid warning?
using qsettings
i've used qsettings
in pretty every project i've ever made. here pattern tend use it:
in main.cpp
#include <qsettings> //... // in main(), after qapplication instantiated, before settings accessed qsettings::setdefaultformat(qsettings::iniformat); qapplication::setorganizationname("myorg"); qapplication::setapplicationname("myapp"):
then anytime access qsettings
, this:
qsettings s; s.value(// reading value s.setvalue(// writing value
everything gets saved in user scope in ini text file. located in windows under c:/users/<username>/appdata/roaming/myorg/myapp.ini
.
this usage of qsettings
(imho) clean, doesn't require global variables or static references , fast , efficient. , readable.
now able have things load settings @ right times, , not errors mentioned in question, see initial example in links below:
http://doc.qt.io/qt-5/qsettings.html#restoring-the-state-of-a-gui-application
http://doc.qt.io/qt-5/qtwidgets-mainwindows-application-example.html
it works better in timeline of qt application , works great. tend make readsettings
, writesettings
of gui classes , number of backend classes. readsettings
happens when widget has showevent
or constructor happen , writesettings
happens in closeevent
. if have dedicated settings dialog, emit signal have affected classes readsettings
, right after settings dialog writes specific settings.
if use qml port of qsettings, uses organization name , application name , default format of qsettings pick filename , location.
http://doc.qt.io/qt-5/qml-qt-labs-settings-settings.html
i believe default functionality of qml component read setting when component created, , write setting whenever qml changes it. change c++ , have recognized qml, should use standard qml/c++ methods out there such as:
http://doc.qt.io/qt-5/qtqml-cppintegration-topic.html
and lastly if planning on installing defaults program decided build , don't want hardcode them, hand write ini file , have installer place in system scope location: c:/programdata/myorg/myapp.ini
and in case settings of program more complex want store in ini file, using qjson
, , savegame example.
hope helps.
Comments
Post a Comment