Settings: how to save the status of multiple switch in a page reused many time
-
I've add some switch in a repeater in these manner:
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: 32 delegate: Switch { x: 0 width: 190 text: modelTabE[index] checked: swSE.parent["esw"+index] Material.accent: Material.DeepOrange font.family: "Courier" } } } } Settings { id: swSE property bool esw0: false property bool esw1: false property bool esw2: false property bool esw3: false property bool esw4: false property bool esw5: false property bool esw6: false property bool esw8: false property bool esw9: false property bool esw10: false property bool esw11: false property bool esw12: false property bool esw13: false property bool esw14: false property bool esw15: false property bool esw16: false property bool esw17: false property bool esw18: false property bool esw19: false property bool esw20: false property bool esw21: false property bool esw22: false property bool esw23: false property bool esw24: false property bool esw25: false property bool esw26: false property bool esw27: false property bool esw28: false property bool esw29: false property bool esw30: false property bool esw31: false } }But return these error ... and not know why (i'm a beginner in QML)
TypeError: Cannot read property 'esw0' of undefined /* one for every property so 32 error */ someone can suggest my the right doc or way to understand these problem? regards -
I've add some switch in a repeater in these manner:
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: 32 delegate: Switch { x: 0 width: 190 text: modelTabE[index] checked: swSE.parent["esw"+index] Material.accent: Material.DeepOrange font.family: "Courier" } } } } Settings { id: swSE property bool esw0: false property bool esw1: false property bool esw2: false property bool esw3: false property bool esw4: false property bool esw5: false property bool esw6: false property bool esw8: false property bool esw9: false property bool esw10: false property bool esw11: false property bool esw12: false property bool esw13: false property bool esw14: false property bool esw15: false property bool esw16: false property bool esw17: false property bool esw18: false property bool esw19: false property bool esw20: false property bool esw21: false property bool esw22: false property bool esw23: false property bool esw24: false property bool esw25: false property bool esw26: false property bool esw27: false property bool esw28: false property bool esw29: false property bool esw30: false property bool esw31: false } }But return these error ... and not know why (i'm a beginner in QML)
TypeError: Cannot read property 'esw0' of undefined /* one for every property so 32 error */ someone can suggest my the right doc or way to understand these problem? regards -
I've add some switch in a repeater in these manner:
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: 32 delegate: Switch { x: 0 width: 190 text: modelTabE[index] checked: swSE.parent["esw"+index] Material.accent: Material.DeepOrange font.family: "Courier" } } } } Settings { id: swSE property bool esw0: false property bool esw1: false property bool esw2: false property bool esw3: false property bool esw4: false property bool esw5: false property bool esw6: false property bool esw8: false property bool esw9: false property bool esw10: false property bool esw11: false property bool esw12: false property bool esw13: false property bool esw14: false property bool esw15: false property bool esw16: false property bool esw17: false property bool esw18: false property bool esw19: false property bool esw20: false property bool esw21: false property bool esw22: false property bool esw23: false property bool esw24: false property bool esw25: false property bool esw26: false property bool esw27: false property bool esw28: false property bool esw29: false property bool esw30: false property bool esw31: false } }But return these error ... and not know why (i'm a beginner in QML)
TypeError: Cannot read property 'esw0' of undefined /* one for every property so 32 error */ someone can suggest my the right doc or way to understand these problem? regardsHi,
Please tell us explicitly what you are trying to do@gfxx said in TypeError: Cannot read property 'esw0' of undefined Repeater + Settings:
checked: swSE.parent["esw"+index]
you can not do this.
If your aim is to save the state (checked property) of your Switchs , please see if the approach used here can help
https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodel -
@gfxx
In yourSettings, fromesw5onward you initialize them all tofalse, which is fine. But foresw0throughesw4you try to initialize them to "themselves" (property bool esw0: esw0), which can't be right? Then again, I've never done QML.@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.
-
Hi,
Please tell us explicitly what you are trying to do@gfxx said in TypeError: Cannot read property 'esw0' of undefined Repeater + Settings:
checked: swSE.parent["esw"+index]
you can not do this.
If your aim is to save the state (checked property) of your Switchs , please see if the approach used here can help
https://stackoverflow.com/questions/48730166/how-to-save-and-restore-the-content-of-a-listmodel@LeLev said in TypeError: Cannot read property 'esw0' of undefined Repeater + Settings:
If your aim is to save the state (checked property) of your Switchs , please see if the approach used here can help
yes I'm try to do these .... but I create a page with many switch and reuse it more time (30 more or less). i need to save the status of the switches whenever it is needed (onDestruction, button.onPressed etc etc). I should save the status of the switches on every single page. From what I understand you can't use array or vector of switch in qml if you want to use Settings .... so I'm looking for the right way to do it without writing thousands of boring lines of code.
not sure your link is the solution .... not have the model of switch only repeater with numeric model. Plus I'm novice in qml/java ....
From what I understand I should use, in my case, a listview with a specially created model, perhaps with ListModel, and then serialize the data. Should I serialize only the checked value or can I serialize other data? In C ++ I would use a vector or array of arrays ... in qml I don't know.
-
@LeLev said in TypeError: Cannot read property 'esw0' of undefined Repeater + Settings:
If your aim is to save the state (checked property) of your Switchs , please see if the approach used here can help
yes I'm try to do these .... but I create a page with many switch and reuse it more time (30 more or less). i need to save the status of the switches whenever it is needed (onDestruction, button.onPressed etc etc). I should save the status of the switches on every single page. From what I understand you can't use array or vector of switch in qml if you want to use Settings .... so I'm looking for the right way to do it without writing thousands of boring lines of code.
not sure your link is the solution .... not have the model of switch only repeater with numeric model. Plus I'm novice in qml/java ....
From what I understand I should use, in my case, a listview with a specially created model, perhaps with ListModel, and then serialize the data. Should I serialize only the checked value or can I serialize other data? In C ++ I would use a vector or array of arrays ... in qml I don't know.
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" } } } } } } -
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.