vincent
vincent

Reputation: 1390

Unix Qt 5.6 QSettings cannot find QSettings::SystemScope configuration file

Link to Qt Bug Report: QTBUG-53313

Moving from Qt 5.5 to Qt 5.6 presents an issue with QSettings where it cannot find the QSettings configuration file at the system scope. In main.cpp below, QSettings is initialized various ways, then its properties are queried:

// 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";
}

The ASSUMED_USER_CONFIG file does not exist on the system.

The ASSUMED_SYSTEM_CONFIG file does exist on the system and contains:

the_value = 42

Compiled with Qt 5.5, the program returns:

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" 

Compiled with Qt 5.6, the program returns:

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" 

There are a few issues here:

When compiled with Qt 5.5, the settings 'filename' is getting constructed as expected for all cases (a,b,c,d). Cases 'a' and 'b' are falling back to the ASSUMED_SYSTEM_CONFIG file, since the ASSUMED_USER_CONFIG file does not exist. Because of this, 'the_value' is correctly retrieved from the settings file.

However, when compiled with Qt 5.6, the settings 'filename' seems to be constructed incorrectly for cases 'c' and 'd' (the correct path is being appended to "/home/user/Qt5.6/5.6/gcc_64"). Because of this, 'the_value' cannot be retrieved from the settings file.

I have not overridden any default environment variables in Qt Creator and Qt 5.6 was installed automatically by the Qt Maintenance tool. I am aware that I can use the environment variable, XDG_CONFIG_HOME, to set the absolute path to the QSettings::SystemScope file, but I don't think I should have to do this.

To restate the primary issue, how can I use the ASSUMED_SYSTEM_CONFIG file (i.e. QSettings::SystemScope) with QSettings?

Has anyone else come up against this? I have tested this on two separate machines.

Upvotes: 3

Views: 1186

Answers (1)

vincent
vincent

Reputation: 1390

This is an issue for precompiled linux binaries. If you compile Qt 5.6 from source, you can avoid this issue by compiling Qt with flag, '-sysconfdir=/etc'.

If this is not an option, you can set the environment variable, XDG_CONFIG_HOME.

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

Upvotes: 1

Related Questions