Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unable to set path for QSettings generated config file

Unable to set path for QSettings generated config file

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PusRob
    wrote on last edited by PusRob
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      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);
      
      P 1 Reply Last reply
      3
      • P PusRob

        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.

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        1
        • jsulmJ jsulm

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

          P Offline
          P Offline
          PusRob
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • B Bonnie

            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);
            
            P Offline
            P Offline
            PusRob
            wrote on last edited by
            #5

            @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?

            B 1 Reply Last reply
            0
            • P PusRob

              @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?

              B Offline
              B Offline
              Bonnie
              wrote on last edited by
              #6

              @PusRob
              Did you try my suggestion?

              QSettings::setPath(QSettings::defaultFormat(), QSettings::UserScope, "/tmp");
              
              P 1 Reply Last reply
              0
              • B Bonnie

                @PusRob
                Did you try my suggestion?

                QSettings::setPath(QSettings::defaultFormat(), QSettings::UserScope, "/tmp");
                
                P Offline
                P Offline
                PusRob
                wrote on last edited by PusRob
                #7

                @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?

                B 1 Reply Last reply
                0
                • P PusRob

                  @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?

                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  @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"
                  
                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved