在QML中更改数组中bool的颜色

问题描述 投票:1回答:1

我期待从数组(calendarListModel)中的布尔值更改Calendar单元格的颜色。

这是一个数组样本:

[
{"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"之类的东西来改变矩形的颜色,但要么将每个单元格变为蓝色或红色,要么根本没有?

问题:根据数组中的true / false值更改颜色的最佳方法是什么?

我正在使用来自CalendarQtQuick.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
               }
            }
        }
    }
}
javascript arrays boolean qml v-play
1个回答
1
投票

好吧,现在你正在使用一组对象并尝试直接索引它,这是不好的。 (特别是因为你正在索引true / false值,它不包含calendarListModel中的单个元素。)即使你可以用日期,名称和状态重建对象,它们仍然是不同的对象。

请改用Array.prototype.find()

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中指定的日期应该有颜色。 Before

之后:请注意,只有1月11日的标记有颜色(蓝色)。实际上,这是因为日期包括在calendarListModel:1544616000000.After

© www.soinside.com 2019 - 2024. All rights reserved.