Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Updating UI when update QSettings
Forum Updated to NodeBB v4.3 + New Features

Updating UI when update QSettings

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.4k 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.
  • D Offline
    D Offline
    DavidM29
    wrote on last edited by DavidM29
    #1

    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;
    
    Pablo J. RoginaP 1 Reply Last reply
    0
    • D DavidM29

      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;
      
      Pablo J. RoginaP Offline
      Pablo J. RoginaP Offline
      Pablo J. Rogina
      wrote on last edited by
      #2

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

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      D 1 Reply Last reply
      2
      • Pablo J. RoginaP Pablo J. Rogina

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

        D Offline
        D Offline
        DavidM29
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • D DavidM29

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

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

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

          D 1 Reply Last reply
          0
          • JonBJ JonB

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

            D Offline
            D Offline
            DavidM29
            wrote on last edited by DavidM29
            #5

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

            JonBJ 1 Reply Last reply
            1
            • D DavidM29

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

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

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

              D 1 Reply Last reply
              0
              • JonBJ JonB

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

                D Offline
                D Offline
                DavidM29
                wrote on last edited by
                #7

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

                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