我在 QML 中有以下 ListView:
ListView{
id:daylistView
width:parent.width
height:parent.height - eventDayLabel.height
boundsBehavior:Flickable.StopAtBounds
highlightRangeMode: ListView.StrictlyEnforceRange
orientation: Qt.Vertical
anchors.top:eventDateRow.bottom
clip:true
focus:true
model: 24
currentIndex : calendarMonth.selectedDate.getDate() === new Date().getDate()
&& calendarMonth.selectedDate.getMonth() === new Date().getMonth()?getHour():12
interactive: true
delegate: Item{
id:hourItem
property var hourTime:hourweeklistviewLabel
width: daylistView.width
height: 60
MouseArea{
anchors.fill:parent
onClicked:{
windowLoader.active =true
daylistView.currentIndex=index
}
}
Rectangle {
z:4
id:hourdaylistviewindexLine
y:getdayMinute()
width:hourItem.width
height:2
border.color: calendarMonth.selectedDate.getDate()===new Date().getDate()
&& getHour() === hourweeklistviewLabel.text
&& calendarMonth.selectedDate.getMonth() === new Date().getMonth()
?"red" : "transparent"
border.width:1
}
Rectangle {
z:4
id:hourline
anchors.verticalCenter: daylistView
width:daylistView.width
height:2
border.color: "lightgray"
border.width:1
}
Label{
z:4
id:hourweeklistviewLabel
anchors.verticalCenter: parent
text:['00','01','02','03',
'04','05','06','07','08','09','10','11','12','13',
'14','15','16','17','18','19','20','21','22','23'][index]
color: getHour() === hourweeklistviewLabel.text
&& calendarMonth.selectedDate.getDate() === new Date().getDate()
&& calendarMonth.selectedDate.getDay() === new Date().getDay()
? systemPalette.highlight : "lightgray"
}
Label{
z:4
id:notesLabel
anchors.left:hourweeklistviewLabel.right
//text:" "
}
}
}
}
這是一個日歷應用程式,其中 ListView 代表每小時的時間。委托包括一個滑鼠區域、兩個矩形和兩個標簽。一個標簽是只讀的。在另一個標簽中,我想從 ListView 外部的 TextInput 設定文本,從另一個視窗。更多的目標是在特定的小時時間顯示標簽,這意味著在 ListView 的特定行中。直到現在我設法獲取 currentItem 小時,但我無法在 ListView 中的標簽中設定文本在 currentIndex 中,雖然我嘗試了很多在 web 中找到的建議方法。如果我找到了一種方法來設定當前專案標簽中的文本,那么我可以選擇在 Listview 中的任何其他索引中設定它。
uj5u.com熱心網友回復:
正如@SoheilArmin 評論的那樣,解決方案在于將 Label 文本系結到一個屬性。在 ListView 代碼中:
delegate: Item{
id:hourItem
property var hourTime:hourweeklistviewLabel
**property var notetaking: notesLabel**
width: daylistView.width
height: 60
設定property var notetaking: notesLabel。
然后在文本欄位中:
TextField {
id:title
x:50
y:20
placeholderText :'Enter Note'
text:daylistView.currentItem.notetaking.text
}
放 text:daylistView.currentItem.notetaking.text
因此,我們有一個雙向系結。還定義一個按鈕,以便可以將文本設定為標簽:
Button {
id: button
text: qsTr("Add Note")
anchors.centerIn:parent
onClicked: {
if (title.text !==""){daylistView.currentItem.notetaking.text= title.text}
else{}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317328.html
