QSettings - counting how many times my app has been opened

  • How can I make qt counting how many times my app has been opened ? I tried QtSettings but I do not know how to initialize first time the counter so the next time I restart the app, the counter get the value from QSettings, not from initialized value. I tried to do that in the main windows constructor.

  • It's basic usage of QSettings, just give it a default value:

    int i = settings.value("counter", 0).toInt();
    settings.setValue("counter", ++i);

    Actually it's clearly stated in the documentation, and also it's said that if the settings isn't found (hasn't been set previously), it returns a null QVariant which means 0 as an int, so you don't even have to give it the default value.

  • Thanks. It works.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.