链接到Qt报告:QTBUG-53313
从QT5.5转到Qt5.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使用QT5.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" 使用Qt5.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" 这里有几个问题:
当使用QT5.5编译时,设置'filename‘将按照所有情况(a、b、c、d)的预期构造。大小写'a‘和'b’是ASSUMED_SYSTEM_CONFIG文件的ASSUMED_SYSTEM_CONFIG,因为ASSUMED_USER_CONFIG文件不存在。因此,“the_value”将正确地从设置文件中检索。
然而,当使用Qt5.6编译时,设置'filename‘似乎构造得不正确,例如'c’和'd‘(正确的路径被附加到"/home/user/Qt5.6/ 5.6 /gcc_64")。因此,不能从设置文件中检索“the_value”。
我没有覆盖Qt中的任何默认环境变量,Qt5.6是由Qt维护工具自动安装的。我知道我可以使用环境变量XDG_CONFIG_HOME来设置QSettings::SystemScope文件的绝对路径,但我认为我不应该这样做。
要重新声明主要问题,如何使用ASSUMED_SYSTEM_CONFIG文件(即QSettings::SystemScope)和QSettings?
还有其他人遇到过这种情况吗?我在两台不同的机器上测试过这个。
发布于 2016-05-20 16:08:40
这是预编译linux二进制文件的一个问题。如果从源代码编译Qt5.6,则可以通过编译带有标志'-sysconfdir=/etc‘的Qt来避免此问题。
如果这不是一个选项,则可以设置环境变量XDG_CONFIG_HOME。
https://stackoverflow.com/questions/37151813
复制相似问题