首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在QML中改变数组内的颜色

在QML中改变数组内的颜色
EN

Stack Overflow用户
提问于 2019-01-02 23:56:15
回答 1查看 843关注 0票数 3

我希望从数组(calendarListModel)中的布尔值更改日历单元格的颜色。

下面是数组的示例:

代码语言:javascript
复制
[
{"date":1544443200000,"name":"Edward","status":1},
{"date":1544529600000,"name":"Katie","status":0},
{"date":1544616000000,"name":"Claire","status":1}
]

我想要做的是根据calendarListModel (**"status": 0 or 1**). 中的布尔值改变日历标记的颜色。

我的记号码是;

代码语言:javascript
复制
Rectangle {
    id: calendarMarker
    visible: arrayFromFireBase.indexOf(styleData.date.getTime()) > -1
    anchors.fill: parent
    color:  "blue"
} 

我试着用calendarListModel.status ? "blue" : "red"等东西改变矩形的颜色,但是要么让每个单元格变成蓝色或红色,要么一点也不做?

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

我使用来自QtQuick.ControlsQtQuick.Controls显示日期,使用CalendarStyleQtQuick.Controls.Styles分配自定义样式。我也在使用V-Play sdk。

下面是我目前所做工作的一个最小、完整和可验证的示例:

代码语言:javascript
复制
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
               }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-08 15:36:43

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

使用Array.prototype.find()代替。

代码语言:javascript
复制
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中。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54014711

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档