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. Settings: how to save the status of multiple switch in a page reused many time
Qt 6.11 is out! See what's new in the release blog

Settings: how to save the status of multiple switch in a page reused many time

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
24 Posts 4 Posters 4.3k 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.
  • ODБOïO ODБOï

    You can do it in c++ if you want.
    In pure qml /javascript you can do like this (this went little bit wrong with the "true" but works..)

    @gfxx

    import QtQuick 2.9
    import QtQuick.Controls 2.5
    import Qt.labs.settings 1.0
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        id:rt
    
        Settings {
            property alias settingData: rt.settingData
        }
    
        property var settingData: [] //
    
        onClosing: {
            var datamodel = []
    
            for (var i = 0; i < emRep.model; ++i) {
                if(emRep.itemAt(i).checked){
                    datamodel.push("true")
                }
                else{
                    datamodel.push("false")
                }
            }
            settingData = datamodel
        }
    
    
        Rectangle {
            id: myRect
            /* layout definition*/
    
            Row {
                id: rowRep3
                x: 0
                y: 0
                width: 400
                height: 618
                Grid {
                    id: clnm1AB2
                    y: 0
                    x: 20
                    width: 380
                    height: 618
                    spacing: -10
                    columns: 2
                    rows: 16
                    Repeater {
                        id: emRep
                        model: 5
                        Switch {
    
                            x: 0
                            width: 190
                            checked: settingData[index] === "true" ? true : false
                            font.family: "Courier"
                        }
                    }
                }
            }
        }
    }
    
    gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by
    #7

    @LeLev wow ... thanks! ... but if reuse the page (so not on ApplicationWindows location, but on swipePage for example, because contains other nested pages) i must use in these manner:

    Settings {
            property alias settingData: mySwipePage.myNestedPage1.settingData1 /*with datamodel1*/
            property alias settingData: mySwipePage.myNestedPage2.settingData2 /*with datamodel2*/
            property alias settingData: mySwipePage.myNestedPage3.settingData3 /*with datamodel3*/
            property alias settingData: mySwipePage.myNestedPage4.settingData4 /*with datamodel4*/
    
        }
    

    or only in these one:

    property alias settingData: rt.settingData  /* so only in mainApp page?*/
    

    bkt

    1 Reply Last reply
    0
    • gfxxG gfxx

      @JonB said in TypeError: Cannot read property 'esw0' of undefined Repeater + Settings:

      In your Settings, from esw5 onward you initialize them all to false, which is fine. But for esw0 through esw4 you try to initialize them to "themselves" (property bool esw0: esw0), which can't be right? Then again, I've never done QML.

      this is an horrible copy and paste error when write these post ... I correct it.

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

      @gfxx
      Bearing in mind I've said I don't do QML...
      ...Are you sure those

      TypeError: Cannot read property 'esw0' of undefined /* one for every property so 32 error */

      are coming as QML is encountering ("compiling"?) the lines you show, because they look alright to me? It sounds (to me) like a runtime error when your code is executing, where somewhere you are accessing a Settings.esw0 and, for whatever reason, there is no Settings object/instance. Then it would know what the esw0 property is defined as, but it's trying to get it off a JavaScript undefined object?

      ODБOïO gfxxG 2 Replies Last reply
      0
      • JonBJ JonB

        @gfxx
        Bearing in mind I've said I don't do QML...
        ...Are you sure those

        TypeError: Cannot read property 'esw0' of undefined /* one for every property so 32 error */

        are coming as QML is encountering ("compiling"?) the lines you show, because they look alright to me? It sounds (to me) like a runtime error when your code is executing, where somewhere you are accessing a Settings.esw0 and, for whatever reason, there is no Settings object/instance. Then it would know what the esw0 property is defined as, but it's trying to get it off a JavaScript undefined object?

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #9

        @JonB hi ,
        the TypeError is because of this line in the Repeater i belive

        checked: swSE.parent["esw"+index]
        
        1 Reply Last reply
        2
        • JonBJ JonB

          @gfxx
          Bearing in mind I've said I don't do QML...
          ...Are you sure those

          TypeError: Cannot read property 'esw0' of undefined /* one for every property so 32 error */

          are coming as QML is encountering ("compiling"?) the lines you show, because they look alright to me? It sounds (to me) like a runtime error when your code is executing, where somewhere you are accessing a Settings.esw0 and, for whatever reason, there is no Settings object/instance. Then it would know what the esw0 property is defined as, but it's trying to get it off a JavaScript undefined object?

          gfxxG Offline
          gfxxG Offline
          gfxx
          wrote on last edited by gfxx
          #10

          @JonB said in Settings: how to save the status of multiple switch in a page reused many time:

          where somewhere you are accessing a Settings.esw0 and, for whatever reason, there is no Settings object/instance.

          Yes you are in right (but not about runtime).... but these because Settings is Sqlite based system ... so it no appreciate non unique index data .... yes, but I suspect it is because Settings is a system based on Sqlite queries and probably with a single table, so the non-unique indexes and names are not appreciated. We must try to get around this obstacle. In C ++ I have always used special databases, never even used QSetting before now. ..... all stems from the fact that as you may have noticed the programmers, all of them, are basic lazy people;))) jokes aside, I really don't think that Setting is created to instantiate multiple tables of data at the same time ... a shame.

          @LeLev said in Settings: how to save the status of multiple switch in a page reused many time:

          @JonB hi ,
          the TypeError is because of this line in the Repeater i belive
          checked: swSE.parent["esw"+index]

          that's exactly what I think (and the summary of my talk above)

          bkt

          ODБOïO 1 Reply Last reply
          0
          • gfxxG gfxx

            @JonB said in Settings: how to save the status of multiple switch in a page reused many time:

            where somewhere you are accessing a Settings.esw0 and, for whatever reason, there is no Settings object/instance.

            Yes you are in right (but not about runtime).... but these because Settings is Sqlite based system ... so it no appreciate non unique index data .... yes, but I suspect it is because Settings is a system based on Sqlite queries and probably with a single table, so the non-unique indexes and names are not appreciated. We must try to get around this obstacle. In C ++ I have always used special databases, never even used QSetting before now. ..... all stems from the fact that as you may have noticed the programmers, all of them, are basic lazy people;))) jokes aside, I really don't think that Setting is created to instantiate multiple tables of data at the same time ... a shame.

            @LeLev said in Settings: how to save the status of multiple switch in a page reused many time:

            @JonB hi ,
            the TypeError is because of this line in the Repeater i belive
            checked: swSE.parent["esw"+index]

            that's exactly what I think (and the summary of my talk above)

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #11

            @gfxx said in Settings: how to save the status of multiple switch in a page reused many time:

            summary

            checked: swSE.parent["esw"+index]
            

            you are trying to access your settings using parent[] attribute, that don't exist
            check it

            Component.onCompleted: console.log( swSE.parent  )
            
            gfxxG 1 Reply Last reply
            1
            • ODБOïO ODБOï

              @gfxx said in Settings: how to save the status of multiple switch in a page reused many time:

              summary

              checked: swSE.parent["esw"+index]
              

              you are trying to access your settings using parent[] attribute, that don't exist
              check it

              Component.onCompleted: console.log( swSE.parent  )
              
              gfxxG Offline
              gfxxG Offline
              gfxx
              wrote on last edited by
              #12

              @LeLev Yes not exist ... but because not exist parent attribute of Setting or is possible to make parent attribute of Settings?

              bkt

              JonBJ 1 Reply Last reply
              0
              • gfxxG gfxx

                @LeLev Yes not exist ... but because not exist parent attribute of Setting or is possible to make parent attribute of Settings?

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

                @gfxx
                swSE is your Settings, and esw0 is an attribute of that. I don't understand why you are trying to use parent? Now that I understand what you're trying to do, don't you simply intend: swSE["esw"+index]? (Whether that works or not is another matter.)

                gfxxG 1 Reply Last reply
                2
                • ODБOïO Offline
                  ODБOïO Offline
                  ODБOï
                  wrote on last edited by ODБOï
                  #14

                  you can do like this

                  Component.onCompleted: console.log( swSE.values[0]  )
                      Settings {
                          id: swSE
                          property variant values : [false,false,true]
                      ...
                  

                  but cant do this swSE["esw0"]

                  1 Reply Last reply
                  1
                  • Shrinidhi UpadhyayaS Offline
                    Shrinidhi UpadhyayaS Offline
                    Shrinidhi Upadhyaya
                    wrote on last edited by
                    #15

                    Hi @gfxx , you can do like this

                    Here is a sample code:-

                    Settings.qml

                    Rectangle {
                           id: root
                    
                           anchors.fill: parent
                           color: "grey"
                    
                           property bool a0: false
                           property bool a1: false
                           property bool a2: false
                           property bool a3: false
                           property bool a4: false
                           property bool a5: true
                           property bool a6: true
                           property bool a7: true
                           property bool a8: true
                           property bool a9: true
                    }
                    

                    main.qml

                     Settings {
                            id: dummySettings
                        }
                    
                    Component.onCompleted: {
                            for(var i=0;i < 10;i++) {
                                console.log(dummySettings["a" + i])
                            }
                        }
                    

                    Shrinidhi Upadhyaya.
                    Upvote the answer(s) that helped you to solve the issue.

                    ODБOïO gfxxG 2 Replies Last reply
                    1
                    • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                      Hi @gfxx , you can do like this

                      Here is a sample code:-

                      Settings.qml

                      Rectangle {
                             id: root
                      
                             anchors.fill: parent
                             color: "grey"
                      
                             property bool a0: false
                             property bool a1: false
                             property bool a2: false
                             property bool a3: false
                             property bool a4: false
                             property bool a5: true
                             property bool a6: true
                             property bool a7: true
                             property bool a8: true
                             property bool a9: true
                      }
                      

                      main.qml

                       Settings {
                              id: dummySettings
                          }
                      
                      Component.onCompleted: {
                              for(var i=0;i < 10;i++) {
                                  console.log(dummySettings["a" + i])
                              }
                          }
                      
                      ODБOïO Offline
                      ODБOïO Offline
                      ODБOï
                      wrote on last edited by ODБOï
                      #16

                      @Shrinidhi-Upadhyaya said in Settings: how to save the status of multiple switch in a page reused many time:

                      Rectangle

                      we are talking about Settings QML Type
                      +sorry but your code does not make sense i belive.. , you can not iterate like that. What you can do in your example is :

                      for (var i in dummySettings){
                       console.log(i)
                      }
                      

                      ...

                      1 Reply Last reply
                      0
                      • Shrinidhi UpadhyayaS Offline
                        Shrinidhi UpadhyayaS Offline
                        Shrinidhi Upadhyaya
                        wrote on last edited by Shrinidhi Upadhyaya
                        #17

                        Hi @LeLev , actually you guys were discussing about how you can access a variable for example like: esw0,esw1 etc and you have replied that you cant do this swSE["esw0"], but actually you can do it right, the code which i have written is just a sample code, you can replace the rectangle with Settings and inside that create 10variables like esw0,esw1 etc after you can access that in main.qml like the way i have done.

                        According to you, we can access variable only by this way

                        dummySettings.a0,dummySettings.a1
                        

                        But we can access the variable by the way i have done.

                        If you copy paste my code, you will be able to see that,you will get the values printed in the console that means you are able to access the variable.

                        Shrinidhi Upadhyaya.
                        Upvote the answer(s) that helped you to solve the issue.

                        ODБOïO gfxxG 3 Replies Last reply
                        3
                        • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                          Hi @LeLev , actually you guys were discussing about how you can access a variable for example like: esw0,esw1 etc and you have replied that you cant do this swSE["esw0"], but actually you can do it right, the code which i have written is just a sample code, you can replace the rectangle with Settings and inside that create 10variables like esw0,esw1 etc after you can access that in main.qml like the way i have done.

                          According to you, we can access variable only by this way

                          dummySettings.a0,dummySettings.a1
                          

                          But we can access the variable by the way i have done.

                          If you copy paste my code, you will be able to see that,you will get the values printed in the console that means you are able to access the variable.

                          ODБOïO Offline
                          ODБOïO Offline
                          ODБOï
                          wrote on last edited by ODБOï
                          #18

                          @Shrinidhi-Upadhyaya said in Settings: how to save the status of multiple switch in a page reused many time:

                          actually you can do it right

                          yes, in fact we can, i didn't knew that

                          @Shrinidhi-Upadhyaya said in Settings: how to save the status of multiple switch in a page reused many time:

                          actually you guys were discussing about how you can access a variable for example like: esw0,esw1

                          the original topic is "how to save Ui state"

                          1 Reply Last reply
                          0
                          • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                            Hi @LeLev , actually you guys were discussing about how you can access a variable for example like: esw0,esw1 etc and you have replied that you cant do this swSE["esw0"], but actually you can do it right, the code which i have written is just a sample code, you can replace the rectangle with Settings and inside that create 10variables like esw0,esw1 etc after you can access that in main.qml like the way i have done.

                            According to you, we can access variable only by this way

                            dummySettings.a0,dummySettings.a1
                            

                            But we can access the variable by the way i have done.

                            If you copy paste my code, you will be able to see that,you will get the values printed in the console that means you are able to access the variable.

                            gfxxG Offline
                            gfxxG Offline
                            gfxx
                            wrote on last edited by
                            #19

                            @all

                            as I can understand ... is not possible "reuse" Settings because is only a table on Sqlite db. But these notice is only my suspicious, not other.
                            If is right, every time the system make a query on that table (only one) these is not so flexible .... I think is better to use a db instead Settings. In these way I can insert my query create(if not exist) - insert - select - update on myPageComponent and with the use of property string myNewTable I can set outside from myPageComponent, for example in my mainPage or in my nestedMainPage, a new name of data table, so if i create for example 1000 new nested page, I have only the problem of set new table name, and I can make these with a for cicle for example .... better than 1000 of other things.

                            Any how if someone have notice that these is possible using Settings .... please I need to know these.

                            regards.

                            bkt

                            1 Reply Last reply
                            0
                            • JonBJ JonB

                              @gfxx
                              swSE is your Settings, and esw0 is an attribute of that. I don't understand why you are trying to use parent? Now that I understand what you're trying to do, don't you simply intend: swSE["esw"+index]? (Whether that works or not is another matter.)

                              gfxxG Offline
                              gfxxG Offline
                              gfxx
                              wrote on last edited by
                              #20

                              @JonB yes these work ... these is a news for my .... sorry. I'm a little bit confusing about QSettings work.

                              Any how in witch way these can be reused? Only make a new

                              Setting { id: newSetting1; }
                              

                              I think ..... and outside of NestedPage .... so I think I have to run iterate outside of nestedPage and the use per connect property has checked the setting .... a long story how can I see .. .. but it could be that I didn't understand all these things.

                              bkt

                              1 Reply Last reply
                              0
                              • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                                Hi @LeLev , actually you guys were discussing about how you can access a variable for example like: esw0,esw1 etc and you have replied that you cant do this swSE["esw0"], but actually you can do it right, the code which i have written is just a sample code, you can replace the rectangle with Settings and inside that create 10variables like esw0,esw1 etc after you can access that in main.qml like the way i have done.

                                According to you, we can access variable only by this way

                                dummySettings.a0,dummySettings.a1
                                

                                But we can access the variable by the way i have done.

                                If you copy paste my code, you will be able to see that,you will get the values printed in the console that means you are able to access the variable.

                                gfxxG Offline
                                gfxxG Offline
                                gfxx
                                wrote on last edited by
                                #21

                                @Shrinidhi-Upadhyaya We actually discuss settings iterated and reuse the page with the settings inside it ... If I understand correctly, this is not possible.

                                regards

                                bkt

                                1 Reply Last reply
                                0
                                • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                                  Hi @gfxx , you can do like this

                                  Here is a sample code:-

                                  Settings.qml

                                  Rectangle {
                                         id: root
                                  
                                         anchors.fill: parent
                                         color: "grey"
                                  
                                         property bool a0: false
                                         property bool a1: false
                                         property bool a2: false
                                         property bool a3: false
                                         property bool a4: false
                                         property bool a5: true
                                         property bool a6: true
                                         property bool a7: true
                                         property bool a8: true
                                         property bool a9: true
                                  }
                                  

                                  main.qml

                                   Settings {
                                          id: dummySettings
                                      }
                                  
                                  Component.onCompleted: {
                                          for(var i=0;i < 10;i++) {
                                              console.log(dummySettings["a" + i])
                                          }
                                      }
                                  
                                  gfxxG Offline
                                  gfxxG Offline
                                  gfxx
                                  wrote on last edited by gfxx
                                  #22

                                  @Shrinidhi-Upadhyaya yes your code sample work in my app too .... but for use it I must:

                                  *declare 100 property (if I have only 100 of these)
                                  *on top of nested page iterate trought Setting file .... than connect the result on bottom nested page

                                  I'm really new in qml. But as I understand I have to declare property bool axx for every single variable and the reuse of the page becomes boring to do :(

                                  I'm in wrong?

                                  if they are true ... it is better to use the old queries and a specific sqlite database for custom setting operations.

                                  I appreciate any reply about.

                                  regards

                                  bkt

                                  1 Reply Last reply
                                  0
                                  • Shrinidhi UpadhyayaS Offline
                                    Shrinidhi UpadhyayaS Offline
                                    Shrinidhi Upadhyaya
                                    wrote on last edited by
                                    #23

                                    Hi @gfxx , iam sorry for understanding your question in a wrong way, i had thought that Settings is a different page.Okay but still if you want to use properties inside Settings type, you can do it in the way i have told.

                                    I did not get your point "Settings Iterated", can you explain me a bit, so that i can help your or at least it can be helpful to me also.
                                    And what do you mean by "100+ 100 declaration of property" ? did not get it actually!

                                    Shrinidhi Upadhyaya.
                                    Upvote the answer(s) that helped you to solve the issue.

                                    gfxxG 1 Reply Last reply
                                    0
                                    • Shrinidhi UpadhyayaS Shrinidhi Upadhyaya

                                      Hi @gfxx , iam sorry for understanding your question in a wrong way, i had thought that Settings is a different page.Okay but still if you want to use properties inside Settings type, you can do it in the way i have told.

                                      I did not get your point "Settings Iterated", can you explain me a bit, so that i can help your or at least it can be helpful to me also.
                                      And what do you mean by "100+ 100 declaration of property" ? did not get it actually!

                                      gfxxG Offline
                                      gfxxG Offline
                                      gfxx
                                      wrote on last edited by gfxx
                                      #24

                                      @Shrinidhi-Upadhyaya said in Settings: how to save the status of multiple switch in a page reused many time:

                                      i had thought that Settings is a different page

                                      I have main + some top nested page + some bottom nested page inside some one of the top nested. The most part is a reused page. So I need to save state of all switch control .... Is really a good ideas to use setting ? As I understand is not good. But I repeat I'm really new in qml.

                                      100 + 100 is my error . ++sorry. I correct the post.

                                      bkt

                                      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