Skip to content

ECalendar 组件

组件简介

ECalendar 是一个功能丰富的日历组件,支持日期选择、事件标记和自定义样式。它提供了直观的用户界面和流畅的交互体验,适合构建需要日期选择或日程管理功能的应用。

基本用法

qml
import "components" as Components

Components.ETheme { id: theme }

Components.ECalendar {
    width: 320
    height: 350
    onDateSelected: (date) => console.log("选择的日期:", date)
}

属性列表

属性名类型默认值描述
currentDatedatenew Date()当前显示的日期
selectedDatedatenew Date()当前选中的日期
showTodaybooltrue是否高亮显示今天的日期
showWeekdaysbooltrue是否显示星期几标题
showMonthNavigationbooltrue是否显示月份导航控件
enableSelectionbooltrue是否允许选择日期
eventsvariant[]日历事件列表
weekdayFormatintQt::ShortDayName星期几显示格式
monthFormatintQt::LongMonthName月份显示格式
primaryColorcolortheme.primaryColor主要颜色(选中日期)
secondaryColorcolortheme.secondaryColor次要颜色(事件标记)
backgroundColorcolortheme.surfaceColor背景颜色
textPrimaryColorcolortheme.textColor主要文本颜色
textSecondaryColorcolortheme.textSecondaryColor次要文本颜色
borderRadiusreal8圆角半径

方法列表

方法名参数返回值描述
selectDate(date)datebool选择指定日期
goToToday()void跳转到今天的日期
nextMonth()void显示下一个月
previousMonth()void显示上一个月
nextYear()void显示下一年
previousYear()void显示上一年
addEvent(event)variantbool添加日历事件
removeEvent(eventId)intbool移除日历事件
getEventsForDate(date)datevariant获取指定日期的事件

信号列表

信号名参数描述
dateSelected(date)date当用户选择日期时触发
monthChanged(month, year)int, int当显示的月份改变时触发
yearChanged(year)int当显示的年份改变时触发
eventClicked(event)variant当用户点击事件时触发

扩展示例

带事件标记的日历

qml
Components.ECalendar {
    width: 320
    height: 350
    
    // 初始化事件
    Component.onCompleted: {
        addEvent({ id: 1, date: new Date(2025, 11, 25), title: "圣诞节", color: theme.primaryColor })
        addEvent({ id: 2, date: new Date(2025, 11, 31), title: "新年夜", color: theme.secondaryColor })
        addEvent({ id: 3, date: new Date(2026, 0, 1), title: "新年", color: theme.primaryColor })
    }
    
    onDateSelected: (date) => console.log("选择的日期:", date)
    onEventClicked: (event) => console.log("点击的事件:", event.title)
}

自定义样式的日历

qml
Components.ECalendar {
    width: 320
    height: 350
    primaryColor: "#ff4081"
    secondaryColor: "#03a9f4"
    backgroundColor: "#f5f5f5"
    textPrimaryColor: "#333333"
    textSecondaryColor: "#666666"
    borderRadius: 12
    
    onDateSelected: (date) => console.log("选择的日期:", date)
}

日期选择器模式

qml
Components.EAnimatedWindow {
    width: 350
    height: 400
    title: "日期选择器"
    visible: true
    
    Column {
        anchors.fill: parent
        padding: 20
        spacing: 16
        
        Components.ECalendar {
            width: 310
            height: 320
            anchors.horizontalCenter: parent.horizontalCenter
            onDateSelected: (date) => selectedDateText.text = "选择的日期: " + date.toLocaleDateString()
        }
        
        Text {
            id: selectedDateText
            anchors.horizontalCenter: parent.horizontalCenter
            text: "选择的日期: " + new Date().toLocaleDateString()
            color: theme.textColor
        }
        
        Components.EButton {
            text: "确认"
            anchors.horizontalCenter: parent.horizontalCenter
            onClicked: parent.parent.hide()
        }
    }
}

最佳实践

  1. 事件管理:合理组织和管理日历事件,避免过多事件导致界面拥挤
  2. 性能优化:对于大量事件,考虑使用分页或懒加载策略
  3. 日期格式:根据用户地区设置合适的日期和星期格式
  4. 交互反馈:为日期选择和事件点击提供清晰的视觉反馈
  5. 可访问性:确保日历组件支持键盘导航和屏幕阅读器

注意事项

  • 日历组件依赖 Qt 6.5 及以上版本的日期时间 API
  • 事件数据格式应为包含 id、date、title 和 color 属性的对象数组
  • 大量事件可能会影响组件性能,建议限制同时显示的事件数量
  • 自定义样式时,注意保持日期选择和事件标记的可见性
  • 组件支持的日期范围为 1900 年到 2100 年