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. Settings Dialog with QSettings
Forum Updated to NodeBB v4.3 + New Features

Settings Dialog with QSettings

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.9k Views 1 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.
  • E Offline
    E Offline
    Eddi0406
    wrote on last edited by
    #1

    Hi,

    currently I'm implementing a settings dialog into my small appilcation. I found the "Config Dialog Example" from Qt and implemented it into my program. It's working fine, but has no "logic" behind the gui.

    Now I want to read and write the settings from different places and files in the source code (not only in the config dialog). I'm using the QSettings class and save my parameters in an ini-file.

    What is the best way to access the file? Should I load it once at application startup and give it as parameter to the config file class or should I load it every time, when I need one parameter?

    Thank you!
    Eddi0406

    edit: Or is it maybe the best way to implement a settings handler class and access it in every class I will read and write parameter to the one file?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Subst27
      wrote on last edited by
      #2

      HI!

      I'm load needed ini file every time I'm want to read or write parameter(s).
      Why hold opened descriptor whole time program in work?

      It's first.

      the second. If your ini file have been changed outside program you can't see this until restart program if you open file once at start up program.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eddi0406
        wrote on last edited by
        #3

        @Subst27
        Okay, good reason. I think I will implement it this way and give the name of the file as parameter. Thank you:)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Subst27
          wrote on last edited by
          #4

          I use separate class and some static methods in it

          such as
          @static void putSetting(QString iniFile,QString group,QString key,QVariant value);
          static QVariant getSetting(QString iniFile,QString group,QString key,QVariant defaultValue);@

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Eddi0406
            wrote on last edited by
            #5

            Can you provide me the implementation ot these two methods also, please? I don't know how to handle the group.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Subst27
              wrote on last edited by
              #6

              sure.

              @void TCommon::putSetting(QString iniFile,QString group,QString key,QVariant value)
              {
              QSettings settings(/QSettings::IniFormat,QSettings::UserScope,""some",iniFile.baseName());///iniFile,QSettings::IniFormat);
              settings.beginGroup(group);
              settings.setValue(key,value);
              settings.endGroup();
              return;
              }
              //
              QVariant TCommon::getSetting(QString iniFile,QString group,QString key,QVariant defaultValue)
              {
              QSettings settings(/QSettings::IniFormat,QSettings::UserScope,"some",iniFile.baseName());///iniFile,QSettings::IniFormat);
              settings.beginGroup(group);
              QVariant value=settings.value(key,defaultValue);
              settings.endGroup();
              return value;
              }
              //@

              Note. This methods I use for windows and linux only. For more general purpose I use another implementation (like commented in this code).

              It's because on Mac and android other structure of deployment

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Eddi0406
                wrote on last edited by
                #7

                Works perfectly, thanks a lot!:)

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  I am using a single QObject-derived class as my settings class (well, per library that is). That class is the one and only accesspoint to the settings, and settings are exposed as Q_PROPERTYs (with getter, setter, change signal).

                  That central settings class manages its settings via a QSettings instance (well, it's a bit more complicated in our case, but that doesn't matter for the argument).

                  Note that I do not allow any key-based access to any settings value from outside of this settings class. Also: no QVariant based values: everything is strongly typed. That means that if you have an enum that encodes some possible values, you can just set this enum and get it too, not an int.

                  1 Reply Last reply
                  0

                  • Login

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