Solved Change component's child propery from javascript file
-
@behruz-montazeri
Do a Google search for that: JavaScript + static local variable.
e.g. https://stackoverflow.com/questions/1535631/static-variables-in-javascript -
@Diracsbracket
I did one. I changed my function. But here's another problem it work for only one qml file and if we call it from another qml file counter is Zero.function changeColor(rectId, param1) { if( typeof changeColor.counter == 'undefined' ) { changeColor.counter = 0; } changeColor.counter++ }
-
@behruz-montazeri
Then use a global JS variable. -
@Diracsbracket
Yes that's the case.property int counter: 0
-
@behruz-montazeri
that's not a JS variable. That's a QML object property. -
@Diracsbracket
Where can i declare a javascript global variable ? -
@behruz-montazeri said in Change component's child propery from javascript file:
@Diracsbracket
Where can i declare a javascript global variable ?In a javascript file. Otherwise create a property at a top-level QML object, e.g. your main window, which exists for the duration of your app.
-
@Diracsbracket
I guess creating property make seance in my situation. making a Javascript file variable doesn't keep track of increasing despite the several calls from different QML files. -
@behruz-montazeri said in Change component's child propery from javascript file:
I guess creating property make seance in my situation.
That's usually the best way indeed.
making a Javascript file variable doesn't keep track of increasing despite the several calls from different QML files.
You can do it, if you really need it:
http://doc.qt.io/qt-5/qtqml-javascript-resources.html#shared-javascript-resources-libraries -
I have a list view i want to change the color of all rectangle inside of component with Javascript.
ListView{ id:fruitList anchors.fill: parent model: mModel delegate: mComponent highlight: Rectangle{ width: parent.width color: "red" } Component{ id:mComponent Item { width:parent.width height: rectId.height Rectangle{ id:rectId width: parent.width height: 80 border.width: 6 border.color: "black" Image { anchors.centerIn: parent width:70 height: width id: mPic source: msource } ItemDelegate{ anchors.fill: parent onClicked: { MyColor.changeColor(rectId,colorM) mText = MyColor.textReturn("page1") } } } } }
in Javascript :
function resetList() { for (var entry = 0; entry < fruitList.count; ++entry) { // rectId.color = "white' } }
-
@behruz-montazeri
I think this thread is closed. Thanks. -
@Diracsbracket
Is it possible to ask in new topic ?