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...
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 6 Posters 3.1k Views 2 Watching
  • 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 21 Apr 2021, 14:56 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();
    
    J 1 Reply Last reply 21 Apr 2021, 15:03
    0
    • F Francky033
      21 Apr 2021, 14:56

      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();
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 21 Apr 2021, 15:03 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 21 Apr 2021, 15:24 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();
        
        J 1 Reply Last reply 21 Apr 2021, 15:28
        0
        • F Francky033
          21 Apr 2021, 15:24

          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();
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 21 Apr 2021, 15:28 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 21 Apr 2021, 16:49 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
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 21 Apr 2021, 18:14 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 21 Apr 2021, 18:27 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?

                A 1 Reply Last reply 21 Apr 2021, 19:50
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 21 Apr 2021, 18:30 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
                    21 Apr 2021, 18:27

                    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?

                    A Offline
                    A Offline
                    artwaw
                    wrote on 21 Apr 2021, 19:50 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 21 Apr 2021, 22:00 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.

                      A 1 Reply Last reply 22 Apr 2021, 11:36
                      0
                      • J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 22 Apr 2021, 06:26 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
                        • F Francky033
                          21 Apr 2021, 22:00

                          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.

                          A Offline
                          A Offline
                          artwaw
                          wrote on 22 Apr 2021, 11:36 last edited by
                          #21

                          @Francky033 I don't have idea what's happening in your system. I'd try to debug the problem by hardcoding known path to the file though, like /Users/[username]/settings.ini or something.

                          For more information please re-read.

                          Kind Regards,
                          Artur

                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            Francky033
                            wrote on 22 Apr 2021, 23:07 last edited by
                            #22

                            Hello everyone and thanks for your help!

                            I finally discovered the problem: I had to put a settings.sync() before the settings.status();

                            While the sync function doesn't seem to have any effect under Windows, Android and linux, it seems necessary to use it under macOS.

                            The documentation is a bit vague on this: "This function is called automatically from QSettings's destructor and by the event loop at regular intervals, so you normally don't need to call it yourself."

                            My problem is solved and I hope this can give a clue to people in the same situation as me. Thanks to the Qt forum and its members!

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              Francky033
                              wrote on 24 Apr 2021, 11:25 last edited by
                              #23

                              Hello,

                              I am again confronted with the same problem. Everything was working fine and the next day, no directory was accessible for saving using this piece of code!
                              When the save box opens, I noticed that all the elements in the directory (including .ini files) are grayed out...
                              What I don't understand is the extremely capricious result of this function (which is so simple). One day it works, the next day it doesn't...

                              Is it a bug in Qt?

                              Have a nice day!

                              Francky033

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 24 Apr 2021, 18:58 last edited by
                                #24

                                Did you restart a build from scratch ?
                                Re-run macdeployqt on your app bundle ?

                                Apple has introduced several security feature with regard to accessing the user environnement which might trigger here.

                                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

                                19/24

                                21 Apr 2021, 22:00

                                • Login

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