gpt4 book ai didi

c++ - Unix Qt 5.6 QSettings 找不到 QSettings::SystemScope 配置文件

转载 作者:太空狗 更新时间:2023-10-29 22:59:21 24 4
gpt4 key购买 nike

Qt 错误报告链接:QTBUG-53313

从 Qt 5.5 迁移到 Qt 5.6 会出现一个问题 QSettings它在系统范围内找不到 QSettings 配置文件。在下面的 main.cpp 中,QSettings 以各种方式初始化,然后查询其属性:

// File: main.cpp

#include <QApplication>
#include <QDebug>
#include <QSettings>
#include <QCoreApplication>

#define ASSUMED_SYSTEM_CONFIG "/etc/xdg/TheOrg/TheApp.conf"
#define ASSUMED_USER_CONFIG "/home/user/.config/TheOrg/TheApp.conf"

void print_info(const QSettings& settings, const char& use_case);

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QCoreApplication::setOrganizationName("TheOrg");
QCoreApplication::setApplicationName("TheApp");

QSettings settings_a;
QSettings settings_b("TheOrg", "TheApp");
QSettings settings_c(QSettings::SystemScope, "TheOrg", "TheApp");
QSettings settings_d(QSettings::NativeFormat, QSettings::SystemScope, "TheOrg", "TheApp");

print_info(settings_a, 'a');
print_info(settings_b, 'b');
print_info(settings_c, 'c');
print_info(settings_d, 'd');

return a.exec();
}

void print_info(const QSettings& settings, const char& use_case)
{
int value = settings.value("the_value").toInt();

qDebug() << "Using case (" << use_case << ")";

qDebug() << "The value is " << value;

qDebug() << "Settings scope is: " << ((settings.scope() == QSettings::SystemScope) ? "System" : "User");
qDebug() << "Settings organization is: " << settings.organizationName();
qDebug() << "Settings application name is: " << settings.applicationName();
qDebug() << "Settings fallbackEnabled is: " << settings.fallbacksEnabled();
qDebug() << "Settings filename is: " << settings.fileName() << "\n";
}

系统上ASSUMED_USER_CONFIG文件不存在

ASSUMED_SYSTEM_CONFIG 文件确实存在在系统上并且包含:

the_value = 42

用Qt 5.5编译,程序返回:

Using case ( a )
The value is 42
Settings scope is: User
Settings organization is: "TheOrg"
Settings application name is: "TheApp"
Settings fallbackEnabled is: true
Settings filename is: "/home/user/.config/TheOrg/TheApp.conf"

Using case ( b )
The value is 42
Settings scope is: User
Settings organization is: "TheOrg"
Settings application name is: "TheApp"
Settings fallbackEnabled is: true
Settings filename is: "/home/user/.config/TheOrg/TheApp.conf"

Using case ( c )
The value is 42
Settings scope is: System
Settings organization is: "TheOrg"
Settings application name is: "TheApp"
Settings fallbackEnabled is: true
Settings filename is: "/etc/xdg/TheOrg/TheApp.conf"

Using case ( d )
The value is 42
Settings scope is: System
Settings organization is: "TheOrg"
Settings application name is: "TheApp"
Settings fallbackEnabled is: true
Settings filename is: "/etc/xdg/TheOrg/TheApp.conf"

用Qt 5.6编译,程序返回:

Using case ( a )
The value is 0
Settings scope is: User
Settings organization is: "TheOrg"
Settings application name is: "TheApp"
Settings fallbackEnabled is: true
Settings filename is: "/home/user/.config/TheOrg/TheApp.conf"

Using case ( b )
The value is 0
Settings scope is: User
Settings organization is: "TheOrg"
Settings application name is: "TheApp"
Settings fallbackEnabled is: true
Settings filename is: "/home/user/.config/TheOrg/TheApp.conf"

Using case ( c )
The value is 0
Settings scope is: System
Settings organization is: "TheOrg"
Settings application name is: "TheApp"
Settings fallbackEnabled is: true
Settings filename is: "/home/user/Qt5.6/5.6/gcc_64/etc/xdg/TheOrg/TheApp.conf"

Using case ( d )
The value is 0
Settings scope is: System
Settings organization is: "TheOrg"
Settings application name is: "TheApp"
Settings fallbackEnabled is: true
Settings filename is: "/home/user/Qt5.6/5.6/gcc_64/etc/xdg/TheOrg/TheApp.conf"

这里有几个问题:

当使用 Qt 5.5 编译时,设置“文件名”在所有情况下都按预期构建(a、b、c、d)。案例 'a' 和 'b' 是 falling back到 ASSUMED_SYSTEM_CONFIG 文件,因为 ASSUMED_USER_CONFIG 文件不存在。因此,可以从设置文件中正确检索“the_value”。

但是,当使用 Qt 5.6 编译时,设置“文件名”似乎对于情况“c”和“d”的构造不正确(正确的路径被附加到“/home/user/Qt5.6/5.6/gcc_64”)。因此,无法从设置文件中检索“the_value”。

我没有覆盖 Qt Creator 中的任何默认环境变量,Qt 5.6 是由 Qt 维护工具自动安装的。我知道我可以使用环境变量 XDG_CONFIG_HOME 来设置 QSettings::SystemScope 文件的绝对路径,但我认为我不必这样做。

重申主要问题我如何将 ASSUMED_SYSTEM_CONFIG 文件(即 QSettings::SystemScope)与 QSettings 一起使用?

还有其他人反对吗?我已经在两台不同的机器上对此进行了测试。

最佳答案

这是预编译 Linux 二进制文件的问题。如果您从源代码编译 Qt 5.6,则可以通过使用标志“-sysconfdir=/etc”编译 Qt 来避免此问题。

如果这不是一个选项,您可以设置环境变量 XDG_CONFIG_HOME。

This bug should now be fixed in the Qt 5.6.1 snapshot .

关于c++ - Unix Qt 5.6 QSettings 找不到 QSettings::SystemScope 配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37151813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com