saving configs in qt linux
i wrote a qt application for linux. The application is supposed to run at startup- which i did with a desktop entry.
but i need it to be more complicated: there is a checkbox that the user supposed to check in order to choose whether the application will run at startup or not.
how do i suppose to save his prefference?
the application was wriiten for windows before and this was saved in the registry. i got from googling that i should save it in /etc.
what file should it be? how do i write it in my code? and could i add a condition to the desktop entry, or should i run some script?
i'm quite开发者_运维百科 new in all this, so i will appriciate a detailed answer.
thank u.
For this particular case, saving a preference setting controlling whether the app should run at startup or not is completely pointless. The very existence of the autorun entry desktop file reflects the state of that preference. If that file exists, you check the checkbox. If the user unchecks the checkbox, you delete the file. If the user checks the checkbox, you create the file. That's it. Duplicating the setting in a preference store will only lead to bugs since now you have to keep the setting and the presence of the file in the file system in sync and you have to handle all sorts of corner cases.
Additionally, please keep in mind that /etc/xdg/autostart
is for system-wide autorun entries. If it's supposed to be a per-user setting, you should create the .desktop file in the user's autostart directory. To determine its location, please follow the Desktop Application Autostart Specification, which mandates that the location be $XDG_CONFIG_DIRS/autostart
, which typically resolves to the .config/autostart
directory in the user's home (however, if the XDG_CONFIG_DIRS
environment variable exists, you should resolve it by reading that value first then appending /autostart
to it.)
Here is an example program that will print out what you want:
#include <cstdlib>
#include <iostream>
#include <QtCore/QString>
#include <QtCore/QDir>
#ifndef Q_OS_UNIX
#error This method only makes sense on Unix, use OS-specific handling for other OSes.
#endif
QString getUserXdgConfigDir()
{
QString result(std::getenv("XDG_CONFIG_DIRS"));
if (result.isEmpty()) {
// XDG_CONFIG_DIRS is not defined, we'll use the default value
// as mandated by http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
result = QDir::homePath() + QDir::separator() + ".config";
}
return result;
}
QString getUserAutostartDir()
{
return getUserXdgConfigDir() + QDir::separator() + "autostart";
}
int main(int argc, char *argv[])
{
std::cout << "User config dir is " << getUserXdgConfigDir().toStdString() << std::endl;
std::cout << "User autostart dir is " << getUserAutostartDir().toStdString() << std::endl;
return 0;
}
My solution is as follows. Write the user's preference as to startup at login in a file at, sat $HOME/.config/myapp.pref (the .config prevents dot-file creep). Then, make your application's auto invoke desktop entry add a command-line switch (for example, call myapp -a instead of myapp).
Then, in your program, if the switch -a is present, check your dot file to see if the user wants auto-startup. If the answer is yes, run normally. If the user does not want auto startup, have the program exit. Also, do this switch and dot file check before you put up any user-visible parts of your app.
For saving settings, use the QSettings class. Its really neat, and on windows it will store its values in the registry, so most likely it will be compatible with your registry entries you already use.
/ect is for system settings.
By startup do you mean login or system startup?
The simplest ways to save single(!) user configs in linux for a user is with a hidden text file in their home directory with the settings(in linux putting a "." in the front of a file name makes it a hidden file).
I think the slightly more proper modern way it way is to put a non hidden file under a folder with your apps name under the config directory directory. ie check if a $XDG_CONFIG_HOME is set if it is make a folder named after your application with your config files under it, otherwise make the folder under $HOME/.config. See http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
P.S. you usually manually read the settings file at the beginning of your application(just after parsing the command line arguments.
Note that is the "simple" way, Qt seems to have a class for dealing with simple dictionary style settings in a cross platform way this may be what you want. Or you could check if you set the startup and undo/do(toggle) that.
精彩评论