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. QSettings doesn't save data
Forum Updated to NodeBB v4.3 + New Features

QSettings doesn't save data

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 749 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.
  • A Offline
    A Offline
    aim0d
    wrote on last edited by aim0d
    #1

    When I click on the logout button and then "Yes" it should save some data in my file and then restart the application, but I don't get why it doesn't save it.
    This is the function that should do the work:

    void home::on_logout_BTN_clicked()
    {
        reply = QMessageBox::question(this, tr("Logout"), tr("Exit the actual session?"),QMessageBox::Yes|QMessageBox::No);
        if ((reply == QMessageBox::Yes) && (p_ciclo_macchina->cicloFermo==true))
        {
            seconds->stop();  // home page timer
    
            qDebug("Prova");
    
            // Restart application
            qApp->quit();
            QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
    }
    

    My setOnPowerFail( ) function:

    void storage::setOnPowerFail()
    {
        storageData->beginGroup("powerFail");
        storageData->setValue("pianiFatti",QString::number(lastPianoFatto));
        storageData->setValue("sacchiCaricati",QString::number(lastSaccoCaricato));
        storageData->setValue("palletProdotti",QString::number(lastPalletProdotti));
        storageData->setValue("numTotaleSacchi",QString::number(numTotaleSacchi));
        storageData->endGroup();
    }
    

    But if I check the file ini nothing changed and after the restart the field where I change value doesnt show the new values.
    I noticed that if I move the dataStorage->setOnPowerFail(); before the reply message box it works just fine, but inside the if statement. What could it be the problem?

    Christian EhrlicherC C JonBJ 3 Replies Last reply
    0
    • A aim0d

      When I click on the logout button and then "Yes" it should save some data in my file and then restart the application, but I don't get why it doesn't save it.
      This is the function that should do the work:

      void home::on_logout_BTN_clicked()
      {
          reply = QMessageBox::question(this, tr("Logout"), tr("Exit the actual session?"),QMessageBox::Yes|QMessageBox::No);
          if ((reply == QMessageBox::Yes) && (p_ciclo_macchina->cicloFermo==true))
          {
              seconds->stop();  // home page timer
      
              qDebug("Prova");
      
              // Restart application
              qApp->quit();
              QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
      }
      

      My setOnPowerFail( ) function:

      void storage::setOnPowerFail()
      {
          storageData->beginGroup("powerFail");
          storageData->setValue("pianiFatti",QString::number(lastPianoFatto));
          storageData->setValue("sacchiCaricati",QString::number(lastSaccoCaricato));
          storageData->setValue("palletProdotti",QString::number(lastPalletProdotti));
          storageData->setValue("numTotaleSacchi",QString::number(numTotaleSacchi));
          storageData->endGroup();
      }
      

      But if I check the file ini nothing changed and after the restart the field where I change value doesnt show the new values.
      I noticed that if I move the dataStorage->setOnPowerFail(); before the reply message box it works just fine, but inside the if statement. What could it be the problem?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      There is no class 'QStorage' in Qt. When it's QSettings - they're only saved when you explicitly ask to do them so or during destruction.

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

      A 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        There is no class 'QStorage' in Qt. When it's QSettings - they're only saved when you explicitly ask to do them so or during destruction.

        A Offline
        A Offline
        aim0d
        wrote on last edited by
        #3

        @Christian-Ehrlicher Yes, sorry you are right, it's QSetting no Qstorage.

        they're only saved when you explicitly ask to do them so or during destruction

        Okay? So how can I save during destruction?
        What's wrong in the code?

        Christian EhrlicherC 1 Reply Last reply
        0
        • A aim0d

          @Christian-Ehrlicher Yes, sorry you are right, it's QSetting no Qstorage.

          they're only saved when you explicitly ask to do them so or during destruction

          Okay? So how can I save during destruction?
          What's wrong in the code?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @aim0d said in QSettings doesn't save data:

          What's wrong in the code?

          You don't destruct your QSettings object nor tell them that it should sync the data to disk as I already wrote.

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

          SGaistS 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            @aim0d said in QSettings doesn't save data:

            What's wrong in the code?

            You don't destruct your QSettings object nor tell them that it should sync the data to disk as I already wrote.

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Unless doing something special, QSettings is usually allocated on the stack of the method that uses it. Most of the time, there's no need for a member variable of that type.

            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
            1
            • A aim0d

              When I click on the logout button and then "Yes" it should save some data in my file and then restart the application, but I don't get why it doesn't save it.
              This is the function that should do the work:

              void home::on_logout_BTN_clicked()
              {
                  reply = QMessageBox::question(this, tr("Logout"), tr("Exit the actual session?"),QMessageBox::Yes|QMessageBox::No);
                  if ((reply == QMessageBox::Yes) && (p_ciclo_macchina->cicloFermo==true))
                  {
                      seconds->stop();  // home page timer
              
                      qDebug("Prova");
              
                      // Restart application
                      qApp->quit();
                      QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
              }
              

              My setOnPowerFail( ) function:

              void storage::setOnPowerFail()
              {
                  storageData->beginGroup("powerFail");
                  storageData->setValue("pianiFatti",QString::number(lastPianoFatto));
                  storageData->setValue("sacchiCaricati",QString::number(lastSaccoCaricato));
                  storageData->setValue("palletProdotti",QString::number(lastPalletProdotti));
                  storageData->setValue("numTotaleSacchi",QString::number(numTotaleSacchi));
                  storageData->endGroup();
              }
              

              But if I check the file ini nothing changed and after the restart the field where I change value doesnt show the new values.
              I noticed that if I move the dataStorage->setOnPowerFail(); before the reply message box it works just fine, but inside the if statement. What could it be the problem?

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              @aim0d Perhap I missed something really obvious, but your home::on_logout_BTN_clicked() slot does not seem to call storage::setOnPowerFail()..

              1 Reply Last reply
              0
              • A aim0d

                When I click on the logout button and then "Yes" it should save some data in my file and then restart the application, but I don't get why it doesn't save it.
                This is the function that should do the work:

                void home::on_logout_BTN_clicked()
                {
                    reply = QMessageBox::question(this, tr("Logout"), tr("Exit the actual session?"),QMessageBox::Yes|QMessageBox::No);
                    if ((reply == QMessageBox::Yes) && (p_ciclo_macchina->cicloFermo==true))
                    {
                        seconds->stop();  // home page timer
                
                        qDebug("Prova");
                
                        // Restart application
                        qApp->quit();
                        QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
                }
                

                My setOnPowerFail( ) function:

                void storage::setOnPowerFail()
                {
                    storageData->beginGroup("powerFail");
                    storageData->setValue("pianiFatti",QString::number(lastPianoFatto));
                    storageData->setValue("sacchiCaricati",QString::number(lastSaccoCaricato));
                    storageData->setValue("palletProdotti",QString::number(lastPalletProdotti));
                    storageData->setValue("numTotaleSacchi",QString::number(numTotaleSacchi));
                    storageData->endGroup();
                }
                

                But if I check the file ini nothing changed and after the restart the field where I change value doesnt show the new values.
                I noticed that if I move the dataStorage->setOnPowerFail(); before the reply message box it works just fine, but inside the if statement. What could it be the problem?

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @aim0d said in QSettings doesn't save data:

                I noticed that if I move the dataStorage->setOnPowerFail(); before the reply message box it works just fine, but inside the if statement. What could it be the problem?

                To expand on what @Christian-Ehrlicher has said. void QSettings::sync() tells you:

                Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application.

                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.

                If you call setOnPowerFail() before QMessageBox::question() that allows the Qt event loop to run and so presumably syncs (writes) your changes to registry/disk. But if you call it afterwards and immediately quit/restart the application I guess the event loop does not get to run, nor does QSettings object get destructed, so the changes never get committed?

                Put in an explicit QSettings::sync() after setOnPowerFail() before restart, does that fix?

                A 1 Reply Last reply
                0
                • JonBJ JonB

                  @aim0d said in QSettings doesn't save data:

                  I noticed that if I move the dataStorage->setOnPowerFail(); before the reply message box it works just fine, but inside the if statement. What could it be the problem?

                  To expand on what @Christian-Ehrlicher has said. void QSettings::sync() tells you:

                  Writes any unsaved changes to permanent storage, and reloads any settings that have been changed in the meantime by another application.

                  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.

                  If you call setOnPowerFail() before QMessageBox::question() that allows the Qt event loop to run and so presumably syncs (writes) your changes to registry/disk. But if you call it afterwards and immediately quit/restart the application I guess the event loop does not get to run, nor does QSettings object get destructed, so the changes never get committed?

                  Put in an explicit QSettings::sync() after setOnPowerFail() before restart, does that fix?

                  A Offline
                  A Offline
                  aim0d
                  wrote on last edited by
                  #8

                  @JonB

                  @JonB said in QSettings doesn't save data:

                  I guess the event loop does not get to run, nor does QSettings object get destructed,

                  I thought this could be the problem.

                  @JonB said in QSettings doesn't save data:

                  Put in an explicit QSettings::sync() after setOnPowerFail() before restart, does that fix?

                  Actually yes, it did it!

                  1 Reply Last reply
                  0
                  • A aim0d has marked this topic as solved on

                  • Login

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