• 1944阅读
  • 0回复

[讨论]Calendar控件的styleData.today属性一直为false [复制链接]

上一主题 下一主题
离线yuyu414
 

只看楼主 倒序阅读 楼主  发表于: 2019-10-18
想在日历上的今天标注一张图片,查到可以用styleData.today这个属性,但是日历控件的styleData.today属性一直为false
文档是这么说的:

readonly property bool styleData.today
true if this date is equal to today's date.
  1. import QtQuick 2.2
  2. import QtQuick.Controls 1.4
  3. import QtQuick.Controls.Private 1.0
  4. import QtQuick.Controls.Styles 1.4
  5. import org.qtproject.examples.calendar 1.0
  6. ApplicationWindow {
  7.     visible: true
  8.     width: 640
  9.     height: 400
  10.     minimumWidth: 400
  11.     minimumHeight: 300
  12.     color: "#f4f4f4"
  13.     title: "Calendar Example"
  14.     SystemPalette {
  15.         id: systemPalette
  16.     }
  17.     SqlEventModel {
  18.         id: eventModel
  19.     }
  20.     Flow {
  21.         id: row
  22.         anchors.fill: parent
  23.         anchors.margins: 20
  24.         spacing: 10
  25.         layoutDirection: Qt.RightToLeft
  26.         Calendar {
  27.             id: calendar
  28.             width: (parent.width > parent.height ? parent.width * 0.6 - parent.spacing : parent.width)
  29.             height: (parent.height > parent.width ? parent.height * 0.6 - parent.spacing : parent.height)
  30.             frameVisible: true
  31.             weekNumbersVisible: true
  32.             selectedDate: new Date()
  33.             focus: true
  34.             onClicked: {
  35.             }
  36.             style: CalendarStyle {
  37.                 dayDelegate: Item {
  38.                     readonly property color sameMonthDateTextColor: "#444"
  39.                     readonly property color selectedDateColor: Qt.platform.os === "osx" ? "#3778d0" : systemPalette.highlight
  40.                     readonly property color selectedDateTextColor: "white"
  41.                     readonly property color differentMonthDateTextColor: "#bbb"
  42.                     readonly property color invalidDatecolor: "#dddddd"
  43.                     Rectangle {
  44.                         anchors.fill: parent
  45.                         border.color: "transparent"
  46.                         color: styleData.date !== undefined && styleData.selected ? selectedDateColor : "transparent"
  47.                         anchors.margins: styleData.selected ? -1 : 0
  48.                     }
  49.                     Image {
  50.                         visible: false
  51.                         anchors.top: parent.top
  52.                         anchors.left: parent.left
  53.                         anchors.margins: 0
  54.                         width: 12
  55.                         height: width
  56.                         source: "qrc:/images/eventindicator.png"
  57.                     }
  58.                     Image {
  59.                         visible: true
  60.                         anchors.fill: parent
  61.                         source: styleData.today ? "qrc:/images/select.png" : ""
  62.                     }
  63.                     Label {
  64.                         id: dayDelegateText
  65.                         text: styleData.today
  66.                         anchors.centerIn: parent
  67.                         color: {
  68.                             var color = invalidDatecolor;
  69.                             if (styleData.valid) {
  70.                                 // Date is within the valid range.
  71.                                 color = styleData.visibleMonth ? sameMonthDateTextColor : differentMonthDateTextColor;
  72.                                 if (styleData.selected) {
  73.                                     color = selectedDateTextColor;
  74.                                 }
  75.                             }
  76.                             color;
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.         Component {
  83.             id: eventListHeader
  84.             Row {
  85.                 id: eventDateRow
  86.                 width: parent.width
  87.                 height: eventDayLabel.height
  88.                 spacing: 10
  89.                 Label {
  90.                     id: eventDayLabel
  91.                     text: calendar.selectedDate.getDate()
  92.                     font.pointSize: 35
  93.                 }
  94.                 Column {
  95.                     height: eventDayLabel.height
  96.                     Label {
  97.                         readonly property var options: { weekday: "long" }
  98.                         text: Qt.locale().standaloneDayName(calendar.selectedDate.getDay(), Locale.LongFormat)
  99.                         font.pointSize: 18
  100.                     }
  101.                     Label {
  102.                         text: Qt.locale().standaloneMonthName(calendar.selectedDate.getMonth())
  103.                               + calendar.selectedDate.toLocaleDateString(Qt.locale(), " yyyy")
  104.                         font.pointSize: 12
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.         Rectangle {
  110.             width: (parent.width > parent.height ? parent.width * 0.4 - parent.spacing : parent.width)
  111.             height: (parent.height > parent.width ? parent.height * 0.4 - parent.spacing : parent.height)
  112.             border.color: Qt.darker(color, 1.2)
  113.             ListView {
  114.                 id: eventsListView
  115.                 spacing: 4
  116.                 clip: true
  117.                 header: eventListHeader
  118.                 anchors.fill: parent
  119.                 anchors.margins: 10
  120.                 model: eventModel.eventsForDate(calendar.selectedDate)
  121.                 delegate: Rectangle {
  122.                     width: eventsListView.width
  123.                     height: eventItemColumn.height
  124.                     anchors.horizontalCenter: parent.horizontalCenter
  125.                     Image {
  126.                         anchors.top: parent.top
  127.                         anchors.topMargin: 4
  128.                         width: 12
  129.                         height: width
  130.                         source: "qrc:/images/eventindicator.png"
  131.                     }
  132.                     Rectangle {
  133.                         width: parent.width
  134.                         height: 1
  135.                         color: "#eee"
  136.                     }
  137.                     Column {
  138.                         id: eventItemColumn
  139.                         anchors.left: parent.left
  140.                         anchors.leftMargin: 20
  141.                         anchors.right: parent.right
  142.                         height: timeLabel.height + nameLabel.height + 8
  143.                         Label {
  144.                             id: nameLabel
  145.                             width: parent.width
  146.                             wrapMode: Text.Wrap
  147.                             text: modelData.name
  148.                         }
  149.                         Label {
  150.                             id: timeLabel
  151.                             width: parent.width
  152.                             wrapMode: Text.Wrap
  153.                             text: modelData.startDate.toLocaleTimeString(calendar.locale, Locale.ShortFormat)
  154.                             color: "#aaa"
  155.                         }
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.     }
  161. }

有用过的吗,先谢谢各位。
快速回复
限100 字节
 
上一个 下一个