> This is basically the way to go.

Thanks, although again I'd prefer to not have to do the actual file roundtrip, 
but it's not horrible.

> >
> > void MainWindow::writeSettings()
> > {
> > // comment/uncomment the *define* below to switch implementations
> > #define USE_QTEMPORARY_FILE
> >
> > #ifdef USE_QTEMPORARY_FILE
> >      // using a QTemporaryFile doesn't seem to work
> >      QTemporaryFile tempFile;
> >      bool ok = tempFile.open();
> > #else
> >      // if instead you use a regular QFile, it works
> >      QFile tempFile("tempFile.ini");
> >      bool ok = tempFile.open(QIODevice::ReadWrite);
> > #endif
> >
> >      if(ok)
> >      {
> >          qDebug() << "Opened" << tempFile.fileName();
> >      } else {
> >          qDebug() << "Unable to open" << tempFile.fileName();
> >      }
> Call tempFile.fileName() before you call close()! (But close() before you
> instantiate QSettings.)

I'm confused by your statement. Isn't that exactly what I'm doing? I 
first open the tempFile:
    bool ok = tempFile.open(QIODevice::ReadWrite);
Then I do call fileName() on it, as long as the open succeeded:
    if(ok)
    {
        qDebug() << "Opened" << tempFile.fileName();
    }
Then I close it on the following line, and then call the QSettings constructor 
with that filename.

> >      tempFile.close();
> >
> >      QSettings settings(tempFile.fileName(), QSettings::IniFormat);
> >      settings.setValue("string", "hello");
> >      settings.setValue("int", 2);
> >
> >      settings.sync();
> >      // using QTemporaryFile always produces an AccessError here
> >     qDebug() << settings.status();
> Because QSettings has the file locked. Make sure settings goes out of scope
> before you re-open the file.

I put the qDebug() << settings.status() line back in above (you omitted in your 
reply), but the AccessError I'm referring to is a **QSettings** error, not an 
access 
error with the QFile object later as shown output on the above debug line. So 
at 
this point the temporary file on disk contains exactly zero bytes, QSettings 
never 
wrote anything to it when the sync() call was made, and after the sync(), the 
QSettings object's status is now QSettings::AccessError. I believe QSettings 
was 
never able to lock the file created by QTemporaryFile - probably because the 
QTemporaryFile object is hanging on to the lock so it can remove the file when 
the QTemporaryFile goes out of scope?

Sean




This message has been scanned for malware by Forcepoint. www.forcepoint.com
_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to