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. Why can't QSettings be written to QTemporaryFile
Forum Updated to NodeBB v4.3 + New Features

Why can't QSettings be written to QTemporaryFile

Scheduled Pinned Locked Moved Solved General and Desktop
36 Posts 6 Posters 3.3k Views 3 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.
  • T Offline
    T Offline
    tovax
    wrote on 9 Oct 2021, 01:29 last edited by tovax 10 Sept 2021, 02:16
    #1

    Hi, all
    I want to encrypt the contents of the INI file. My idea is to first write the contents of qsettings to the temporary file, then encrypt the contents of the temporary file, and finally write to the INI file. But I found that writing qsettings to the temporary file failed. Could you help me please?
    Best regards!

    void JCDemoAES::test()
    {
            // Open temp file
            tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
            tmpFile->open();
        
            // Settings
            QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
            settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
            settings.beginGroup("GroupTest");
        
            QStringList list;
            list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
            settings.setValue("UserCode", list);
            settings.setValue("UserCodeDefault", QStringLiteral("1234"));
        
            settings.endGroup();
        
            // Close temp file
            qDebug() << __FUNCTION__ << tmpFile->readAll();
            tmpFile->close();
    }
    
    P 1 Reply Last reply 9 Oct 2021, 03:24
    0
    • T Offline
      T Offline
      tovax
      wrote on 9 Oct 2021, 02:37 last edited by
      #2

      Debug output: JCDemoAES::test ""

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jeremy_k
        wrote on 9 Oct 2021, 03:13 last edited by
        #3

        There's no call to QSettings::sync, and the QSettings object isn't destroyed until after attempting to read. The settings may not have been flush to the file yet.

        Waiting until after the sync completes to open the file for reading may also be necessary.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        T 1 Reply Last reply 9 Oct 2021, 03:28
        2
        • T tovax
          9 Oct 2021, 01:29

          Hi, all
          I want to encrypt the contents of the INI file. My idea is to first write the contents of qsettings to the temporary file, then encrypt the contents of the temporary file, and finally write to the INI file. But I found that writing qsettings to the temporary file failed. Could you help me please?
          Best regards!

          void JCDemoAES::test()
          {
                  // Open temp file
                  tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                  tmpFile->open();
              
                  // Settings
                  QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                  settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                  settings.beginGroup("GroupTest");
              
                  QStringList list;
                  list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                  settings.setValue("UserCode", list);
                  settings.setValue("UserCodeDefault", QStringLiteral("1234"));
              
                  settings.endGroup();
              
                  // Close temp file
                  qDebug() << __FUNCTION__ << tmpFile->readAll();
                  tmpFile->close();
          }
          
          P Offline
          P Offline
          Pl45m4
          wrote on 9 Oct 2021, 03:24 last edited by
          #4

          @tovax

          Double check what tempFile->fileName() returns and if it really matches your complete file path.

          Can you confirm the creation of such file?

          • https://doc.qt.io/qt-5/qtemporaryfile.html#fileName

          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          T 1 Reply Last reply 9 Oct 2021, 03:34
          0
          • J jeremy_k
            9 Oct 2021, 03:13

            There's no call to QSettings::sync, and the QSettings object isn't destroyed until after attempting to read. The settings may not have been flush to the file yet.

            Waiting until after the sync completes to open the file for reading may also be necessary.

            T Offline
            T Offline
            tovax
            wrote on 9 Oct 2021, 03:28 last edited by
            #5

            @jeremy_k Hi, thank you very much! I modified the code, but I still can't write it successfully.

            Debug output:
            JCDemoAES::test "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.sEbGkR"
            JCDemoAES::test ""

            void JCDemoAES::test()
            {
                // Init temp file
                tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                tmpFile->open();
                qDebug() << __FUNCTION__ << tmpFile->fileName();
                tmpFile->close();
            
                // Settings
                QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                settings.beginGroup("GroupTest");
            
                QStringList list;
                list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                settings.setValue("UserCode", list);
                settings.setValue("UserCodeDefault", QStringLiteral("1234"));
            
                settings.sync();
                settings.endGroup();
            
                // Read temp file
                tmpFile->open();
                qDebug() << __FUNCTION__ << tmpFile->readAll();
                tmpFile->close();
            }
            
            J 1 Reply Last reply 9 Oct 2021, 03:41
            0
            • P Pl45m4
              9 Oct 2021, 03:24

              @tovax

              Double check what tempFile->fileName() returns and if it really matches your complete file path.

              Can you confirm the creation of such file?

              • https://doc.qt.io/qt-5/qtemporaryfile.html#fileName
              T Offline
              T Offline
              tovax
              wrote on 9 Oct 2021, 03:34 last edited by
              #6

              @Pl45m4 Hi, thanks!
              The temporary file has indeed been successfully created. I can see it in the folder.

              Debug output:
              JCDemoAES::test 1 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.LBTGmC"
              JCDemoAES::test 2 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.LBTGmC"
              JCDemoAES::test 3 ""

              void JCDemoAES::test()
              {
                  // Init temp file
                  tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                  tmpFile->open();
                  qDebug() << __FUNCTION__ << 1 << tmpFile->fileName();
                  tmpFile->close();
              
                  // Settings
                  QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                  qDebug() << __FUNCTION__ << 2 << settings.fileName();
                  settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                  settings.beginGroup("GroupTest");
              
                  QStringList list;
                  list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                  settings.setValue("UserCode", list);
                  settings.setValue("UserCodeDefault", QStringLiteral("1234"));
              
                  settings.sync();
                  settings.endGroup();
              
                  // Read temp file
                  tmpFile->open();
                  qDebug() << __FUNCTION__ << 3 << tmpFile->readAll();
                  tmpFile->close();
              }
              
              1 Reply Last reply
              0
              • T tovax
                9 Oct 2021, 03:28

                @jeremy_k Hi, thank you very much! I modified the code, but I still can't write it successfully.

                Debug output:
                JCDemoAES::test "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.sEbGkR"
                JCDemoAES::test ""

                void JCDemoAES::test()
                {
                    // Init temp file
                    tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                    tmpFile->open();
                    qDebug() << __FUNCTION__ << tmpFile->fileName();
                    tmpFile->close();
                
                    // Settings
                    QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                    settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                    settings.beginGroup("GroupTest");
                
                    QStringList list;
                    list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                    settings.setValue("UserCode", list);
                    settings.setValue("UserCodeDefault", QStringLiteral("1234"));
                
                    settings.sync();
                    settings.endGroup();
                
                    // Read temp file
                    tmpFile->open();
                    qDebug() << __FUNCTION__ << tmpFile->readAll();
                    tmpFile->close();
                }
                
                J Offline
                J Offline
                jeremy_k
                wrote on 9 Oct 2021, 03:41 last edited by
                #7

                QSettings::sync() should go after QSettings::endGroup().

                Asking a question about code? http://eel.is/iso-c++/testcase/

                T 1 Reply Last reply 9 Oct 2021, 03:44
                1
                • J jeremy_k
                  9 Oct 2021, 03:41

                  QSettings::sync() should go after QSettings::endGroup().

                  T Offline
                  T Offline
                  tovax
                  wrote on 9 Oct 2021, 03:44 last edited by
                  #8

                  @jeremy_k Thank you for your patience!
                  Debug output:
                  JCDemoAES::test 1 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.zJpgBl"
                  JCDemoAES::test 2 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.zJpgBl"
                  JCDemoAES::test 3 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.zJpgBl"
                  JCDemoAES::test 4 ""

                  void JCDemoAES::test()
                  {
                      // Init temp file
                      tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                      tmpFile->open();
                      qDebug() << __FUNCTION__ << 1 << tmpFile->fileName();
                      tmpFile->close();
                  
                      // Settings
                      QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                      qDebug() << __FUNCTION__ << 2 << settings.fileName();
                      settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                      settings.beginGroup("GroupTest");
                  
                      QStringList list;
                      list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                      settings.setValue("UserCode", list);
                      settings.setValue("UserCodeDefault", QStringLiteral("1234"));
                  
                      settings.endGroup();
                      settings.sync();
                  
                      // Read temp file
                      tmpFile->open();
                      qDebug() << __FUNCTION__ << 3 << tmpFile->fileName();
                      qDebug() << __FUNCTION__ << 4 << tmpFile->readAll();
                      tmpFile->close();
                  }
                  
                  P 1 Reply Last reply 9 Oct 2021, 03:50
                  0
                  • T tovax
                    9 Oct 2021, 03:44

                    @jeremy_k Thank you for your patience!
                    Debug output:
                    JCDemoAES::test 1 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.zJpgBl"
                    JCDemoAES::test 2 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.zJpgBl"
                    JCDemoAES::test 3 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.zJpgBl"
                    JCDemoAES::test 4 ""

                    void JCDemoAES::test()
                    {
                        // Init temp file
                        tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                        tmpFile->open();
                        qDebug() << __FUNCTION__ << 1 << tmpFile->fileName();
                        tmpFile->close();
                    
                        // Settings
                        QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                        qDebug() << __FUNCTION__ << 2 << settings.fileName();
                        settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                        settings.beginGroup("GroupTest");
                    
                        QStringList list;
                        list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                        settings.setValue("UserCode", list);
                        settings.setValue("UserCodeDefault", QStringLiteral("1234"));
                    
                        settings.endGroup();
                        settings.sync();
                    
                        // Read temp file
                        tmpFile->open();
                        qDebug() << __FUNCTION__ << 3 << tmpFile->fileName();
                        qDebug() << __FUNCTION__ << 4 << tmpFile->readAll();
                        tmpFile->close();
                    }
                    
                    P Offline
                    P Offline
                    Pl45m4
                    wrote on 9 Oct 2021, 03:50 last edited by Pl45m4 10 Sept 2021, 03:59
                    #9

                    @tovax

                    There could be an issue because InitFormat may expect an *.ini extension. Your temp file seem to have ini file format, but a random file extension.
                    It could be possible that QSettings wont recognize this file. But this is just a guess.
                    The read/write access looks good now

                    On Unix, NativeFormat and IniFormat mean the
                     same thing, except that the file extension
                     is different (.conf for NativeFormat, .ini for IniFormat).
                    

                    (from: https://doc.qt.io/qt-5/qsettings.html#Format-enum)


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    T 1 Reply Last reply 9 Oct 2021, 03:59
                    0
                    • P Pl45m4
                      9 Oct 2021, 03:50

                      @tovax

                      There could be an issue because InitFormat may expect an *.ini extension. Your temp file seem to have ini file format, but a random file extension.
                      It could be possible that QSettings wont recognize this file. But this is just a guess.
                      The read/write access looks good now

                      On Unix, NativeFormat and IniFormat mean the
                       same thing, except that the file extension
                       is different (.conf for NativeFormat, .ini for IniFormat).
                      

                      (from: https://doc.qt.io/qt-5/qsettings.html#Format-enum)

                      T Offline
                      T Offline
                      tovax
                      wrote on 9 Oct 2021, 03:59 last edited by
                      #10

                      @Pl45m4 I tested QFile, the write failure should not be caused by the extension.

                      Debug output:
                      JCDemoAES::test 1 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.TBjIps"
                      JCDemoAES::test 2 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.TBjIps"
                      JCDemoAES::test 3 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.TBjIps"
                      JCDemoAES::test 4 "[GroupTest]\nUserCode=0000, 1111, 2222, 3333\nUserCodeDefault=1234\n"

                      void JCDemoAES::test()
                      {
                          // Init temp file
                          // tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                          tmpFile = new QFile(QStringLiteral("%1/%2.TBjIps").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                          tmpFile->open(QIODevice::ReadWrite | QIODevice::Text);
                          qDebug() << __FUNCTION__ << 1 << tmpFile->fileName();
                          tmpFile->close();
                      
                          // Settings
                          QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                          qDebug() << __FUNCTION__ << 2 << settings.fileName();
                          settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                          settings.beginGroup("GroupTest");
                      
                          QStringList list;
                          list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                          settings.setValue("UserCode", list);
                          settings.setValue("UserCodeDefault", QStringLiteral("1234"));
                      
                          settings.endGroup();
                          settings.sync();
                      
                          // Read temp file
                          tmpFile->open(QIODevice::ReadWrite | QIODevice::Text);
                          qDebug() << __FUNCTION__ << 3 << tmpFile->fileName();
                          qDebug() << __FUNCTION__ << 4 << tmpFile->readAll();
                          tmpFile->close();
                      }
                      
                      P 1 Reply Last reply 9 Oct 2021, 04:09
                      0
                      • T tovax
                        9 Oct 2021, 03:59

                        @Pl45m4 I tested QFile, the write failure should not be caused by the extension.

                        Debug output:
                        JCDemoAES::test 1 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.TBjIps"
                        JCDemoAES::test 2 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.TBjIps"
                        JCDemoAES::test 3 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.TBjIps"
                        JCDemoAES::test 4 "[GroupTest]\nUserCode=0000, 1111, 2222, 3333\nUserCodeDefault=1234\n"

                        void JCDemoAES::test()
                        {
                            // Init temp file
                            // tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                            tmpFile = new QFile(QStringLiteral("%1/%2.TBjIps").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                            tmpFile->open(QIODevice::ReadWrite | QIODevice::Text);
                            qDebug() << __FUNCTION__ << 1 << tmpFile->fileName();
                            tmpFile->close();
                        
                            // Settings
                            QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                            qDebug() << __FUNCTION__ << 2 << settings.fileName();
                            settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                            settings.beginGroup("GroupTest");
                        
                            QStringList list;
                            list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                            settings.setValue("UserCode", list);
                            settings.setValue("UserCodeDefault", QStringLiteral("1234"));
                        
                            settings.endGroup();
                            settings.sync();
                        
                            // Read temp file
                            tmpFile->open(QIODevice::ReadWrite | QIODevice::Text);
                            qDebug() << __FUNCTION__ << 3 << tmpFile->fileName();
                            qDebug() << __FUNCTION__ << 4 << tmpFile->readAll();
                            tmpFile->close();
                        }
                        
                        P Offline
                        P Offline
                        Pl45m4
                        wrote on 9 Oct 2021, 04:09 last edited by Pl45m4 10 Sept 2021, 04:18
                        #11

                        @tovax

                        Got (actually not me) the solution...

                        Try to open the tempFile one time BEFORE you create the QSettings object.
                        Then it should work :)

                        Here's the MVP :)
                        https://stackoverflow.com/a/57471942

                        fileName returns an empty string when the file is not open. Which you did in your first try (but wrong sync). In your latest approach, the file is closed when creating the settings.
                        It's a QTemporaryFile thing, this is why it worked with QFile even when the file is closed.

                        So open before settings and sync after endGroup :)


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        T 1 Reply Last reply 9 Oct 2021, 06:21
                        1
                        • J Offline
                          J Offline
                          jeremy_k
                          wrote on 9 Oct 2021, 04:20 last edited by
                          #12

                          This works for me:

                          #include <QCoreApplication>
                          #include <QSettings>
                          #include <QTemporaryFile>
                          #include <QTextCodec>
                          #include <QDebug>
                          int main(int argc, char *argv[])
                          {
                              QCoreApplication a(argc, argv);
                          
                              QTemporaryFile tmpFile("/tmp/tmpfile.XXXXXXX");
                              tmpFile.open();
                              tmpFile.close();
                          
                              QSettings settings(tmpFile.fileName(), QSettings::IniFormat);
                              settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                              settings.setValue("UserCode", "test value");
                              settings.sync();
                              
                              QFile reader(tmpFile.fileName());
                              reader.open(QIODevice::ReadOnly);
                              qDebug() << "file" << tmpFile.fileName();
                              qDebug() << "content" << reader.readAll();
                          }
                          

                          Output:

                          21:19:24: Starting /tmp/build-settingsfile-Desktop_Qt_5_15_5_clang_64bit-Debug/settingsfile ...
                          file "/tmp/tmpfile.ZlFULTY"
                          content "[General]\nUserCode=test value\n"
                          

                          Asking a question about code? http://eel.is/iso-c++/testcase/

                          T 1 Reply Last reply 9 Oct 2021, 06:36
                          4
                          • J Offline
                            J Offline
                            jeremy_k
                            wrote on 9 Oct 2021, 04:34 last edited by jeremy_k 10 Sept 2021, 04:34
                            #13

                            The issue seems to be caching in the QIODevice, at least on macOS with Qt 5.15.5.

                            #include <QCoreApplication>
                            #include <QSettings>
                            #include <QTemporaryFile>
                            #include <QTextCodec>
                            #include <QDebug>
                            int main(int argc, char *argv[])
                            {
                                QCoreApplication a(argc, argv);
                            
                                QTemporaryFile tmpFile("/tmp/tmpfile.XXXXXXX");
                                tmpFile.open();
                                tmpFile.write("some text");
                                tmpFile.seek(0);
                                qDebug() << "tmpfile" << tmpFile.readAll();
                                tmpFile.close();
                            
                                QSettings settings(tmpFile.fileName(), QSettings::IniFormat);
                                settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                                settings.setValue("UserCode", "test value");
                                settings.sync();
                            
                                QFile reader(tmpFile.fileName());
                                reader.open(QIODevice::ReadOnly);
                                qDebug() << "file" << tmpFile.fileName() << reader.fileName();
                                qDebug() << "content" << reader.readAll();
                                tmpFile.open();
                                qDebug() << "tmpfile" << tmpFile.readAll();
                            }
                            

                            Output:

                            tmpfile "some text"
                            file "/tmp/tmpfile.NmlyxbW" "/tmp/tmpfile.NmlyxbW"
                            content "[General]\nUserCode=test value\n"
                            tmpfile "some text"
                            

                            Asking a question about code? http://eel.is/iso-c++/testcase/

                            J T 2 Replies Last reply 9 Oct 2021, 04:40
                            1
                            • J jeremy_k
                              9 Oct 2021, 04:34

                              The issue seems to be caching in the QIODevice, at least on macOS with Qt 5.15.5.

                              #include <QCoreApplication>
                              #include <QSettings>
                              #include <QTemporaryFile>
                              #include <QTextCodec>
                              #include <QDebug>
                              int main(int argc, char *argv[])
                              {
                                  QCoreApplication a(argc, argv);
                              
                                  QTemporaryFile tmpFile("/tmp/tmpfile.XXXXXXX");
                                  tmpFile.open();
                                  tmpFile.write("some text");
                                  tmpFile.seek(0);
                                  qDebug() << "tmpfile" << tmpFile.readAll();
                                  tmpFile.close();
                              
                                  QSettings settings(tmpFile.fileName(), QSettings::IniFormat);
                                  settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                                  settings.setValue("UserCode", "test value");
                                  settings.sync();
                              
                                  QFile reader(tmpFile.fileName());
                                  reader.open(QIODevice::ReadOnly);
                                  qDebug() << "file" << tmpFile.fileName() << reader.fileName();
                                  qDebug() << "content" << reader.readAll();
                                  tmpFile.open();
                                  qDebug() << "tmpfile" << tmpFile.readAll();
                              }
                              

                              Output:

                              tmpfile "some text"
                              file "/tmp/tmpfile.NmlyxbW" "/tmp/tmpfile.NmlyxbW"
                              content "[General]\nUserCode=test value\n"
                              tmpfile "some text"
                              
                              J Offline
                              J Offline
                              jeremy_k
                              wrote on 9 Oct 2021, 04:40 last edited by
                              #14

                              https://doc.qt.io/qt-6/qiodevice.html says:
                              Some subclasses, such as QFile and QTcpSocket, are implemented using a memory buffer for intermediate storing of data.

                              My guess is that closing and reopening the QTemporaryFile doesn't discard its buffer and fails to notice the underlying file change.

                              Asking a question about code? http://eel.is/iso-c++/testcase/

                              1 Reply Last reply
                              0
                              • P Pl45m4
                                9 Oct 2021, 04:09

                                @tovax

                                Got (actually not me) the solution...

                                Try to open the tempFile one time BEFORE you create the QSettings object.
                                Then it should work :)

                                Here's the MVP :)
                                https://stackoverflow.com/a/57471942

                                fileName returns an empty string when the file is not open. Which you did in your first try (but wrong sync). In your latest approach, the file is closed when creating the settings.
                                It's a QTemporaryFile thing, this is why it worked with QFile even when the file is closed.

                                So open before settings and sync after endGroup :)

                                T Offline
                                T Offline
                                tovax
                                wrote on 9 Oct 2021, 06:21 last edited by
                                #15

                                @Pl45m4
                                Output:
                                JCDemoAES::test 1 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.NYWrOp"
                                JCDemoAES::test 2 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.NYWrOp"
                                JCDemoAES::test 3 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.NYWrOp"
                                JCDemoAES::test 4 ""

                                void JCDemoAES::test()
                                {
                                    // Init temp file
                                    tmpFile = new QTemporaryFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()), this);
                                    tmpFile->open();
                                    qDebug() << __FUNCTION__ << 1 << tmpFile->fileName();
                                
                                    // Settings
                                    QSettings settings(tmpFile->fileName(), QSettings::IniFormat);
                                    qDebug() << __FUNCTION__ << 2 << settings.fileName();
                                    settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                                    settings.beginGroup("GroupTest");
                                
                                    QStringList list;
                                    list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                                    settings.setValue("UserCode", list);
                                    settings.setValue("UserCodeDefault", QStringLiteral("1234"));
                                
                                    settings.endGroup();
                                    settings.sync();
                                
                                    // Read temp file
                                    qDebug() << __FUNCTION__ << 3 << tmpFile->fileName();
                                    qDebug() << __FUNCTION__ << 4 << tmpFile->readAll();
                                    tmpFile->close();
                                }
                                
                                1 Reply Last reply
                                0
                                • J jeremy_k
                                  9 Oct 2021, 04:20

                                  This works for me:

                                  #include <QCoreApplication>
                                  #include <QSettings>
                                  #include <QTemporaryFile>
                                  #include <QTextCodec>
                                  #include <QDebug>
                                  int main(int argc, char *argv[])
                                  {
                                      QCoreApplication a(argc, argv);
                                  
                                      QTemporaryFile tmpFile("/tmp/tmpfile.XXXXXXX");
                                      tmpFile.open();
                                      tmpFile.close();
                                  
                                      QSettings settings(tmpFile.fileName(), QSettings::IniFormat);
                                      settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                                      settings.setValue("UserCode", "test value");
                                      settings.sync();
                                      
                                      QFile reader(tmpFile.fileName());
                                      reader.open(QIODevice::ReadOnly);
                                      qDebug() << "file" << tmpFile.fileName();
                                      qDebug() << "content" << reader.readAll();
                                  }
                                  

                                  Output:

                                  21:19:24: Starting /tmp/build-settingsfile-Desktop_Qt_5_15_5_clang_64bit-Debug/settingsfile ...
                                  file "/tmp/tmpfile.ZlFULTY"
                                  content "[General]\nUserCode=test value\n"
                                  
                                  T Offline
                                  T Offline
                                  tovax
                                  wrote on 9 Oct 2021, 06:36 last edited by
                                  #16

                                  @jeremy_k
                                  PC:
                                  Win10 64bit
                                  Qt 5.14.2

                                      QTemporaryFile tmpFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()));
                                      tmpFile.open();
                                      tmpFile.close();
                                  
                                      QSettings settings(tmpFile.fileName(), QSettings::IniFormat);
                                      settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                                      settings.setValue("UserCode", "test value");
                                      settings.sync();
                                  
                                      QFile reader(tmpFile.fileName());
                                      reader.open(QIODevice::ReadOnly);
                                      qDebug() << "file" << tmpFile.fileName();
                                      qDebug() << "content" << reader.readAll();
                                  

                                  Output:
                                  file "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.IwayTr"
                                  content ""

                                  1 Reply Last reply
                                  0
                                  • J jeremy_k
                                    9 Oct 2021, 04:34

                                    The issue seems to be caching in the QIODevice, at least on macOS with Qt 5.15.5.

                                    #include <QCoreApplication>
                                    #include <QSettings>
                                    #include <QTemporaryFile>
                                    #include <QTextCodec>
                                    #include <QDebug>
                                    int main(int argc, char *argv[])
                                    {
                                        QCoreApplication a(argc, argv);
                                    
                                        QTemporaryFile tmpFile("/tmp/tmpfile.XXXXXXX");
                                        tmpFile.open();
                                        tmpFile.write("some text");
                                        tmpFile.seek(0);
                                        qDebug() << "tmpfile" << tmpFile.readAll();
                                        tmpFile.close();
                                    
                                        QSettings settings(tmpFile.fileName(), QSettings::IniFormat);
                                        settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                                        settings.setValue("UserCode", "test value");
                                        settings.sync();
                                    
                                        QFile reader(tmpFile.fileName());
                                        reader.open(QIODevice::ReadOnly);
                                        qDebug() << "file" << tmpFile.fileName() << reader.fileName();
                                        qDebug() << "content" << reader.readAll();
                                        tmpFile.open();
                                        qDebug() << "tmpfile" << tmpFile.readAll();
                                    }
                                    

                                    Output:

                                    tmpfile "some text"
                                    file "/tmp/tmpfile.NmlyxbW" "/tmp/tmpfile.NmlyxbW"
                                    content "[General]\nUserCode=test value\n"
                                    tmpfile "some text"
                                    
                                    T Offline
                                    T Offline
                                    tovax
                                    wrote on 9 Oct 2021, 06:38 last edited by
                                    #17

                                    @jeremy_k said in Why can't QSettings be written to QTemporaryFile:

                                    e seems to be caching in the QIODev

                                        QTemporaryFile tmpFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath()).arg(QApplication::applicationName()));
                                        tmpFile.open();
                                        tmpFile.write("some text");
                                        tmpFile.seek(0);
                                        qDebug() << "tmpfile" << tmpFile.readAll();
                                        tmpFile.close();
                                    
                                        QSettings settings(tmpFile.fileName(), QSettings::IniFormat);
                                        settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                                        settings.setValue("UserCode", "test value");
                                        settings.sync();
                                    
                                        QFile reader(tmpFile.fileName());
                                        reader.open(QIODevice::ReadOnly);
                                        qDebug() << "file" << tmpFile.fileName() << reader.fileName();
                                        qDebug() << "content" << reader.readAll();
                                        tmpFile.open();
                                        qDebug() << "tmpfile" << tmpFile.readAll();
                                    

                                    Output:
                                    tmpfile "some text"
                                    file "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.bveDgi" "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_14_2_MSVC2017_64bit-Debug/debug/JCDemoAES.bveDgi"
                                    content "some text"
                                    tmpfile "some text"

                                    1 Reply Last reply
                                    0
                                    • T Offline
                                      T Offline
                                      tovax
                                      wrote on 9 Oct 2021, 10:14 last edited by
                                      #18

                                      I tested qt5.15.2 and couldn't work.
                                      PC: Win10 64-bit
                                      Compiler: MSVC2019

                                      1 Reply Last reply
                                      0
                                      • Christian EhrlicherC Online
                                        Christian EhrlicherC Online
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on 9 Oct 2021, 11:58 last edited by Christian Ehrlicher 10 Sept 2021, 11:58
                                        #19

                                        You should check QSettings::status() and will notice that you get a QSettings::FormatError since you try to load an invalid ini file.

                                        qDebug() << "Settings status: " <<  settings.status();
                                        

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        T 2 Replies Last reply 10 Oct 2021, 00:44
                                        4
                                        • Christian EhrlicherC Christian Ehrlicher
                                          9 Oct 2021, 11:58

                                          You should check QSettings::status() and will notice that you get a QSettings::FormatError since you try to load an invalid ini file.

                                          qDebug() << "Settings status: " <<  settings.status();
                                          
                                          T Offline
                                          T Offline
                                          tovax
                                          wrote on 10 Oct 2021, 00:44 last edited by tovax 10 Oct 2021, 00:45
                                          #20

                                          @Christian-Ehrlicher Hi, thanks.
                                          It's "QSettings::NoError".
                                          Output:
                                          JCDemoAES::test 1 "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug/debug/JCDemoAES.tSHxeC"
                                          JCDemoAES::test 2 QSettings::NoError
                                          JCDemoAES::test file: "E:/JCShared/Projects/JCDemo/JCDemoAES/build-JCDemoAES-Desktop_Qt_5_15_2_MSVC2019_64bit-Debug/debug/JCDemoAES.tSHxeC"
                                          JCDemoAES::test content: ""

                                              // Init temp file
                                              QTemporaryFile tmpFile(QStringLiteral("%1/%2").arg(QApplication::applicationDirPath(), QApplication::applicationName()), this);
                                              tmpFile.open();
                                              tmpFile.close();
                                              qDebug() << __FUNCTION__ << 1 << tmpFile.fileName();
                                          
                                              // Settings
                                              QSettings settings(tmpFile.fileName(), QSettings::IniFormat);
                                              qDebug() << __FUNCTION__ << 2 << settings.status();
                                              settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
                                              settings.beginGroup("GroupTest");
                                          
                                              QStringList list;
                                              list << QStringLiteral("0000") << QStringLiteral("1111") << QStringLiteral("2222") << QStringLiteral("3333");
                                              settings.setValue("UserCode", list);
                                              settings.setValue("UserCodeDefault", QStringLiteral("1234"));
                                          
                                              settings.endGroup();
                                              settings.sync();
                                          
                                              // Read temp file
                                              QFile reader(tmpFile.fileName());
                                              reader.open(QIODevice::ReadOnly);
                                              qDebug() << __FUNCTION__ << "file:" << tmpFile.fileName();
                                              qDebug() << __FUNCTION__ << "content:" << reader.readAll();
                                          
                                          1 Reply Last reply
                                          0

                                          10/36

                                          9 Oct 2021, 03:59

                                          topic:navigator.unread, 26
                                          • Login

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