34

QSettings用来在 Windows 中将一些数据存储为 ini 文件。我想看ini文件,但不知道ini文件的位置是什么。

这是我的代码:

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

我必须在哪里看?或者我可能会错过将其写入文件的代码?什么时候QSettings写它的值?

4

9 回答 9

44

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

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

控制台输出看起来像:

/home/user/.config/folderName/fileName.conf
于 2013-07-22T14:30:07.463 回答
12

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

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

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

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

QSettings settings("/home/petra/misc/myapp.ini",
                QSettings::IniFormat);
于 2010-10-27T10:06:21.657 回答
10

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

~/.config/CompanyName/ApplicationName.conf

于 2013-06-19T19:41:22.110 回答
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).

于 2010-10-28T06:40:07.630 回答
3

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

from PyQt5.QtCore import QSettings

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

它应该返回这样的输出。

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

资源

于 2020-03-19T01:53:47.390 回答
3

查看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);

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

于 2017-11-07T18:02:24.317 回答
1

在 windows 路径如下: C:\Users\user_name\AppData\Roaming\bbb

于 2019-07-28T11:43:44.657 回答
0

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

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

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

于 2017-02-16T09:45:14.920 回答
0

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

    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

于 2020-04-30T15:43:57.543 回答