How to declare a global changeable variable?
-
@GCDX
Can i ask what the global will control ?
I understand you first use a #define from .pro file but need app to alter value.
so i wonder what its role is :)
Just to know. -
@GCDX
#define
s and "variables" have nothing to do with each other.Unless Qt has a good reason for advising otherwise(?), I see no reason why a
.pro
file should not have aDEFINES += ...
for stuff you have a good reason to want to define.As @mrjj asks, perhaps it would be a good idea to explain what you are actually trying to achieve?
-
@JonB OKay i see, its because i was editing a QT code that was written by someone else. So he used a define and i assume define was a static global variable that is used but could not really find out how to write a dynamic global variable
-
@JonB It needs an API key. However, the problem is that if too many use the API key it will be a threat to the server and the API key will get banned
-
@GCDX
I can only imagine this means: at present there is a DEFINE for a constant number/string for some "API key", and you want to change that to be a variable which can be assigned at runtime (somehow) so that it can vary. -
@JonB Yes, i want it to change such that every user who downloads the app can generate their own API key so a single API key won't be the one polling the system
-
@GCDX
Then you do indeed need to change over from the DEFINE to a variable! (The existing DEFINE is not a "static global variable", it's a [pre-processor] constant, which is quite different.) -
@GCDX
Read up on http://doc.qt.io/qt-5/qsettings.html#details for an explanation (including sub-topic Platform-Specific Notes). In the usual case, you do not ask the user where to save the settings (yourQFileDialog
), you allow Qt to save it in the "default" place for your OS.QSettings
automatically saves your changes for you.[EDIT: I'm not sure why I started talking about
QSettings
here, I thought you/@mrjj had mentioned it, but perhaps not --- I may have been getting mixed up with another question I was answering! Nonetheless, it's a possible way of saving something to file if that's what you want...] -
@GCDX
Only if user should select file name himself.
else you will just save it using QFile to a writable location , you can find lookup using
http://doc.qt.io/qt-5/qstandardpaths.html -
@GCDX
If you ask the user for a file name/path to save to, you are going to have to ask the user for the same path in order to read it back at a later date. This does not sound likely! More like @mrjj's suggestion of saving to a standard file name in a standard directory so that you can re-read it subsequently automatically. -
@GCDX
QSetting could also be used. Not a bad idea.
On linux it stores in a ini file pr default. on Windows , in registry.