我正在寻找从数组 ( calendarListModel
) 中的布尔值更改日历单元格的颜色。
这是数组的示例:
[
{"date":1544443200000,"name":"Edward","status":1},
{"date":1544529600000,"name":"Katie","status":0},
{"date":1544616000000,"name":"Claire","status":1}
]
我想要做的是根据calendarListModel
( "status": 0 or 1
)中的布尔值更改日历标记的颜色。
我的标记代码是;
Rectangle {
id: calendarMarker
visible: arrayFromFireBase.indexOf(styleData.date.getTime()) > -1
anchors.fill: parent
color: "blue"
}
我尝试使用诸如calendarListModel.status ? "blue" : "red"
其他变化之类的东西来更改矩形的颜色,但是要么将每个单元格设为蓝色或红色,要么根本没有?
问题:根据数组中的真/假值更改颜色的最佳方法是什么?
我使用Calendar
fromQtQuick.Controls
来显示日期和CalendarStyle
fromQtQuick.Controls.Styles
来分配自定义样式。我也在使用 V-Play sdk。
这是我目前正在做的一个最小、完整且可验证的示例:
import VPlayApps 1.0
import QtQuick 2.2
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.1
App {
id: app
// this model is read from a firebase db using v-play
property var calendarListModel: [
{"date":1544443200000,"name":"Edward","status":1},
{"date":1544529600000,"name":"Katie","status":0},
{"date":1547182800000,"name":"Claire","status":1}
]
Calendar {
id: calendar
anchors.fill: parent
selectedDate: new Date()
weekNumbersVisible: true
focus: true
onSelectedDateChanged: {
const day = selectedDate.getDate();
const month = selectedDate.getMonth() + 1;
const year = selectedDate.getFullYear();
}
style: CalendarStyle {
dayOfWeekDelegate: Item {
width: parent.width
height: dp(30)
Rectangle {
anchors.fill: parent
border.color: "#00000000"
Label {
id: dayOfWeekDelegateText
text: Qt.locale().dayName(styleData.dayOfWeek, Locale.ShortFormat)
anchors.centerIn: parent
color: "black"
}
}
}
// a delegate for each day cell
dayDelegate: Item {
id: container
readonly property color sameMonthDateTextColor: "#444"
readonly property color selectedDateColor: "#20d5f0"
readonly property color selectedDateTextColor: "white"
readonly property color differentMonthDateTextColor: "#bbb"
// the background of each cell
Rectangle {
anchors.fill: parent
border.color: "#00000000"
color: styleData.selected ? selectedDateColor : "white"
}
// a marker on the upper-left corner on each cell
Rectangle {
id: calendarMarker
property bool isConfirmed: false
anchors {
top: parent.top; left: parent.left
}
width: 12
height: width
// the issue lies here
color: {
var confCol
calendarListModel.indexOf(status? true : false)
confCol ? "#4286f4" : "red"
}
}
// the day number
Label {
id: dayDelegateText
text: styleData.date.getDate()
anchors.centerIn: parent
color: styleData.selected ? selectedDateTextColor : styleData.visibleMonth ? sameMonthDateTextColor : differentMonthDateTextColor
}
}
}
}
}