QSettings - ini 文件的位置在哪里?

发布于 2024-09-29 03:06:18 字数 343 浏览 6 评论 0原文

我正在使用 QSettings 将一些数据存储为 Windows 中的 ini 文件。 我想查看ini文件,但我不知道ini文件的位置在哪里。

这是我的代码:

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

我必须在哪里查看?或者我可能错过了将其写入文件的代码? QSettings 何时写入其值?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

一江春梦 2024-10-06 03:06:18

要打印设置文件的确切位置,请使用 QSettings 类的 fileName 方法。

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

控制台输出如下所示:

/home/user/.config/folderName/fileName.conf

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
只有影子陪我不离不弃 2024-10-06 03:06:18

我想你会在这里找到你想要的一切:http://doc .qt.io/archives/qt-4.7/qsettings.html

它是特定于平台的,请参见:

平台特定注释
存储应用程序设置的位置

您也可以将设置存储在文件中:

QSettings settings("/home/petra/misc/myapp.ini",
                QSettings::IniFormat);

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);
活雷疯 2024-10-06 03:06:18

QSettings 将位置更改保存到 QSettings.Scope 枚举QSettings 默认保存到本地范围。在 Linux 上,我在以下位置找到了本地设置:

~/.config/CompanyName/ApplicationName.conf

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

陌路终见情 2024-10-06 03:06:18

在 Linux 中,您可以使用此代码片段或将此行插入到您的主代码中,以便使用 python 查找文件的位置。

from PyQt5.QtCore import QSettings

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

它应该返回这样的输出。

/$HOME/.config/组织名称/应用名称.conf

来源

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

雪化雨蝶 2024-10-06 03:06:18

查看 QStandardPaths 类,它链接到多个标准路径,包括所有支持平台上的配置。 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);

共享配置目录、应用程序数据目录等中有配置文件的路径。

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.

泪是无色的血 2024-10-06 03:06:18

如果您创建QSettings而不提供任何特定路径,则ini文件将位于应用程序路径

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

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

但要小心:应用程序路径可能会改变:例如,如果您在调试模式下使用 Qt Creator 开发应用程序,则应用程序路径位于 /debug 子文件夹中。

如果您在发布模式下运行它,则应用程序路径位于 /release 子文件夹中。

当部署应用程序时,默认情况下,应用程序路径与可执行文件位于同一文件夹中(至少对于 Windows)。

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).

も星光 2024-10-06 03:06:18

在 Windows 上,如果不提供 ini 文件名,您将在注册表中找到数据。
使用此代码片段:

    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();

运行此代码后,您将看到输出:

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

现在,打开 regedit 工具并按照您列出的路径进行操作得到:1

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

初见 2024-10-06 03:06:18

在windows中路径如下:
C:\Users\用户名\AppData\Roaming\bbb

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

软甜啾 2024-10-06 03:06:18

在 Mac OSX 上,我在 ~/Library/Preferences 下找到了该文件

QSettings 类提供持久的独立于平台的应用程序设置。
用户通常希望应用程序能够记住其跨会话的设置(窗口大小和位置、选项等)。此信息通常存储在 Windows 上的系统注册表中,以及 Mac OS X 上的 XML 首选项文件中。在 Unix 系统上,由于缺乏标准,许多应用程序(包括 KDE 应用程序)使用 INI 文本文件

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文