Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QSettings how to printout default value of key?



  • 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.



  • @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.



  • @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?



  • @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?
    


  • @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.


  • Lifetime Qt Champion

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



  • @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)
    

  • Lifetime Qt Champion

    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.



  • @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!


Log in to reply