我希望从数组(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"等东西改变矩形的颜色,但是要么让每个单元格变成蓝色或红色,要么一点也不做?
问题:根据数组中的真假值更改颜色的最佳方法是什么?
我使用来自QtQuick.Controls的QtQuick.Controls显示日期,使用CalendarStyle从QtQuick.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
}
}
}
}
}发布于 2019-01-08 15:36:43
好的,现在你正在使用一个对象数组,并试图直接索引它,这是不好的。(特别是因为您目前正在为true/false值进行索引,它不包含calendarListModel中的单个元素。)即使您可以用日期、名称和状态重构对象,它们仍然是不同的对象。
color: {
var modelObject = calendarListModel.find(
function(obj) {
// look for a matching date
return obj.date === styleData.date.getTime();
}
);
if (modelObject === undefined) // not found in list model
return "lightgrey";
return modelObject.status ? "blue" : "red";
}下面是从一个macOS测试的前后两个部分:
注意所有标记都是红色的。只有calendarListModel中指定的日期才有颜色。

注意,只有1月11日的标记颜色(蓝色)。事实上,这是因为日期包含在calendarListModel:1544616000000中。

https://stackoverflow.com/questions/54014711
复制相似问题