Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unable to set path for QSettings generated config file



  • Hi.

    I encountered a problem while trying to set the location of the config file generated by QSettings. Here's the code:

    int main(int argc, char *argv[])
    {
        QCoreApplication::setOrganizationName("MyAppOrg");
        QCoreApplication::setOrganizationDomain("myapp.org");
        QCoreApplication::setApplicationName("MyApp");
    
        QCoreApplication a(argc, argv);
    
        QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, "/tmp");
        QSettings settings;
    
        qDebug() << settings.fileName();
    
        return a.exec();
    }
    

    As per the official docs, I call the static function before creating the QSettings object. The result however is the following (which is the default path, not what I provided):

    "/home/user/.config/MyAppOrg/MyApp.conf"
    

    No matter what path I try to set, the result above remains constant, never changes. I tried to set with SystemScope as well, but no change.

    If I try to use the following, still no joy:

    QSettings settings;
    settings.setPath(QSettings::IniFormat, QSettings::SystemScope, "/tmp");
    

    Could somebody else retry this to see if it truly is a bug? Or what am I doing wrong?

    I'm using:
    GCC 9.3.0
    Linux 5.4.38
    Qt 5.14.1

    Thanks in advance.



  • The default format of the constructor you use is QSettings::NativeFormat, so I think you should set that format.
    Or use QSettings::defaultFormat().
    And is there any reason you must set path this way?
    Can't you just pass the full path to the constructor as we usually do?

    QSettings settings("/tmp/MyApp.conf", QSettings::IniFormat);
    

  • Qt Champions 2019

    @PusRob Can you try to call setPath before you create QCoreApplication instance?



  • @jsulm I tried it, but I still get the same (wrong) result.



  • @Bonnie If I use the constructor, then it works. But I thought the static function should work as well, hence the question at the end: is this a bug?



  • @PusRob
    Did you try my suggestion?

    QSettings::setPath(QSettings::defaultFormat(), QSettings::UserScope, "/tmp");
    


  • @Bonnie I tried your solution as well, and it does seem to work with the default settings. Interesting. Maybe the documentation should be updated then. Thanks.

    p.s. Should I open a request ticket (to modifiy the docs) or bug report about this?



  • @PusRob
    Are you sure? I've tried in my Ubuntu.
    I copied your code from the top post, only changed one line:

    QSettings::setPath(QSettings::defaultFormat(), QSettings::UserScope, "/tmp");
    

    And the output is

    "/tmp/MyAppOrg/MyApp.conf"
    

Log in to reply