Home > Blockchain >  How to automatically delete QSettings after application is closed?
How to automatically delete QSettings after application is closed?

Time:05-25

I'm trying to use QSettings and QTemporaryFile to check if the user is logged into two applications at the same time. Every time the user logs in, I use this:

QTemporaryFile tmpFile;
tempFile.open();
QSettings sessionSettings(tempFile.fileName(), QSettings::IniFormat);

if(!sessionSettings.value("activeUser").toBool()){
   sessionSettings.setValue("activeUser", 1);
   return false;
}

return true;

I would like the QSettings to be deleted or cleared after the application is closed. That way the user can only log into one application at a time. How can I do that?

CodePudding user response:

The quickest method would probably be calling QFile::remove() on the file when the application closes, e.g. by including it in some kind of "shutdown" function that is called at the end of the application (beware of crashes though, as the file would persist then)

As Genjutsu said, it is probably a better idea to use e.g. SingleApplication which essentially sets up a shared memory, ensuring that the state of the application is properly detected, even in the event of crashes.

CodePudding user response:

You can do this right in your main function after app.exec(). Something like this:

int main() {
    QApplication a;
    ...........
    int ret = a.exec();
    //clear settings here
    return ret;
}
  • Related