QML自定义日历Delegate重复渲染问题:不修改Rectangle尺寸的解决方法
解决QML自定义日历显示下月日期的问题
方法1:禁用MonthGrid的前后月日期显示
MonthGrid默认通过showLeadingDays和showTrailingDays属性显示上月末、下月初的日期来填充完整网格,直接将这两个属性设为false即可隐藏这些跨月日期:
delegate: MonthGrid { id: _monthGrid showLeadingDays: false showTrailingDays: false delegate: Label { required property var model width: 34 height: 34 horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter font.pixelSize: 14 font.bold: true opacity: model.month === _monthGrid.month ? 1 : 0.5 color: "#FFFFFF" text: model.day } }
方法2:仅渲染当前月的日期标签
如果需要保留网格完整布局(避免出现空缺)但不显示跨月日期文本,可以在Label中通过visible属性控制,只让当前月的日期可见:
delegate: Label { required property var model width: 34 height: 34 horizontalAlignment: Text.AlignHCenter verticalAlignment: Text.AlignVCenter font.pixelSize: 14 font.bold: true visible: model.month === _monthGrid.month color: "#FFFFFF" text: model.day }
方法3:添加可视区域裁剪
若要彻底限制内容渲染范围,避免Delegate超出父容器显示,可以给ListView或MonthGrid添加clip: true属性,超出范围的内容会被自动裁剪:
ListView { id: _calendarView width: 236 height: 202 clip: true snapMode: ListView.SnapOneItem orientation: ListView.Horizontal highlightRangeMode: ListView.StrictlyEnforceRange spacing: 1 // ... 其余代码 }
内容的提问来源于stack exchange,提问作者xzxolop
相关产品推荐
相关产品推荐

