Unsolved Save permanent settings on QT Web App
-
Hi everyone,
I was thinking of using Qsettings to save a user's username, however, i can't seem to be able to find how to do that. Can someone give me an example how to define my own variable such as QString username and store it permanently(won't be deleted after the app is closed) . If not , i'm open to other suggestions on how to go about doing this.
-
@GCDX
You just use http://doc.qt.io/qt-5/qsettings.html#setValue to set your own name & value, e.g.QSettings::setValue("username", username)
, that's it. -
Hi @GCDX ,
For setting and getting values of user by QSetting class you can perform following code.QSettings* InitRegSettings()
{
QSettings* regSett;
regSett = new QSettings("Organization-name","Project-name");
return regSett;
}void SetMyValue(QString key, QVariant value)
{
InitRegSettings()->setValue(key,value); //Store value of user
}
QVariant GetMyValue(QString key, QVariant defaultValue)
{
return InitRegSettings()->value(key,defaultValue);//Get value of user
} -
@Prince_0912 @JonB do i need to define a file to store all the setting values??? Beccause i followed this
https://wiki.qt.io/How_to_Use_QSettingsIt showed me how to make a file and all, except it did not tell me how to write to a file and read?
-
Also, do i need to use a special file format or can i just anything like txt? I'm really confused because there's not documentation on how the settings are saved? Do i need to create a file to store the settings? Where do i create this file to store the settings?
-
@GCDX Please take some time to read http://doc.qt.io/qt-5/qsettings.html - it explains everything
-
@GCDX
If you are going to useQSettings
, the links we have asked you to read answer all the questions: Qt deals with the format, the filename & the location, you don't create or do anything (other than @Prince_0912 's skeleton code).If you do all this this yourself instead, it's up to you.
-
@GCDX ,
These values are store in Registry.Computer\HKEY_CURRENT_USER\Software\Organization-name\Project-name
In this location the value is set and get.
-
@JonB @Prince_0912 okay thanks so much. https://wiki.qt.io/How_to_Use_QSettings cause this told me to create an ini file to store the information, so i was very confused! Thanks so much for the help
-
@GCDX Your'e Welcome,
after getting solution,
Go to right hand side of page -> click on Topic Tools -> Mark as Solved. -
@Prince_0912 said in Save permanent settings on QT Web App:
Hi @GCDX ,
For setting and getting values of user by QSetting class you can perform following code.QSettings* InitRegSettings()
{
QSettings* regSett;
regSett = new QSettings("Organization-name","Project-name");
return regSett;
}void SetMyValue(QString key, QVariant value)
{
InitRegSettings()->setValue(key,value); //Store value of user
}
QVariant GetMyValue(QString key, QVariant defaultValue)
{
return InitRegSettings()->value(key,defaultValue);//Get value of user
}You have several memory leaks here. Each time you call one of these function, you create a new
QSettings
object that is not deleted.If the application object is properly configured like described in the QSettings documentation, you just need to use
QSettings
on the stack wherever you need it. -
@GCDX
Further to @SGaist 's observation.Although http://doc.qt.io/qt-5/qsettings.html#details states
Constructing and destroying a QSettings object is very fast.
to me it seems "odd/inefficient" to be creating one specifying your organization & project names each time, as these never vary. I would either take the doc's suggestion of:
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
or I would create one & only one
QSettings
in your app and re-use that each time.