Edit QSettings for an app outside the source
-
In a situation where a QSettings key is erroneous or otherwise unwanted by the end user, how could they flush/clear their settings for that application? I'm talking specifically about
QSettings::NativeFormat
. -
@Ewan-Green
What do you mean by this? You get, say, one unwanted key from the Windows Registry and that makes you want to wipe everything there? There is QSettings::clear()Removes all entries in the primary location associated to this QSettings object.
Is that what you want? Or QSettings::remove(const QString &key) removes just one key?
-
No. The point is to do it from outside the app. In this scenario, the problem is that the developer didn't plan ahead, and the settings key is unusable or invalid causing bad behavior/crashes in the app. If I were the user, I shouldn't have to modify the source code to get it back to a usable state. With
QSettings::IniFormat
or something, I could just change the .ini file; it would be more straightforward as all the other QSettings formats have unified methods for changing the settings across platforms. The native format does not, so it's not quite as straightforward.Also, it doesn't have to be every single settings key; I just figured if there was an existing way to do it, that would be it.
-
@Ewan-Green said in Edit QSettings for an app outside the source:
the settings key is unusable or invalid causing bad behavior/crashes in the app
In that case the app should be fixed. An application should not crash just because there are unwanted/invalid settings.
"If I were the user, I shouldn't have to modify the source code to get it back to a usable state" - there is no need to modify source code, user can modify the settings. For settings in registry there is regedit.exe, ini files can be edited in any text editor.
You can also implement a function in your app which resets the settings to their default values. -
@Ewan-Green said in Edit QSettings for an app outside the source:
The native format does not, so it's not quite as straightforward.
The native format varies from platform to platform. It is the Windows registry on that platform, INI format text files on Linux, and CFPreferences API in the Apple world. There is, therefore, no single method.
Assuming such a reset option is infrequently required you could use a command line switch to trigger a clear before any GUI is started (and potentially uses an invalid value blindly). Something like
int main(int argc, char **argv) { QApplication app(argc, argv); QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setOrganizationDomain("mysoft.com"); QCoreApplication::setApplicationName("Unicorn Fun"); QCoreApplication::setApplicationVersion("1.0"); QCommandLineParser parser; parser.setApplicationDescription("Unicorn herding application"); QCommandLineOption resetOption("r", QCoreApplication::translate("main", "Reset default settings") ); parser.addOption(resetOption); ... parser.process(app); bool reset = parser.isSet(resetOption); if (reset) { QSettings settings; settings.clear(); // or whatever passes for "reset" options } // start GUI etc. return app.exec(); }
-
@Ewan-Green
I still don't know what you are asking for.If you want to alter the settings content, for whatever reason, from within the Qt app we have shown for example
QSettings
methods to clear all the keys, or remove individual ones, or you can do whatever you want.The point is to do it from outside the app.
If I were the user, I shouldn't have to modify the source code to get it back to a usable state.If this is what you want, of course the user does not have to modify your source code. If, say, your platform is Windows then the native format naturally saves to the Registry, and you must know that Windows supplies the Registry Editor (
regedit
) to allow users --- with suitable permissions --- to browse and edit entries from a UI to their heart's content, including those saved by a Qt application. There are thousands of articles on the web telling people to do this for all sorts of purposes. Which seems to be exactly what you are complaining does not exist? -
I was asking if there was a tool of the same nature available cross-platform for these kind of things, because of the fact that the native format varies from platform to platform. This thread is getting needlessly backhanded.
This is not a theoretical situation. In this app, the last saved MIDI port number is no longer available, so it crashes when trying to open said port. I'd love to go back in time and make the developer aware of this issue, but it hasn't been maintained for quite some time.
-
You could probably create a simple utility where all it basically does is create a QSettings instance with the same organization and application names as your application and then call QSettings::clear() as has already been mentioned. I'm not if you have to worry about the fallback settings locations. Since those are generally not writeable when run as a user, those settings should not have gotten corrupted.
-
@mchinand As I wrote before an app should handle its settings in a way it does not crash just because some settings are invalid...
So, instead of implementing work-arounds (like some tools to fix settings) fix your app and make sure it is robust.
If a developer would tell me I have to use a tool to fix the settings to prevent his/her app from crashing I would look for alternatives to that app... -
@Ewan-Green
As said before, you have two choices, depending on what you want.If you want one tool which works cross-platform and on native saved settings, you have only one choice: Qt itself. Since only that has support for cross-platform and native. Just like the code is now.
You then have a choice with Qt:
- Write a standalone utility, which allows whatever entries to be viewed/edited enough to deal with your cases. use that to "clear up" your current entries.
- Put error handling into your existing application, so that if, say, it throws an error like you show the code traps that and takes whatever appropriate remedial action, e.g. updating a value or deleting it or whatever.
Either of these might use the kind of
QSettings
we have been referring to, such asclear()
orremoveKey()
. Which do work cross-platform/native.Only the first of these satisfies your statement "The point is to do it from outside the app.".
The other way of doing that is to use an existing, non-Qt, external tool for the target platform. Such as
regedit
for the Windows Registry.I don't know what else you could be asking for.
-
@Ewan-Green said in Edit QSettings for an app outside the source:
I was asking if there was a tool of the same nature available cross-platform for these kind of things, because of the fact that the native format varies from platform to platform.
On Windows, use regedit. On Linux, edit the relevant text file. On MacOS, use the Mac settings command line tool, etc. The reason that Qt uses the native config system on each platform is so that if a user does need to tinker and hack with the settings, they don't need anything Qt specific and can do it the "normal" way the user is already familiar with on that platform.