Qml 实现星级评分组件

【写在前面】

在现代应用程序中,星级评分是一个常见的用户界面元素,它允许用户对产品、服务或内容进行评价。

想必大家在用各种带有评分的软件中看到过这个组件:

本文将指导你如何使用 Qml 创建一个简单而美观的星级评分组件,并且支持高度自定义。


【正文开始】

先来看看效果图:

现在开始讲解思路:

首先,我们需要考虑半星的情况,因此可以分为三个部分:

1、红色部分:满填充星星 【fillDelegate】。

2、绿色部分:半填充星星 【halfDelegate】​​​​​​​。

2、蓝色部分:无填充星星 【emptyDelegate】​​​​​​​。

这三部分都是通过代理实现的,因此如果需要自定义,则必须提供这三个组件:

javascript 复制代码
    property Component fillDelegate: Component {
        Text {
            text: fillIcon
            color: root.iconColor
            font.family: fontAwesome.name
            font.pixelSize: iconFontSize
        }
    }
    property Component emptyDelegate: Component {
        Text {
            text: emptyIcon
            color: root.iconColor
            font.family: fontAwesome.name
            font.pixelSize: iconFontSize
        }
    }
    property Component halfDelegate: Component {
        Text {
            text: halfIcon
            color: root.iconColor
            font.family: fontAwesome.name
            font.pixelSize: iconFontSize
        }
    }

接下来,我们需要计算每个部分的数量,其中半星必然只有一颗( 如果有 ):

javascript 复制代码
    property int fillCount: Math.floor(root.value)
    property int emptyStartIndex: Math.round(root.value)
    property bool hasHalf: root.value - fillCount > 0

然后用 Repeater + Loader 载入即可:

javascript 复制代码
    Repeater {
        id: repeater
        model: root.count
        delegate: MouseArea {
            id: rootItem
            width: root.iconSize
            height: root.iconSize
            hoverEnabled: true
            onEntered: hovered = true;
            onExited: hovered = false;
            onClicked: {
                root.isDone = !root.isDone;
                if (root.isDone) {
                    __private.doneValue = root.value;
                    root.done(__private.doneValue);
                }
            }
            onPositionChanged: function(mouse) {
                if (root.allowHalf) {
                    if (mouse.x > (width * 0.5)) {
                        root.value = index + 1;
                    } else {
                        root.value = index + 0.5;
                    }

                } else {
                    root.value = index + 1;
                }
            }
            property bool hovered: false

            Loader {
                active: index < repeater.fillCount
                sourceComponent: fillDelegate
                property bool hovered: rootItem.hovered
            }

            Loader {
                active: repeater.hasHalf && index === (repeater.emptyStartIndex - 1)
                sourceComponent: halfDelegate
                property bool hovered: rootItem.hovered
            }

            Loader {
                active: index >= repeater.emptyStartIndex
                sourceComponent: emptyDelegate
                property bool hovered: rootItem.hovered
            }
        }

        property int fillCount: Math.floor(root.value)
        property int emptyStartIndex: Math.round(root.value)
        property bool hasHalf: root.value - fillCount > 0
    }

至此,核心部分讲解完了,其他部分直接看源码即可。


【结语】

最后:项目链接(多多star呀..⭐_⭐):

Github 地址:https://github.com/mengps/QmlControls/tree/master/Ratehttps://github.com/mengps/QmlControls/tree/master/Rate CSDN 的:
https://download.csdn.net/download/u011283226/89734737https://download.csdn.net/download/u011283226/89734737

相关推荐
恋恋西风13 分钟前
Qt 打开文件列表选择文件,实现拖拽方式打开文件,拖拽加载
开发语言·qt
奔跑吧 android35 分钟前
【Qt】【1. 版本特性介绍】
qt·cpp·qml
西贝爱学习5 小时前
【QT】安装包
开发语言·qt
郝学胜-神的一滴9 小时前
QAxios研发笔记(二):在Qt环境下基于Promise风格简化Http的Post请求
开发语言·c++·笔记·qt·网络协议·程序人生·http
鄃鳕10 小时前
pyside6 qt 事件循环
开发语言·qt
wangjialelele19 小时前
Qt中的常用组件:QWidget篇
开发语言·前端·c++·qt
春蕾夏荷_7282977251 天前
qcustomplot 显示坐标轴
qt·qcustomplot·坐标轴
郝学胜-神的一滴1 天前
Qt删除布局与布局切换技术详解
开发语言·数据库·c++·qt·程序人生·系统架构
yy_xzz1 天前
Debian 系统中 Qt Creator 用 sudo 启动后权限问题
c++·qt
凯子坚持 c1 天前
【星光不负 码向未来 | 万字解析:基于ArkUI声明式UI与分布式数据服务构建生产级跨设备音乐播放器】
分布式·ui