Qt 6.11 is out! See what's new in the release
blog
Update Loaded Component in Stackview
-
Hello everybody,
i try to update the text from a loaded component every second (the text is the current time).
the qml Side is set by an StackView. In the Side a Loader loads the component which includes a text field.
// property string updateTime: secondValue Loader { id: firstBox sourceComponent: greybox height: 80 anchors{ top: mainText.bottom topMargin: 30 left: parent.left leftMargin: 35 right: parent.right rightMargin: 160 } property string firstItem: qsTr("Date:") property string firstValue: currentDate() property string secondItem: qsTr("Time:") property string secondValue: currentTime() property string thirdItem: qsTr("Weekday:") property string thirdValue: currentWeekday() } Timer{ id: timer interval: 1000 repeat: true running: true onTriggered:{ /*what i already try*/ // firstBox.active = false // firstBox.active = true // firstBox.source = "" // firstBox.sourceComponent = greybox // updateTime: currentTime() // greybox.secondValue = currentTime() // firstBox.sourceComponent = greybox /*hole side is reload so interactivity with the side is difficult*/ // pageStack.replace(pageStack.currentItem,"qrc:/qml_Sides/ThisSide.qml") /*here i can see the timer is running*/ console.debug("Hello World") } } /*grey boxes*/ Component{ id: greybox Rectangle { color:"#E9EDF0" border.color: "#e7ebee" Rectangle { color: "#d4d8db" width: 1 height: parent.height anchors.centerIn: parent } Text { id: firstItemText text: firstItem font.pixelSize: 16 anchors { left: parent.left leftMargin: 10 top: parent.top topMargin: 5 } } Text { id: firstValueText text: firstValue font.pixelSize: 16 anchors { left: parent.left leftMargin: 190 top: firstItemText.top } } Text { id: secondItemText text: secondItem font.pixelSize: 16 anchors { left: firstItemText.left top: firstItemText.bottom topMargin: 5 } } Text { id: secondValueText text: secondValue font.pixelSize: 16 anchors { left: firstValueText.left top: secondItemText.top } } Text { id: thirdItemText text: thirdItem font.pixelSize: 16 anchors { left: firstItemText.left top: secondItemText.bottom topMargin: 5 } } Text { id: thirdValueText text: thirdValue font.pixelSize: 16 anchors { left: firstValueText.left top: thirdItemText.top } } } } function currentDate(){ return Qt.formatDate(new Date(),"dd-MM-yyyy"); } function currentTime(){ return Qt.formatTime(new Date(),"hh-mm-ss"); } function currentWeekday(){ var dayInTheWeek = new Date().getDay() var allWeekdays = [qsTr("Sunday"), qsTr("Monday"), qsTr("...."), qsTr("...."), qsTr("...."), qsTr("Friday"), qsTr(".....")]; return allWeekdays[dayInTheWeek] }Is it possible to update components loaded in the Stackview?
And when yes How? -
Public properties/functions can be acessed via
currentItem, e.g:stack.currentItem.myLabel = "Some text". -
Okay Thank you
i solved it like you suggest:
property alias setTime: firstBox.secondValue property alias setDate: firstBox.firstValue property alias setWeekday: firstBox.thirdValue Loader{ ... property string firstValue: currentDate() property string secondValue: currentTime() property string thirdValue: currentWeekday() } Timer{ id: timer interval: 999 repeat: true running: true onTriggered:{ stack.currentItem.setTime = currentTime() stack.currentItem.setDate = currentDate() stack.currentItem.setWeekday = currentWeekday() } }