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 how to printout default value of key?
QtWS25 Last Chance

QSettings how to printout default value of key?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.6k 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.
  • K Offline
    K Offline
    Kite R
    wrote on last edited by Kite R
    #1

    Is there a way to printout what is the default value for a specific key?

    int defaultValue = condition ? 1 : 0;
    int v = settings.value("key", defaultValue).toInt();
    qDebug() << ...
    

    I want to set the default value conditionally, and since this value influences my program I want to verify it.

    JonBJ 1 Reply Last reply
    0
    • K Kite R

      @JonB I want to check what the default value is from settings.value() and not from the variable, to make sure the value that is passed is the correct one, if that makes sense.

      int defaultValue = 1;
      qDebug() << defaultValue; // the int
      * 1
      settings.value("key", defaultValue);
      qDebug() <<               // <- also printout the value set in settings;
      * what am I?
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #4

      @Kite-R
      No, it does not make sense, and I don't know what you mean/are thinking of.

      If it helps: settings do not have any "default values" in themselves. They are either present or they are not. If they are not, passing a default value will return that. The docs state:

      If no default value is specified, a default QVariant is returned.

      So the only thing which is doing a default value is your settings.value("key", defaultValue) call.

      qDebug() << settings.value("key", defaultValue);
      qDebug() << settings.value("key");
      

      The first line prints out with defaultValue if key is not present. The second line prints out key's value, without any default. I don't know if this is what you are looking for.

      K 1 Reply Last reply
      3
      • K Kite R

        Is there a way to printout what is the default value for a specific key?

        int defaultValue = condition ? 1 : 0;
        int v = settings.value("key", defaultValue).toInt();
        qDebug() << ...
        

        I want to set the default value conditionally, and since this value influences my program I want to verify it.

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

        @Kite-R
        What do you mean? defaultValue is the default value, since you call settings.value("key", defaultValue). qDebug() << defaultValue prints it out, though presumably you have something else in mind?

        K 1 Reply Last reply
        0
        • JonBJ JonB

          @Kite-R
          What do you mean? defaultValue is the default value, since you call settings.value("key", defaultValue). qDebug() << defaultValue prints it out, though presumably you have something else in mind?

          K Offline
          K Offline
          Kite R
          wrote on last edited by
          #3

          @JonB I want to check what the default value is from settings.value() and not from the variable, to make sure the value that is passed is the correct one, if that makes sense.

          int defaultValue = 1;
          qDebug() << defaultValue; // the int
          * 1
          settings.value("key", defaultValue);
          qDebug() <<               // <- also printout the value set in settings;
          * what am I?
          
          JonBJ 1 Reply Last reply
          0
          • K Kite R

            @JonB I want to check what the default value is from settings.value() and not from the variable, to make sure the value that is passed is the correct one, if that makes sense.

            int defaultValue = 1;
            qDebug() << defaultValue; // the int
            * 1
            settings.value("key", defaultValue);
            qDebug() <<               // <- also printout the value set in settings;
            * what am I?
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Kite-R
            No, it does not make sense, and I don't know what you mean/are thinking of.

            If it helps: settings do not have any "default values" in themselves. They are either present or they are not. If they are not, passing a default value will return that. The docs state:

            If no default value is specified, a default QVariant is returned.

            So the only thing which is doing a default value is your settings.value("key", defaultValue) call.

            qDebug() << settings.value("key", defaultValue);
            qDebug() << settings.value("key");
            

            The first line prints out with defaultValue if key is not present. The second line prints out key's value, without any default. I don't know if this is what you are looking for.

            K 1 Reply Last reply
            3
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

              When you want to know if a key exist then use QSettings::contains().

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

              1 Reply Last reply
              0
              • JonBJ JonB

                @Kite-R
                No, it does not make sense, and I don't know what you mean/are thinking of.

                If it helps: settings do not have any "default values" in themselves. They are either present or they are not. If they are not, passing a default value will return that. The docs state:

                If no default value is specified, a default QVariant is returned.

                So the only thing which is doing a default value is your settings.value("key", defaultValue) call.

                qDebug() << settings.value("key", defaultValue);
                qDebug() << settings.value("key");
                

                The first line prints out with defaultValue if key is not present. The second line prints out key's value, without any default. I don't know if this is what you are looking for.

                K Offline
                K Offline
                Kite R
                wrote on last edited by Kite R
                #6

                @JonB Oh, thank you! That seems to be what I was looking for.

                It seems I was doing it like this, which is wrong somehow:

                int defaultValue = 1;
                int loadValue = settings.value("key", defaultValue).toInt();
                qDebug() << loadValue;
                

                This always returns 0. I'm not knowledgeable enough to know why though.

                But using your way is perfect:

                int defaultValue = 5;
                qDebug() << settings.value("key", defaultValue)] :" QVariant(int, 5)
                qDebug() << settings.value("key")] :"               QVariant(Invalid)
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  Hi,

                  @Kite-R said in QSettings how to printout default value of key?:

                  INT defaultValue = 1;

                  What is an INT ?
                  As for why it fails, I would say that the conversion failed. QVariant::toInt has a parameter that allows you to check if the conversion was successful.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  K 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    Hi,

                    @Kite-R said in QSettings how to printout default value of key?:

                    INT defaultValue = 1;

                    What is an INT ?
                    As for why it fails, I would say that the conversion failed. QVariant::toInt has a parameter that allows you to check if the conversion was successful.

                    K Offline
                    K Offline
                    Kite R
                    wrote on last edited by
                    #8

                    @SGaist Oh sorry, I was using UINT but made it simpler for the post, forgot to set lower-case, I edited post for clarity. I think you're right, the conversion was failing and was returning QString, instead of int (UINT for my case).

                    I checked it out and decided to apply it, so I ended up converting everything to bool, since I only needed 0 or 1, and my problem is solved.

                    bool v = settings.value("key", defaultValue).toBool();
                    if (v.canConvert(QVariant::Bool)) { qDebug() << QString("Hi"); }
                    

                    Thanks everyone!

                    1 Reply Last reply
                    0
                    • Ronel_qtmasterR Ronel_qtmaster referenced this topic on

                    • Login

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