Settings: how to save the status of multiple switch in a page reused many time
-
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..)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" } } } } } }@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?*/ -
@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.
@gfxx
Bearing in mind I've said I don't do QML...
...Are you sure thoseTypeError: 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.esw0and, for whatever reason, there is noSettingsobject/instance. Then it would know what theesw0property is defined as, but it's trying to get it off a JavaScriptundefinedobject? -
@gfxx
Bearing in mind I've said I don't do QML...
...Are you sure thoseTypeError: 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.esw0and, for whatever reason, there is noSettingsobject/instance. Then it would know what theesw0property is defined as, but it's trying to get it off a JavaScriptundefinedobject? -
@gfxx
Bearing in mind I've said I don't do QML...
...Are you sure thoseTypeError: 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.esw0and, for whatever reason, there is noSettingsobject/instance. Then it would know what theesw0property is defined as, but it's trying to get it off a JavaScriptundefinedobject?@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)
-
@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)
@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 itComponent.onCompleted: console.log( swSE.parent ) -
@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 itComponent.onCompleted: console.log( swSE.parent ) -
@LeLev Yes not exist ... but because not exist parent attribute of Setting or is possible to make parent attribute of Settings?
@gfxx
swSEis yourSettings, andesw0is an attribute of that. I don't understand why you are trying to useparent? 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.) -
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]) } } -
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 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) }...
-
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.a1But 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.
-
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.a1But 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 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"
-
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.a1But 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.
@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.
-
@gfxx
swSEis yourSettings, andesw0is an attribute of that. I don't understand why you are trying to useparent? 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.)@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.
-
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.a1But 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 We actually discuss settings iterated and reuse the page with the settings inside it ... If I understand correctly, this is not possible.
regards
-
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 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 pageI'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
-
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! -
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 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.