QML:ListView按下选中当前项和高亮的一例

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
        snapMode: ListView.SnapOneItem
        model:5                //没有特殊数据时使用数字做模型
        delegate:DelegateItem{}
        highlight:HighlightDelegateItem{}
        focus: true
    }
}

DelegateItem.qml

import QtQuick 2.0
//import Qt5Compat.GraphicalEffects
import QtGraphicalEffects 1.14

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

    Rectangle
    {
        id:cir
        color: "#ffffff"
        width: 36
        height: 36
        radius: 18
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 6
    }

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            view.currentIndex = index  //item切换
        }
    }
}

HighlightDelegateItem.qml

import QtQuick 2.0

Rectangle
{
    id:root
    width: 460    //设置的宽高无效,会被裁剪成项目(委托组件)的大小
    height: 340
    radius: 25
    y: view.currentItem.y
    color: "#ff0000"
    z:view.z + 2;    //高亮委托的组件在最上面
}

DelegateItem.qml加上一个状态:

import QtQuick 2.0
//import Qt5Compat.GraphicalEffects
import QtGraphicalEffects 1.14

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

    Rectangle
    {
        id:cir
        color: "#ffffff"
        width: 36
        height: 36
        radius: 18
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 6
    }

    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            if(root.state == "")
                root.state = "showDetails"
            else
                root.state = ""

            view.currentIndex = index  //item切换
        }
    }

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

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值