QML 导航栏


在这里插入图片描述


前言

最近在学css,看到css做的东西还挺好看的,于是想着能不能用qml复刻,今天算是这个demo系列的第一个,后面会持续更新的。
有想要的我做的效果也可以评论区评论,给我找找灵感。感谢大家。

一、效果

在这里插入图片描述

二、代码

import QtQuick
import QtQuick.Controls

Rectangle{
    anchors.centerIn: parent

    property var menus: [
        {title: "Home", url: "qrc:/img/CaptureFrame.svg"},
        {title: "Mail", url: "qrc:/img/CaptureFrame.svg"},
        {title: "Setting", url: "qrc:/img/CaptureFrame.svg"},
        {title: "Theme", url: "qrc:/img/CaptureFrame.svg"},
        {title: "Person", url: "qrc:/img/CaptureFrame.svg"}]

    property int cellWidthMin: 50
    property int cellWidthMax: 100
    property int cellHeight: 40
    property int padding: 20
    property int currIndex: 0

    width: (menus.length - 1) * cellWidthMin + cellWidthMax + padding
    height: 50
    border.color: "pink"
    radius: 20
    color: Qt.rgba(192/255, 192/255, 192/255, 1)

    Row{
        width: childrenRect.width
        height: parent.height
        anchors.centerIn: parent
        Repeater{
            model: menus
            delegate: Rectangle{
                id: menuItem
                width: currIndex === index ? cellWidthMax: cellWidthMin
                height: cellHeight
                radius: 20
                color: Qt.rgba(64/255, 128/255, 128/255, 1)
                anchors.verticalCenter: parent.verticalCenter

                Image {
                    source: modelData.url
                    sourceSize: Qt.size(cellHeight / 3 * 2, cellHeight / 3 * 2)
                    anchors.left: parent.left
                    anchors.leftMargin: 12
                    anchors.verticalCenter: parent.verticalCenter
                    Text {
                        text: modelData.title
                        visible: currIndex === index
                        anchors.left: parent.right
                        anchors.rightMargin: 10
                        anchors.verticalCenter: parent.verticalCenter
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                        font.pixelSize: 18
                    }
                }

                Behavior on width{
                    NumberAnimation {
                        target: menuItem
                        property: "width"
                        duration: 200
                        easing.type: Easing.InOutQuad
                    }
                }
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        currIndex = index
                    }
                }
            }
        }
    }
}

总结

需要了解就是Behavior的用法,可以在属性改变的时候调用动画。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值