QSettings - where is the location of the ini file?
Asked Answered
S

9

46

I'm using QSettings to store some data as ini file in Windows. I want to see the ini file, but I don't know what is the location of the ini file.

This is my code:

QSettings *set = new QSettings(QSettings::IniFormat, QSettings::UserScope, "bbb", "aaa");
set->setValue("size", size());
set->setValue("pos", pos());

Where do I have to look? Or may be I miss the code which write it to the file? When does the QSettings write its values?

Syphilology answered 27/10, 2010 at 9:47 Comment(0)
F
51

To print out the exact location of your settings file use method fileName method of QSettings class.

QSettings settings("folderName", "fileName");
qDebug() << settings.fileName();

Console output looks then like:

/home/user/.config/folderName/fileName.conf
Fakery answered 22/7, 2013 at 14:30 Comment(2)
The base path (without the filename) can also be retrieved via QStandardPaths::writableLocation() using QStandardPaths::ConfigLocation as the typeCarbazole
YES!. This answer is bestest.Kobayashi
E
11

I think you'll find everything you're looking for here : http://doc.qt.io/archives/qt-4.7/qsettings.html

It's plateform specific, see under :

Platform-Specific Notes Locations Where Application Settings Are Stored

You can store Settings in files as well :

QSettings settings("/home/petra/misc/myapp.ini",
                QSettings::IniFormat);
Ebner answered 27/10, 2010 at 10:6 Comment(0)
G
10

QSettings save location changes to the QSettings.Scope enum. QSettings save to the Local scope by default. On Linux, I found my local settings in:

~/.config/CompanyName/ApplicationName.conf

Giddy answered 19/6, 2013 at 19:41 Comment(1)
Yes! This is what I wanted to know - QSettings settings; - without any paths. Like was here: https://mcmap.net/q/373337/-qsettings-file-chooser-should-remember-the-last-directoryHang
C
6

In linux you can use this snippet or insert this lines into your main code for find location of your file with python.

from PyQt5.QtCore import QSettings

settings = QSettings("Organization Name", "App name")
print(QSettings.fileName(settings))

It should return an output like this.

/$HOME/.config/Organization Name/App name.conf

Source

Comnenus answered 19/3, 2020 at 1:53 Comment(0)
I
5

Check out the QStandardPaths class, it links to multiple standard paths including configuration on all supported platforms. https://doc.qt.io/qt-5/qstandardpaths.html

QT >= 5.5:

QString path = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);

QT < 5.5:

QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);

There are paths for config files in shared config directories, application data directories, and more.

Incontestable answered 7/11, 2017 at 18:2 Comment(0)
U
4

If you create a QSettings without giving any specific path, the ini file will be located in the application path.

QSettings Settings("myapp.ini", QSettings::IniFormat);
Settings.setValue("Test", "data");

//...
qDebug() << QApplication::applicationDirPath();

Be careful though : the application path might change : for instance, if you are developping your app with Qt Creator, in debug mode, the application path is in the /debug subfolder.

If you are running it in release mode, the application path is in the /release subfolder.

And when your application is deployed, by default, the application path is in the same folder as the executable (at least for Windows).

Unilocular answered 28/10, 2010 at 6:40 Comment(2)
I'm not sure that this really works, I've tried it but I could not find the settings in the application path.Afterbrain
QApplication:applicationDirPath() is usually not writable, so very unlikely to be used as the location for file created at runtime. Maybe you meant the application's current working directory?Carbazole
J
3

On Windows without providing an ini filename, you'll find the data in the registry. Using this code snippet:

    int red = color.red();
    int green = color.green();
    int blue = color.blue();
    QSettings settings("Joe", "SettingsDemo");
    qDebug() << settings.fileName();
    settings.beginGroup("ButtonColor");
    settings.setValue("button1r", red);
    settings.setValue("button1g", green);
    settings.setValue("button1b", blue);
    settings.endGroup();

After running this code, you'll see the output:

"\\HKEY_CURRENT_USER\\Software\\Joe\\SettingsDemo"

Now, opening the regedit tool and following the path list you got: 1

Jonell answered 30/4, 2020 at 15:43 Comment(0)
H
2

in windows path is like below: C:\Users\user_name\AppData\Roaming\bbb

Henchman answered 28/7, 2019 at 11:43 Comment(0)
S
0

On Mac OSX, I found the file under at ~/Library/Preferences

The QSettings class provides persistent platform-independent application settings. Users normally expect an application to remember its settings (window sizes and positions, options, etc.) across sessions. This information is often stored in the system registry on Windows, and in XML preferences files on Mac OS X. On Unix systems, in the absence of a standard, many applications (including the KDE applications) use INI text files

http://doc.qt.io/archives/qt-4.7/qsettings.html

Selfaddressed answered 16/2, 2017 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.