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. Save permanent settings on QT Web App
Forum Updated to NodeBB v4.3 + New Features

Save permanent settings on QT Web App

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 1.9k 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.
  • G Offline
    G Offline
    GCDX
    wrote on last edited by
    #1

    Hi everyone,

    I was thinking of using Qsettings to save a user's username, however, i can't seem to be able to find how to do that. Can someone give me an example how to define my own variable such as QString username and store it permanently(won't be deleted after the app is closed) . If not , i'm open to other suggestions on how to go about doing this.

    JonBJ 1 Reply Last reply
    0
    • G GCDX

      Hi everyone,

      I was thinking of using Qsettings to save a user's username, however, i can't seem to be able to find how to do that. Can someone give me an example how to define my own variable such as QString username and store it permanently(won't be deleted after the app is closed) . If not , i'm open to other suggestions on how to go about doing this.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @GCDX
      You just use http://doc.qt.io/qt-5/qsettings.html#setValue to set your own name & value, e.g. QSettings::setValue("username", username), that's it.

      1 Reply Last reply
      1
      • Prince_0912P Offline
        Prince_0912P Offline
        Prince_0912
        wrote on last edited by Prince_0912
        #3

        Hi @GCDX ,
        For setting and getting values of user by QSetting class you can perform following code.

        QSettings* InitRegSettings()
        {
        QSettings* regSett;
        regSett = new QSettings("Organization-name","Project-name");
        return regSett;
        }

        void SetMyValue(QString key, QVariant value)
        {
        InitRegSettings()->setValue(key,value); //Store value of user
        }
        QVariant GetMyValue(QString key, QVariant defaultValue)
        {
        return InitRegSettings()->value(key,defaultValue);//Get value of user
        }

        G SGaistS 2 Replies Last reply
        1
        • Prince_0912P Prince_0912

          Hi @GCDX ,
          For setting and getting values of user by QSetting class you can perform following code.

          QSettings* InitRegSettings()
          {
          QSettings* regSett;
          regSett = new QSettings("Organization-name","Project-name");
          return regSett;
          }

          void SetMyValue(QString key, QVariant value)
          {
          InitRegSettings()->setValue(key,value); //Store value of user
          }
          QVariant GetMyValue(QString key, QVariant defaultValue)
          {
          return InitRegSettings()->value(key,defaultValue);//Get value of user
          }

          G Offline
          G Offline
          GCDX
          wrote on last edited by GCDX
          #4

          @Prince_0912 @JonB do i need to define a file to store all the setting values??? Beccause i followed this
          https://wiki.qt.io/How_to_Use_QSettings

          It showed me how to make a file and all, except it did not tell me how to write to a file and read?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GCDX
            wrote on last edited by
            #5

            Also, do i need to use a special file format or can i just anything like txt? I'm really confused because there's not documentation on how the settings are saved? Do i need to create a file to store the settings? Where do i create this file to store the settings?

            jsulmJ JonBJ Prince_0912P 3 Replies Last reply
            0
            • G GCDX

              Also, do i need to use a special file format or can i just anything like txt? I'm really confused because there's not documentation on how the settings are saved? Do i need to create a file to store the settings? Where do i create this file to store the settings?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @GCDX Please take some time to read http://doc.qt.io/qt-5/qsettings.html - it explains everything

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • G GCDX

                Also, do i need to use a special file format or can i just anything like txt? I'm really confused because there's not documentation on how the settings are saved? Do i need to create a file to store the settings? Where do i create this file to store the settings?

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

                @GCDX
                If you are going to use QSettings, the links we have asked you to read answer all the questions: Qt deals with the format, the filename & the location, you don't create or do anything (other than @Prince_0912 's skeleton code).

                If you do all this this yourself instead, it's up to you.

                G 1 Reply Last reply
                0
                • G GCDX

                  Also, do i need to use a special file format or can i just anything like txt? I'm really confused because there's not documentation on how the settings are saved? Do i need to create a file to store the settings? Where do i create this file to store the settings?

                  Prince_0912P Offline
                  Prince_0912P Offline
                  Prince_0912
                  wrote on last edited by
                  #8

                  @GCDX ,
                  These values are store in Registry.

                  Computer\HKEY_CURRENT_USER\Software\Organization-name\Project-name

                  In this location the value is set and get.

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @GCDX
                    If you are going to use QSettings, the links we have asked you to read answer all the questions: Qt deals with the format, the filename & the location, you don't create or do anything (other than @Prince_0912 's skeleton code).

                    If you do all this this yourself instead, it's up to you.

                    G Offline
                    G Offline
                    GCDX
                    wrote on last edited by
                    #9

                    @JonB @Prince_0912 okay thanks so much. https://wiki.qt.io/How_to_Use_QSettings cause this told me to create an ini file to store the information, so i was very confused! Thanks so much for the help

                    Prince_0912P 1 Reply Last reply
                    1
                    • G GCDX

                      @JonB @Prince_0912 okay thanks so much. https://wiki.qt.io/How_to_Use_QSettings cause this told me to create an ini file to store the information, so i was very confused! Thanks so much for the help

                      Prince_0912P Offline
                      Prince_0912P Offline
                      Prince_0912
                      wrote on last edited by
                      #10

                      @GCDX Your'e Welcome,
                      after getting solution,
                      Go to right hand side of page -> click on Topic Tools -> Mark as Solved.

                      1 Reply Last reply
                      0
                      • Prince_0912P Prince_0912

                        Hi @GCDX ,
                        For setting and getting values of user by QSetting class you can perform following code.

                        QSettings* InitRegSettings()
                        {
                        QSettings* regSett;
                        regSett = new QSettings("Organization-name","Project-name");
                        return regSett;
                        }

                        void SetMyValue(QString key, QVariant value)
                        {
                        InitRegSettings()->setValue(key,value); //Store value of user
                        }
                        QVariant GetMyValue(QString key, QVariant defaultValue)
                        {
                        return InitRegSettings()->value(key,defaultValue);//Get value of user
                        }

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

                        @Prince_0912 said in Save permanent settings on QT Web App:

                        Hi @GCDX ,
                        For setting and getting values of user by QSetting class you can perform following code.

                        QSettings* InitRegSettings()
                        {
                        QSettings* regSett;
                        regSett = new QSettings("Organization-name","Project-name");
                        return regSett;
                        }

                        void SetMyValue(QString key, QVariant value)
                        {
                        InitRegSettings()->setValue(key,value); //Store value of user
                        }
                        QVariant GetMyValue(QString key, QVariant defaultValue)
                        {
                        return InitRegSettings()->value(key,defaultValue);//Get value of user
                        }

                        You have several memory leaks here. Each time you call one of these function, you create a new QSettings object that is not deleted.

                        If the application object is properly configured like described in the QSettings documentation, you just need to use QSettings on the stack wherever you need it.

                        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
                        2
                        • JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @GCDX
                          Further to @SGaist 's observation.

                          Although http://doc.qt.io/qt-5/qsettings.html#details states

                          Constructing and destroying a QSettings object is very fast.

                          to me it seems "odd/inefficient" to be creating one specifying your organization & project names each time, as these never vary. I would either take the doc's suggestion of:

                          If you use QSettings from many places in your application, you might want to specify the organization name and the application name using QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName(), and then use the default QSettings constructor

                          or I would create one & only one QSettings in your app and re-use that each time.

                          1 Reply Last reply
                          1

                          • Login

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