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. Settings and macOS. Impossible to create a ini file...
QtWS25 Last Chance

Settings and macOS. Impossible to create a ini file...

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 3.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.
  • F Offline
    F Offline
    Francky033
    wrote on last edited by Francky033
    #1

    Hello,

    I have a problem with Qt 5.15.2 and os X. When I try to save an ini file, the Settings.status() returns QSettings::NoError but I don't have any file in the indicated directory!

    Adding settings.sync() doesn't change anything...

    How to do ?

    Thanks !

    QString p = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
    
    QSettings settings(p + "/test.ini", QSettings::IniFormat);
    
    settings.setPath(QSettings::IniFormat, QSettings::UserScope, p);
    
    settings.beginGroup("Data");
    
    settings.setValue("a1", 0.0);
    settings.setValue("a2", 0.0);
    
    settings.endGroup();
    
    return settings.status();
    
    1 Reply Last reply
    0
    • nageshN Offline
      nageshN Offline
      nagesh
      wrote on last edited by
      #2

      @Francky033 pls read the documentation for the locations where does this ini files get's stored.
      https://doc.qt.io/qt-5/qsettings.html#platform-specific-notes

      QSettings constructor is using filename not the file path..

      If format is QSettings::IniFormat, fileName (first parameter) is the name of an INI file.
      https://doc.qt.io/qt-5/qsettings.html#QSettings-3

      QSettings settings(p + "/test.ini", QSettings::IniFormat);
      

      If you want to set the path for ini files set it before the object creation
      https://doc.qt.io/qt-5/qsettings.html#setPath
      read the warning mentioned in the Qt doc

      Warning: This function doesn't affect existing QSettings objects.
      

      @Francky033 here in the code QSettings object is created and then set the path for ini file.
      use QSettings::setPath() before the object creation.

      1 Reply Last reply
      3
      • F Offline
        F Offline
        Francky033
        wrote on last edited by Francky033
        #3

        Hello nagesh,

        I followed your instructions and looked at the doc.

        I put the setPath before the creation of the file and I use the name of the file in file without the path but I have the same result.
        In the log, I have a strange error message that tells me that the file is in read mode : Error opening for read : "No such file or directory".

        There is no problem with the file name or directory. Here, I try to write in the directory /users/user_name/Library/preferences

        Everything works fine when I launch the program from qtcreator, but it doesn't work anymore when I install the application. There is clearly a permissions problem...

        QString p = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
        
        QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, p);
        
        QSettings settings("test.ini", QSettings::IniFormat);
        
        settings.beginGroup("Data");
        
        settings.setValue("a1", 0.0);
        settings.setValue("a2", 0.0);
        
        settings.endGroup();
        
        return settings.status();
        
        jsulmJ 1 Reply Last reply
        0
        • F Francky033

          Hello nagesh,

          I followed your instructions and looked at the doc.

          I put the setPath before the creation of the file and I use the name of the file in file without the path but I have the same result.
          In the log, I have a strange error message that tells me that the file is in read mode : Error opening for read : "No such file or directory".

          There is no problem with the file name or directory. Here, I try to write in the directory /users/user_name/Library/preferences

          Everything works fine when I launch the program from qtcreator, but it doesn't work anymore when I install the application. There is clearly a permissions problem...

          QString p = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
          
          QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, p);
          
          QSettings settings("test.ini", QSettings::IniFormat);
          
          settings.beginGroup("Data");
          
          settings.setValue("a1", 0.0);
          settings.setValue("a2", 0.0);
          
          settings.endGroup();
          
          return settings.status();
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Francky033 said in Settings and macOS. Impossible to create a ini file...:

          tells me that the file is in read mode : Error opening for read : "No such file or directory".

          It actually tells that the file does not exist.
          You should check what the whole path looks like (value of variable p).

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

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Francky033
            wrote on last edited by
            #5

            Ah, I think I found the problem. The directory QStandardPaths::HomeLocation doesn't seem to be writable. But if I use QStandardPaths::AppLocalDataLocation. Everything works !

            But QStandardPaths::writableLocation(QStandardPaths::HomeLocation) should allow write access. Why is this not the case?

            jsulmJ SGaistS 2 Replies Last reply
            0
            • F Francky033

              Ah, I think I found the problem. The directory QStandardPaths::HomeLocation doesn't seem to be writable. But if I use QStandardPaths::AppLocalDataLocation. Everything works !

              But QStandardPaths::writableLocation(QStandardPaths::HomeLocation) should allow write access. Why is this not the case?

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

              @Francky033 said in Settings and macOS. Impossible to create a ini file...:

              Why is this not the case?

              Could be a bug in Qt. You can check Qt bug tracker and file a bug if it is not already reported.

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

              1 Reply Last reply
              0
              • F Francky033

                Ah, I think I found the problem. The directory QStandardPaths::HomeLocation doesn't seem to be writable. But if I use QStandardPaths::AppLocalDataLocation. Everything works !

                But QStandardPaths::writableLocation(QStandardPaths::HomeLocation) should allow write access. Why is this not the case?

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                @Francky033 said in Settings and macOS. Impossible to create a ini file...:

                Ah, I think I found the problem. The directory QStandardPaths::HomeLocation doesn't seem to be writable. But if I use QStandardPaths::AppLocalDataLocation. Everything works !

                But QStandardPaths::writableLocation(QStandardPaths::HomeLocation) should allow write access. Why is this not the case?

                What exact path are you getting that fails ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  Francky033
                  wrote on last edited by Francky033
                  #8
                  • What exact path are you getting that fails ?

                  /Users/my_user_name
                  ~/Library/Preferences/

                  Another strange point. The save only works if I write :

                  QSettings settings(file, QSettings::IniFormat);
                  (with file containing the complete path )

                  and if in the setPath I use the full path of the file rather than the directory in which I want to save :

                  QString p = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation);
                  
                  QString file = p +"/test.ini";
                  
                  QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, file);
                  
                  QSettings settings(file, QSettings::IniFormat);
                  
                  settings.beginGroup("Data");
                  
                  settings.setValue("a1", 0.0);
                  settings.setValue("a2", 0.0);
                  
                  settings.endGroup();
                  
                  return settings.status();
                  

                  This code works but I don't know why it works in fact.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    Francky033
                    wrote on last edited by
                    #9

                    No, in fact, it does not work anymore.
                    I always get the message "No such file or directory" when I run the application in a terminal but the result of qsettins::status() is No::ERROR

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Francky033
                      wrote on last edited by
                      #10

                      In fact, my code is a bit more complicated. I'm calling a getSaveFileName box just before. Could this have an effect?

                      QString file = QFileDialog::getSaveFileName(
                      		this, tr("Save a configuration file..."), QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation),
                      		"configuration file (*.ini)");
                      
                      QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, file);
                      
                      QSettings settings(file, QSettings::IniFormat);
                      
                      settings.beginGroup("Data");
                      
                      settings.setValue("a1", 0.0);
                      settings.setValue("a2", 0.0);
                      
                      settings.endGroup();
                      
                      return settings.status();
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • F Francky033

                        In fact, my code is a bit more complicated. I'm calling a getSaveFileName box just before. Could this have an effect?

                        QString file = QFileDialog::getSaveFileName(
                        		this, tr("Save a configuration file..."), QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation),
                        		"configuration file (*.ini)");
                        
                        QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, file);
                        
                        QSettings settings(file, QSettings::IniFormat);
                        
                        settings.beginGroup("Data");
                        
                        settings.setValue("a1", 0.0);
                        settings.setValue("a2", 0.0);
                        
                        settings.endGroup();
                        
                        return settings.status();
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Francky033 said in Settings and macOS. Impossible to create a ini file...:

                        QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, file);

                        Shouldn't you actually provide a path to a folder, not to a file? So, using setPath you set the folder where ini files should be stored, then if you create a QSettings instance you pass the name of an ini file which then stored in the folder specified by setPath.

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

                        1 Reply Last reply
                        1
                        • F Offline
                          F Offline
                          Francky033
                          wrote on last edited by
                          #12

                          I modified my code this way but it still doesn't work...

                          QString file = QFileDialog::getSaveFileName(
                          		this, tr("Save a configuration file..."), QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation), "configuration file (*.ini)");
                          
                          QString d = file.left(file .lastIndexOf("/"));
                          
                          QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, d);
                          
                          QSettings settings(file, QSettings::IniFormat);
                          
                          settings.beginGroup("Data");
                          
                          settings.setValue("a1", 0.0);
                          settings.setValue("a2", 0.0);
                          
                          settings.endGroup();
                          
                          return settings.status();
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • F Francky033

                            I modified my code this way but it still doesn't work...

                            QString file = QFileDialog::getSaveFileName(
                            		this, tr("Save a configuration file..."), QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation), "configuration file (*.ini)");
                            
                            QString d = file.left(file .lastIndexOf("/"));
                            
                            QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, d);
                            
                            QSettings settings(file, QSettings::IniFormat);
                            
                            settings.beginGroup("Data");
                            
                            settings.setValue("a1", 0.0);
                            settings.setValue("a2", 0.0);
                            
                            settings.endGroup();
                            
                            return settings.status();
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @Francky033 said in Settings and macOS. Impossible to create a ini file...:

                            QSettings settings(file, QSettings::IniFormat);

                            file should be only file name here.

                            "QString d = file.left(file .lastIndexOf("/"));" - use QFileInfo::absolutePath() to get the folder without file name.

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

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              Francky033
                              wrote on last edited by
                              #14

                              jsulm, I modified my code this way, but no effect

                              QString file = QFileDialog::getSaveFileName(
                              		this, tr("Save a configuration file..."), QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation), "configuration file (*.ini)");
                              
                              QFileInfo f(file);
                              
                              QString d = f.absoluteFilePath();
                              QString name = f.fileName();
                              
                              QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, d);
                              
                              QSettings settings(f, QSettings::IniFormat);
                              
                              settings.beginGroup("Data");
                              
                              settings.setValue("a1", 0.0);
                              settings.setValue("a2", 0.0);
                              
                              settings.endGroup();
                              
                              return settings.status();
                              
                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                But why are you using setPath at all ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • F Offline
                                  F Offline
                                  Francky033
                                  wrote on last edited by
                                  #16

                                  I removed the setPath and I still have the same result. When I start the program from qtcreator, everything works. But it does not work anymore when the program is installed on the OS.
                                  This seems to me to be a permissions problem. But how to solve this?

                                  artwawA 1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    Did you used macdeployqt before running it outside of Qt ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • F Francky033

                                      I removed the setPath and I still have the same result. When I start the program from qtcreator, everything works. But it does not work anymore when the program is installed on the OS.
                                      This seems to me to be a permissions problem. But how to solve this?

                                      artwawA Offline
                                      artwawA Offline
                                      artwaw
                                      wrote on last edited by
                                      #18

                                      @Francky033 If it is permission problem OS should ask user upon the startup if read/write permission to folder should be granted (like it happens every time I deploy my software).
                                      It's ok if you run it from QtCreator as child process of QtCreator is allowed standard access to the home tree.

                                      For more information please re-read.

                                      Kind Regards,
                                      Artur

                                      1 Reply Last reply
                                      0
                                      • F Offline
                                        F Offline
                                        Francky033
                                        wrote on last edited by
                                        #19

                                        SGaist : Yes !

                                        artwaw : But if it is not a problem of permissions, where can the problem come from? What also surprises me is the message "No such file or directory" that appears in the log when the settings variable is created and the NoERROR as result of settings::status() at the end of the record.

                                        artwawA 1 Reply Last reply
                                        0
                                        • J.HilkJ Offline
                                          J.HilkJ Offline
                                          J.Hilk
                                          Moderators
                                          wrote on last edited by J.Hilk
                                          #20

                                          I believe you've been let a stray, this code is taken from my working application:

                                          QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +QString("/");
                                          
                                          QString settingsPath = appDataPath + QString("data/Settings.ini");
                                          
                                          //And used this way
                                          auto settings = new QSettings(settingsPath(), QSettings::IniFormat);
                                          

                                          this results in a directory path

                                          qDebug() << Q_FUNC_INFO << settings->fileName();
                                          

                                          "/Users/jonashilk/Library/Application Support/CompanyName/Applicationname/data/Settings.ini"


                                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                          Q: What's that?
                                          A: It's blue light.
                                          Q: What does it do?
                                          A: It turns blue.

                                          1 Reply Last reply
                                          3

                                          • Login

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