QML编写自定义控件:手风琴图片滑动

代码:

DelegateItem.qml

import QtQuick 2.0
import Qt5Compat.GraphicalEffects

Rectangle
{
    id:root
    width: 50
    height: 320
    radius: 25
    color: ListView.isCurrentItem?"#157efb":"#53d769" //选中颜色设置
    border.color: Qt.lighter(color, 1.1)

    Image
    {
        id:_image
        anchors.fill: parent
        fillMode:Image.PreserveAspectCrop
        source: imgPath
        sourceSize: Qt.size(parent.size, parent.size)
        smooth: true
        antialiasing: true
        visible: false
    }

    Rectangle
    {
        id: _mask
        color: "black"
        anchors.fill: parent
        radius: root.radius
        visible: false
        antialiasing: true
        smooth: true
    }
    OpacityMask
    {
        id: mask_image
        anchors.fill: _image
        source: _image
        maskSource: _mask
        visible: true
        antialiasing: true
    }

    Rectangle
    {
        id:cir
        color: "#ffffff"
        width: 36
        height: 36
        radius: 18
        anchors.bottom: parent.bottom
        anchors.left: parent.left
        anchors.leftMargin: 7
        anchors.bottomMargin: 6
        MouseArea
        {
            anchors.fill: cir
            cursorShape: Qt.PointingHandCursor
            onClicked:
            {
                view.currentIndex = index  //item切换

                if(root.state == "")
                {
                    for(var i = 0;i < view.count;++i)
                    {
                        var item = view.itemAtIndex(i)
                        if(item !== index)
                        {
                            item.state = ""
                        }
                    }
                    root.state = "showDetails"
                }
                else
                {
                    root.state = ""
                }
            }
        }
    }

    Column
    {
        anchors.left: cir.right
        anchors.top: cir.top
        anchors.leftMargin: 10

        Text
        {
            text: title
            color: "#ffffff"
            font.pixelSize: 16
            font.bold: true
        }
        Text
        {
            text: detailed
            color: "#ffffff"
            font.pixelSize: 14
        }
    }

    states:
    [
        State
        {
            name: "showDetails"
            PropertyChanges { target: root;width: 460;}
        }
    ]
    transitions: //设置状态转变的动画
    [
        Transition
        {
            NumberAnimation//其值为数值类型的动画
            {
                duration: 300;
                properties: "height,width"
            }
        }
    ]
}

main.qml

import QtQuick 2.12
import QtQuick.Window 2.0

Window
{
    id:root;
    visible: true;
    width: 800; height: 400

    ListView
    {
        id:view
        anchors.fill: parent
        orientation: Qt.Horizontal
        spacing: 10
        model:ListModel
        {
            ListElement{imgPath: "qrc:/img/1.jpg";title:"标题1";detailed:"黄河之水天上来";}
            ListElement{imgPath: "qrc:/img/2.jpg";title:"标题2";detailed:"奔流到海不复回";}
            ListElement{imgPath: "qrc:/img/3.jpg";title:"标题3";detailed:"明镜高堂悲白发";}
            ListElement{imgPath: "qrc:/img/4.jpg";title:"标题4";detailed:"朝如青丝暮成雪";}
            ListElement{imgPath: "qrc:/img/5.jpg";title:"标题5";detailed:"人生得意须尽欢";}
        }
        delegate:DelegateItem{}
        focus: true
    }
}

效果:

UI参考:jQuery圆形修边图片手风琴代码 

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值