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. Retentive variable value
Qt 6.11 is out! See what's new in the release blog

Retentive variable value

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 527 Views
  • 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.
  • D Offline
    D Offline
    Damian7546
    wrote on last edited by Damian7546
    #1

    Hi,

    I would like to retentive my bool variable. Restore it value after restart app.
    Please tell my if below example is properly to retain input1 value :

    MyApp::MyApp()
    {
        readSettings();
    }
    
    void MyApp::writeSettings()
    {
        QSettings setting("RetVal", "values" );
        setting.setValue("input1", input1);
    }
    
    void MyApp::readSettings()
    {
         QSettings setting("RetVal", "values" );
         input1 = setting.value("input1").toBool();
    
    }
    void MyApp:input1Changed(bool state)
    {
         input1 = state;
         writeSettings();
    }
    
    O C 2 Replies Last reply
    0
    • D Damian7546

      Hi,

      I would like to retentive my bool variable. Restore it value after restart app.
      Please tell my if below example is properly to retain input1 value :

      MyApp::MyApp()
      {
          readSettings();
      }
      
      void MyApp::writeSettings()
      {
          QSettings setting("RetVal", "values" );
          setting.setValue("input1", input1);
      }
      
      void MyApp::readSettings()
      {
           QSettings setting("RetVal", "values" );
           input1 = setting.value("input1").toBool();
      
      }
      void MyApp:input1Changed(bool state)
      {
           input1 = state;
           writeSettings();
      }
      
      C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #3

      Apart from "RetVal" and "values" not being obviously good choices for organisation and application name respectively, I do not see anything wrong here.

      I would generally set the organisation and application names on the application object (i.e. one place only) and use the parameter-free QSettings constructor elsewhere. See Basic Usage in the docs.

      There is rarely a need to call QSettings::sync() directly. It will be called internally when the stack-based QSettings instances are destroyed anyway.

      1 Reply Last reply
      2
      • D Damian7546

        Hi,

        I would like to retentive my bool variable. Restore it value after restart app.
        Please tell my if below example is properly to retain input1 value :

        MyApp::MyApp()
        {
            readSettings();
        }
        
        void MyApp::writeSettings()
        {
            QSettings setting("RetVal", "values" );
            setting.setValue("input1", input1);
        }
        
        void MyApp::readSettings()
        {
             QSettings setting("RetVal", "values" );
             input1 = setting.value("input1").toBool();
        
        }
        void MyApp:input1Changed(bool state)
        {
             input1 = state;
             writeSettings();
        }
        
        O Offline
        O Offline
        ollarch
        wrote on last edited by ollarch
        #2

        @Damian7546 just call "settings.sync()" after setting the value.

        1 Reply Last reply
        0
        • D Damian7546

          Hi,

          I would like to retentive my bool variable. Restore it value after restart app.
          Please tell my if below example is properly to retain input1 value :

          MyApp::MyApp()
          {
              readSettings();
          }
          
          void MyApp::writeSettings()
          {
              QSettings setting("RetVal", "values" );
              setting.setValue("input1", input1);
          }
          
          void MyApp::readSettings()
          {
               QSettings setting("RetVal", "values" );
               input1 = setting.value("input1").toBool();
          
          }
          void MyApp:input1Changed(bool state)
          {
               input1 = state;
               writeSettings();
          }
          
          C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #3

          Apart from "RetVal" and "values" not being obviously good choices for organisation and application name respectively, I do not see anything wrong here.

          I would generally set the organisation and application names on the application object (i.e. one place only) and use the parameter-free QSettings constructor elsewhere. See Basic Usage in the docs.

          There is rarely a need to call QSettings::sync() directly. It will be called internally when the stack-based QSettings instances are destroyed anyway.

          1 Reply Last reply
          2
          • D Damian7546 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