Important: Please read the Qt Code of Conduct -

Updating UI when update QSettings

  • Hello,

    I'm trying to update some part of my UI when the user update some settings.
    My issue is that when I change the settings and save it. I do emit a signal and one the signal is emitted, I do update my UI depending on the value I read in my QStettings file. But the value I read seems to not be properly updated or I do have a format issue I don't really know.

    Here is an example of what I'm doing.

    Using a combo box I set the selected index in the settings file. Then with that index I want to show a Component if that index is different from 0.
    So here is the code I use :

    myComponent.visible = settings.value("Usi/comboParam",0) !=="0"

    This seems to work when I do start my program. But at runtime the value is never "0" again.

    So I tried to add as well the same line with a int type such as the following code :

    myComponent.visible = settings.value("Usi/comboParam",0) !==0

    But this one is never false, not even when I start the program.

    Is there a specific format to use for this kind of Settings ? On the C++ side I use a QVariant as I do have Int, String and boolean settings.

    Here is the function I use to return my settings file value :

        QVariant value = QSettings::value(key, defaultValue);
        if (QString(value.typeName()) == "QString" &&  (value.toString() == "false" || value.toString() == "true"))
            return QVariant(value.toBool());
        return value;

  • @DavidM29 you may want to call QSettings::sync() just after saving the new values to disk, so to be sure such new values will be indeed available when the signal you emit is processed. From documentation:

    If there already exists a setting with the same key, the existing value is overwritten by the new value. For efficiency, the changes may not be saved to permanent storage immediately. (You can always call sync() to commit your changes.)

  • @Pablo-J.-Rogina
    I'm already using the sync() method when saving my settings.
    I think the problem is due to the QVariant type and using it in a condition.

  • @DavidM29
    I don't understand, I may be misunderstanding, but....

    In your C++ code which you say works you are checking settings for values false/true. In your JS you are checking for 0/"0"? Make sure you understand just how these settings are actually saved in QSettings/the file?

  • @JonB
    My C++ code say that if the value saved in the file is a string and is "false" or "true" it should return a boolean value.
    But if not it just return the value saved in the file. As you may see there is 2 return. One for the if the other for other cases.

    In my file I do save String, Int and bool values.
    When I checking 0/"0" it is because I'm expeting this specific value to be an int. When I'm looking at it in my file it is an Int.

    Edit :
    I just found out. The QVariant was returning a QVariant which is neither a string or an int I believe. So what I did is that I added this into my getter :

        QVariant value = QSettings::value(key, defaultValue);
        if (QString(value.typeName()) == "QString" &&  (value.toString() == "false" || value.toString() == "true")){
            return QVariant(value.toBool());
        } else if (value.canConvert<int>()){  //This is the new code
            return value.toInt();
        return value;

    So now it checks if it can be converted in an Int and if yes it does return an Int. And now my condition seems to be working fine.

  • @DavidM29
    Sorry I don't really know what you are saying. All I know is your code reads settings.value("Usi/comboParam",0) and compares that against something. Since you're saying you don't understand what that value is/the result of the comparison, please debug it out before comparing so you know what it actually is.

  • @JonB
    I did debug it and when I compared it it was printing 0. But in a type that I did ignore !
    I just edit my previous post. I found a solution.
    Thank you for your help !

Log in to reply