saving configs in qt linux
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;
}
Qt - How to save a configuration file on multiple platforms
Depends on the settings you want to record, but I would suggest to use QSettings.
Where does Qt Creator save its settings?
See QtCreator Quick Tour.
Qt Creator creates the following files and directories:
QtCreator.db
QtCreator.ini
qtversion.xml
toolChains.xml
qtcreator
qtc-debugging-helper
qtc-qmldump
The location depends on the platform. On Linux and other Unix
platforms, the files are located in~/.config/QtProject
and
~/.local/share/data/QtProject/qtcreator
.On Mac OS, the files are located in
~/.config/QtProject
and
~/Library/Application Support/QtProject/Qt Creator
.
On Windows in general, the files are located in %APPDATA%\QtProject
and %LOCALAPPDATA%\QtProject
. Yes, you can use these paths in Explorer and in various command line shells. The environment variables (between the %
-signs) are expanded automatically. If you need the concrete paths, see below.
On Windows 10, 8, Vista and 7, the files are located in
<drive>:\Users\<username>\AppData\Roaming\QtProject
and
<drive>:\Users\<username>\AppData\Local\QtProject
.On Windows XP, the files are located in
<drive>:\Documents and Settings\<username>\Application Data\QtProject
and<drive>:\Documents and Settings\<username>\Local Settings\Application Data\QtProject
.
Where are Qt Creator Build & Run settings stored?
As far as I know that information is stored in the file xxx.pro.user
project settings
I have a GUI for my program that saves a config file, how can I double click on this config file to load the GUI directly with the config data?
There is probably more than one way to implement this feature. Here is what I would do:
- Make a unique file type for the config file (a dot extension like .myappconf or something like that should do it)
- Create a file association for your application with that file type. This is typically done in the installer or in the .desktop file when you are on Linux.
- Now with this association the action of double clicking the file will trigger your Operating System to run a new copy of your app and present it with the config file (via a command line argument).
- The application should check on startup if there is already a copy of it running. If so, it should send a notification about the config file to the old copy and then self-terminate.
Note: If you are okay with the double click action starting up a new copy instead of loading the file into the old one (if any), you can skip the last step.
What's the standard approach for configuration in Qt console apps?
You could use QSettings
for this. Please refer to the documentation for details:
http://qt-project.org/doc/qt-5.1/qtcore/qsettings.html
You could always use other formats as well like XML, Json, and so forth, but generically speaking, QSettings is the way, or if you are writing a KDE application, then probably KConfig
.
These are the two important methods you need to be aware of when dealing with QSettings for reading and writing:
Reading
QVariant QSettings::value(const QString & key,
const QVariant & defaultValue = QVariant()) const
Writing
void QSettings::setValue(const QString & key, const QVariant & value)
Then, you can simply stick to the native format (or even ini on your Linux if you prefer):
QSettings::NativeFormat 0 Store the settings using the most
appropriate storage format for the platform. On Windows, this means
the system registry; on Mac OS X, this means the CFPreferences API; on
Unix, this means textual configuration files in INI format.
Here you can find an example for your convenience:
#include <QSettings>
int main()
{
....
QSettings settings("Foo", "Bar");
// settings.beginGroup("application");
QString string = settings.value("foo", "bar");
// settings.endGroup();
....
}
Note, the groups are optional, and it depends on your exact purpose. You can group settings that way to keep certain ones encapsulated.
This may also be important for you to know as per documentation:
On Unix systems, if the file format is NativeFormat, the following files are used by default:
$HOME/.config/MySoft/Star Runner.conf (Qt for Embedded Linux: $HOME/Settings/MySoft/Star Runner.conf)
$HOME/.config/MySoft.conf (Qt for Embedded Linux: $HOME/Settings/MySoft.conf)
/etc/xdg/MySoft/Star Runner.conf
/etc/xdg/MySoft.conf
QBS profiles are not saved and updated at any time
Do not try to attempt to amend qbs profiles derived from Qt Creator kits "from the outside" -- that will not work reliably, as you've discovered. These profiles can get regenerated at any time. Instead, go to Tools -> Options -> Kits and do your changes via the "Additional Qbs Properties" entry in the kit dialog. These changes will persist.
QSettings persistence constantly writing to config file on initial startup
So I finally figured out what the was going wrong. We have a timer that shows whether certain Can Bus devices are present. If they are not present we hide the icons and log out the user directly through calling the onUserLoggedOut() method and not through a connect statement. I was constantly searching for a timer and connect statement and not a direct method call nested within a switch that uses a timer.
How to load settings in Qt app with QSettings
It depends on the way you will use your settings file. Do you want to allow the user of your application to dynamically change the settings in the file (.ini file for example) ? Or the settings have to be set by the GUI ?
If you are using some GUI to change the settings, I advice you you to load the main settings at the start of your application from a static class for example.
void SettingsManager::loadSettings()
{
// .ini format example
QSettings settings(FileName, QSettings::IniFormat);
IntegerSetting = settings.value("SettingName", default).toInt();
BooleanSetting = settings.value("SettingName", default).toBool();
// ...
}
Then, there is no problem to save your changed values on-demand because of the QSettings optimization.
/**
* key is your setting name
* variant is your value (could be string, integer, boolean, etc.)
*/
void SettingsManager::writeSetting(const QString &key, const QVariant &variant)
{
QSettings settings(FileName, QSettings::IniFormat);
settings.setValue(key, variant);
}
Related Topics
Is It Efficient to Use Epoll with Devices (/Dev/Event/...)
How to Modify The Linux Kernel to Change The Version String That Uname Returns
How to Use Xargs to Run a Function in a Command Substitution for Each Match
Get The Name of The Directory Where a Script Is Executed
How to Use Multiple Lower Layers in Overlayfs
How Does One Override an Existing Zsh Keyboard Completion
How to Get Complete Stack Dump from Profiler in Every Sample for Use in Flame Graph
X11 Forwarding Through Google Colab
How to Get "Instant" Output of "Tail -F" as Input
Accept Multiple Lines of Input in a Bash Script
Why Do Es and Ds Zero Out Eventually on 64 Bit Kernel When Set to Tls Selectors
Why Does The Stack Have to Be Page Aligned
Which Is Faster of Two Case or If