• QML自定义的日历控件


            QML中提供了日历的控件Calendar,但该控件为QtQuick.Controls 1中提供的控件,因此只能使用QtQuick.Controls.Styles的方式对该控件进行设置,效果如图:

            在Qt文档中关于日历Calendar的描述为:Calendar允许从天数网格中选择日期,类似于QCalendarWidget。日历上的日期可以用鼠标选择,也可以用键盘导航。CalendarStyle 可用于设置日历组件的样式,其样式组件区域为:

        property color backgroundColor: 设置日历整体背景颜色,可设置半透明效果
        property color backgroundBorderColor: 背景边框颜色
        property color navigationBarBackgroundColor: 导航栏背景颜色
        property color navigationBarTextColor: 导航栏字体颜色
        property color dayOfWeekBackgroundColor: 星期显示栏背景颜色
        property color dayOfWeekTextColor:星期显示栏字体颜色
        property color daySelectedBackgroundColor: 选中的日期的背景颜色
        property color dayInvalidBackgroundColor:  非本月日期的背景颜色
        property color dayTextColor: 本月日期字体颜色
        property color gridLineColor: 日期框颜色

             大家在使用过程中可结合我QML中提供的属性和文档进行自定义日历样式和添加修改属性,QML源码:

    1. import QtQml 2.7
    2. import QtQuick 2.7
    3. import QtQuick.Controls 1.6
    4. import QtQuick.Controls.Styles 1.4
    5. Calendar {
    6. id:calendar
    7. width: 260
    8. height: 260
    9. frameVisible:false
    10. property color backgroundColor: "#d90b394e"
    11. property color backgroundBorderColor: "#e6115777"
    12. property color navigationBarBackgroundColor: "#99b3b3b3"
    13. property color navigationBarTextColor: "#38d3dc"
    14. property color dayOfWeekBackgroundColor: "#4d2d2f39"
    15. property color dayOfWeekTextColor: "#38d3dc"
    16. property color daySelectedBackgroundColor: "#6638d3dc"
    17. property color dayInvalidBackgroundColor: "#4d5c5952"
    18. property color dayTextColor: "#38d3dc"
    19. property color gridLineColor: "#555"
    20. signal dateChanged(var dateStr)
    21. onClicked: {
    22. var s = datamanager.dateToString(date)
    23. dateChanged(s)
    24. }
    25. style: CalendarStyle {
    26. gridVisible: false
    27. background: Rectangle{
    28. color: calendar.backgroundColor
    29. border.color: calendar.backgroundBorderColor
    30. }
    31. navigationBar:Rectangle{
    32. height: 30
    33. color: calendar.navigationBarColor
    34. Label {
    35. text: styleData.title
    36. anchors.centerIn: parent
    37. color: calendar.titleColor
    38. font.pixelSize: 13
    39. font.family: "微软雅黑"
    40. font.bold: true
    41. }
    42. Label {
    43. anchors.left: parent.left
    44. width: 30
    45. height: 30
    46. text:"<"
    47. font.pixelSize: 16
    48. horizontalAlignment: Text.AlignHCenter
    49. verticalAlignment: Text.AlignVCenter
    50. font.family: "微软雅黑"
    51. font.bold: true
    52. color: "#cecece"
    53. MouseArea{
    54. anchors.fill: parent
    55. onClicked: calendar.showPreviousMonth()
    56. }
    57. }
    58. Label {
    59. anchors.right: parent.right
    60. width: 30
    61. height: 30
    62. text:">"
    63. font.pixelSize: 16
    64. horizontalAlignment: Text.AlignHCenter
    65. verticalAlignment: Text.AlignVCenter
    66. font.family: "微软雅黑"
    67. font.bold: true
    68. color: "#cecece"
    69. MouseArea{
    70. anchors.fill: parent
    71. onClicked: calendar.showNextMonth()
    72. }
    73. }
    74. }
    75. dayOfWeekDelegate:Rectangle{
    76. color: calendar.dayOfWeekBackgroundColor
    77. height: 26
    78. Label {
    79. text: Qt.locale().dayName(styleData.dayOfWeek)
    80. anchors.centerIn: parent
    81. color: calendar.dayOfWeekTextColor
    82. font.pixelSize: 9
    83. }
    84. }
    85. dayDelegate: Rectangle {
    86. color: styleData.selected ? calendar.daySelectedBackgroundColor :(styleData.visibleMonth && styleData.valid ? "#00000000" : calendar.dayInvalidBackgroundColor)
    87. border.width: 0
    88. Label {
    89. text: styleData.date.getDate()
    90. anchors.centerIn: parent
    91. color: styleData.visibleMonth && styleData.valid ? calendar.dayTextColor: "#888"
    92. }
    93. Rectangle {
    94. width: parent.width
    95. height: 1
    96. color: calendar.gridLineColor
    97. anchors.bottom: parent.bottom
    98. }
    99. Rectangle {
    100. width: 1
    101. height: parent.height
    102. color: calendar.gridLineColor
    103. anchors.right: parent.right
    104. }
    105. }
    106. }
    107. }

  • 相关阅读:
    Java策略模式在我司应用
    Java计算两个日期之间的工作时长【包含节假日、补班、周末】
    Nature、science、cell旗下刊物
    Java添加条形码到PDF表格
    java“俄罗斯方块”
    HCIA 交换机原理与ARP协议
    简述 HTTP 请求的过程是什么?
    java面试官:程序员,请你告诉我是谁把公司面试题泄露给你的?
    10款超牛Vim插件,爱不释手了
    1818_ChibiOS的计数信号量
  • 原文地址:https://blog.csdn.net/zjgo007/article/details/126622615