accessing "child" (perhaps) properties
-
Hello,
lets say I have this:
main.qml:ApplicationWindow { id: mainWindow StackView { id: view anchors.fill: parent initialItem: view_InitialScreen } Component { id: view_InitialScreen InitialScreen {} } Component.OnCompleted: { console.log(view_InitialScreen.parentObject.testRect.color); } }
InitialScreen.qml:
Item { id: parentObject Rectangle { id: testRect color: "white" } }
so the testRect.color is obviously not accessible from main.qml component.oncompleted...
is there a way to reach out there? -
@J-Hilk said in accessing "child" (perhaps) properties:
@shokarta did you notice, that I removed
parentObject
in the console log ?did you do the same?
My bad, you can't access Componets that way,
try this:
Component.onCompleted: { console.log(view.currentItem.tRect.color); }
with InitialItem:
Item { property alias tRect: testRect id: parentObject Rectangle { id: testRect color: "white" } }
-
@shokarta yes, with alias, either alias the Component or the property
Item { id: parentObject property alias tRect: testRect Rectangle { id: testRect color: "white" } } ----- Component.OnCompleted: { console.log(view_InitialScreen.tRect.color); }
Item { id: parentObject property alias testRectColor: testRect.color Rectangle { id: testRect color: "white" } } ----- Component.OnCompleted: { console.log(view_InitialScreen.testRectColor); }
-
@J-Hilk
Sorry, none of options work to me.1st gives me "qml: undefined"
2nd gives me "qrc:/main.qml:190: TypeError: Cannot read property 'color' of undefined"I did set the alias property quite fine, just into Item (parentObject), but then even when I started to type view_InitialScreen.t... inside Component.OnCompleted inside ApplicationWindows (mainWindow) its not even offering me the variable tRect or restRectColor...
-
@J-Hilk said in accessing "child" (perhaps) properties:
@shokarta did you notice, that I removed
parentObject
in the console log ?did you do the same?
My bad, you can't access Componets that way,
try this:
Component.onCompleted: { console.log(view.currentItem.tRect.color); }
with InitialItem:
Item { property alias tRect: testRect id: parentObject Rectangle { id: testRect color: "white" } }
-
@J-Hilk said in accessing "child" (perhaps) properties:
view.currentItem
yes, this work the way i need :) thanks
Can I also modify it like that?so isntead of:
Component.onCompleted: { console.log(view.currentItem.tRect.color); }
i would do:
Component.onCompleted: { view.currentItem.tRect.color = "red"; }
should it work?
can i also replace "currentItem" with direct id of the Component{} ? because i want not to get it from currentItem, but from "view_InitialScreen" (which ironicaly is not initial :D)
-
@shokarta said in accessing "child" (perhaps) properties:
should it work?
sure why not?
can i also replace "currentItem" with direct id of the Component{}
No, components are not instances, you can push the InitialItem 200 times on the stack view, how would you differentiate between them?
You should however be able to use the StackView methods https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#methods
to address specific items(Components) by their index
-
@J-Hilk said in accessing "child" (perhaps) properties:
to address specific items(Components) by their index
why do i have to use stackview at all?
basicaly i wish to get the property via alias from the Item "parentObject", at the end this does not have to be bound to StackView, or does it?what you are saying is exactly my problem:
@J-Hilk said in accessing "child" (perhaps) properties:No, components are not instances, you can push the InitialItem 200 times on the stack view, how would you differentiate between them?
so if im pushing back and forward on each view, isnt it at the end the same view (not cloned like you mentione) just at the very top of all other views? or by each stacview.push() im cloning the view into new one and placing it atop?
- EDIT: i have tried to place onsole.log(currentItem) inside StackView{} to see how it will be changing, then i was clicking back and forth between two views and this is what i get:
qml: InitialScreen_QMLTYPE_1(0x768e90) qml: ChoiseOffline_QMLTYPE_3(0xcbdd090) qml: InitialScreen_QMLTYPE_1(0xcd16340) qml: ChoiseOffline_QMLTYPE_3(0xce102a0) qml: InitialScreen_QMLTYPE_1(0xce6b230) qml: ChoiseOffline_QMLTYPE_3(0xce6b3b0) qml: InitialScreen_QMLTYPE_1(0xce9af60)
so its realy generating new index each time i push().
so is it posible somehow search the newest view by its name? So i wish to search InitialScreen newest index of StackView... possible somehow?