QML之轮播效果

QML之轮播效果(两种方法实现)

  • 想要实现的效果,我们本章要实现的是酷狗歌单界面中轮播效果,首先展示酷狗特效:
    在这里插入图片描述

方法一:

首先展示我们的效果:
在这里插入图片描述
上代码:

import QtQuick.Window 2.2
import QtQuick.Controls 2.15
import QtQuick 2.15

ApplicationWindow {
    id: window
    width:800
    height: 400
    visible: true
    Rectangle{
        id:view
        width:600
        height:300
        radius:15
        property string lastColor: "red"
        property int lastIndex: 0
        anchors.centerIn: parent
        clip:true
        property int  count: 0
        ListModel{
            id:myLunBoListModel
            ListElement{
                mycolor:"red"
            }
            ListElement{
                mycolor:"green"
            }
            ListElement{
                mycolor:"black"
            }
            ListElement{
                mycolor:"blue"
            }
            ListElement{
                mycolor:"grey"
            }
            ListElement{
                mycolor:"yellow"
            }
        }
        //左边隐藏的矩形向右移动到中间位置,用来模拟点击indicator控件按钮向右移动的动画
        Rectangle{
            id:leftRect
            height: 300
            width: 600
            radius:15
            x:-600
            z:1
            color:myLunBoListModel.get(indicator.currentIndex).mycolor
        }
        PropertyAnimation{
            id: myRightMovingAnimation
            target: leftRect
            duration: 50
            property: "x"
            to: 0
            onFinished: {
                myShowRect.color=myLunBoListModel.get(indicator.currentIndex).mycolor
                leftRect.x=-600
            }
        }

        //控件可视范围内需要显示的矩形
        Rectangle{
            id:myShowRect
            height: 300
            width: 600
            radius:15
            x:0
            z:0
            color:view.lastColor
        }

        //右边隐藏的矩形向左移动到中间位置,用来模拟点击indicator控件按钮向左移动的动画
        Rectangle{
            id:rightRect
            height: 300
            width: 600
            radius:15
            x:600
            z:1
            color:myLunBoListModel.get(indicator.currentIndex).mycolor
        }
        PropertyAnimation{
            id: myLeftMovingAnimation
            target: rightRect
            duration: 50
            property: "x"
            to: 0
            onFinished: {
                myShowRect.color=myLunBoListModel.get(indicator.currentIndex).mycolor
                rightRect.x=600
            }
        }


        //用于监测鼠标进入轮播界面,显示左右移动按钮
        MouseArea{
            z:1
            anchors.fill: parent
            hoverEnabled: true
            onEntered: {
                leftImage.source="qrc:/images/turnLeft0.png";
                rightImage.source="qrc:/images/turnRight0.png";
            }
            onExited: {
                leftImage.source="";
                rightImage.source="";
            }
        }
        //左边按钮
        MouseArea{
            id:leftBtn
            width:20
            height:50
            z:1.5
            hoverEnabled: true
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            onClicked: {
                var idx=indicator.currentIndex-1
                if(idx<0)
                    indicator.currentIndex=myLunBoListModel.count-1
                else
                    indicator.currentIndex=idx
                myLeftMovingAnimation.running=true
                view.lastColor=myLunBoListModel.get(indicator.currentIndex).mycolor
            }
            Image{
                id:leftImage
                anchors.fill: parent
            }
            //加入进入和退出效果
            onEntered: {
                leftImage.source="qrc:/images/turnLeft1.png"
                rightImage.source="qrc:/images/turnRight0.png";
            }
            onExited: {
                leftImage.source=""
                rightImage.source="";
            }
        }
        //右边按钮
        MouseArea{
            id:rightBtn
            width:20
            height:50
            z:1.5
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            hoverEnabled: true
            onClicked: {
                var idx=indicator.currentIndex+1
                if(idx>=myLunBoListModel.count)
                    indicator.currentIndex=0
                else
                    indicator.currentIndex=idx
                myRightMovingAnimation.running=true
                view.lastColor=myLunBoListModel.get(indicator.currentIndex).mycolor
                console.log("当前myCurrentIndex:",indicator.currentIndex,"color: ", view.lastColor)
            }
            //加入进入和退出效果
            onEntered: {
                console.log("进入右键")
                leftImage.source="qrc:/images/turnLeft0.png"
                rightImage.source="qrc:/images/turnRight1.png";
            }
            onExited: {
                console.log("退出右键")
                leftImage.source=""
                rightImage.source="";
            }
            Image{
                id:rightImage
                anchors.fill: parent
            }
        }
    }

    PageIndicator {
        id: indicator
        count: myLunBoListModel.count
        interactive: false
        spacing:15
        width:200
        height:20
        hoverEnabled: true
        //加入鼠标悬浮,显示左右移动按钮效果
        onHoveredChanged: {
            leftImage.source=hovered?"qrc:/images/turnLeft0.png":""
            rightImage.source=hovered?"qrc:/images/turnRight0.png":""
        }
        anchors.bottom: view.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        delegate: Rectangle{
            width:index==indicator.currentIndex?20:10
            height:10
            radius:5
            color:"white"
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onClicked: {
                    console.log("lastIndex:",view.lastIndex,"index:",index)
                    indicator.currentIndex=index
                    if(view.lastIndex<index){
                        myRightMovingAnimation.running=true}
                    else if(view.lastIndex>index){
                        myLeftMovingAnimation.running=true}
                    view.lastIndex=index
                }
            }
        }
    }
}

方法二:

效果展示:
在这里插入图片描述
瑕疵是当currentIndex==mySwipeView.count-1时就会出现,如下所示:
在这里插入图片描述

import QtQuick.Window 2.2
import QtQuick.Controls 2.15
import QtQuick 2.15

ApplicationWindow {
    id: window
    width:800
    height: 400
    visible: true
    SwipeView {
        id:mySwipeView
        anchors.fill: parent
        currentIndex: myIndicator.currentIndex
        clip:true
        Rectangle{
            color:"red"
            height:190
            width:500
            radius:15
            antialiasing :true
        }
        Rectangle{
            color:"green"
            radius:15
            height:190
            width:500
            antialiasing :true
        }
        Rectangle{
            color:"blue"
            radius:15
            height:190
            width:500
            antialiasing :true
        }
        Rectangle{
            color:"black"
            height:190
            width:500
            radius: 15
        }
        Rectangle{
            color:"pink"
            radius:15
            height:190
            width:500
            antialiasing :true
        }
    }
    Button{
        width:20
        height:100
        anchors.right: parent.right
        anchors.verticalCenter: parent.verticalCenter
        onClicked: {
            var idx=mySwipeView.currentIndex+1
            if(idx<mySwipeView.count)  //其中SwipeView的count值=5
                mySwipeView.currentIndex=idx
            else
                mySwipeView.currentIndex=0
        }
    }
    Button{
        width:20
        height:100
        anchors.left: parent.left
        anchors.verticalCenter: parent.verticalCenter
        onClicked: {
            var idx=mySwipeView.currentIndex-1
            if(idx<0)
                mySwipeView.currentIndex=mySwipeView.count-1
            else
                mySwipeView.currentIndex=idx
        }
    }
    PageIndicator{
        id:myIndicator
        count: mySwipeView.count
        currentIndex: mySwipeView.currentIndex
        interactive: false
        anchors.bottom: mySwipeView.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        spacing:12
        delegate: Rectangle{
            width:myIndicator.currentIndex===index?15:6
            height:6
            radius:3
            color:Qt.rgba(1,1,1,0.7)
            MouseArea{
                anchors.fill: parent
                onClicked:{
                    myIndicator.currentIndex=index
                    mySwipeView.currentIndex=index
                }
            }
        }
    }
}

方法三:

使用PathView亦可实现

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: QMLQt Quick)是一种基于Qt框架的声明性语言,用于构建图形用户界面。在QML中,我们可以使用Popup组件来实现灯箱效果。 首先,我们需要在QML代码中导入Qt Quick的Popup组件,并创建一个Popup对象。然后,我们可以设置Popup的属性,例如位置、大小和动画效果等。接下来,我们可以在Popup中添加需要显示的内容,例如图片、文本或其他控件。 为了实现灯箱效果,我们可以设置Popup的背景为半透明的黑色,并将其位置设置为覆盖整个屏幕。这样就可以实现以弹出的形式显示内容,并将背景进行模糊处理,达到灯箱效果。 在代码中,我们可以通过设置Popup的背景属性为矩形类型,然后通过设置矩形的颜色为半透明的黑色来实现背景的效果。同时,可以设置Popup的modal属性为true,使得Popup在显示时阻止用户与其他界面进行交互。 最后,我们可以通过设置Popup的visible属性来控制灯箱的显示和隐藏。当需要显示灯箱时,将visible属性设置为true,当需要隐藏灯箱时,将visible属性设置为false。 通过上述步骤,我们可以使用QML中的Popup组件来实现灯箱效果。这种效果可以用于展示需要突出显示的内容,提高用户体验和视觉效果。 ### 回答2: QML Popup灯箱效果是通过使用QML中的Popup组件和不透明的遮罩来实现的。 首先,在QML中创建Popup组件并设置其属性,如弹出的位置,大小,内容等。可以通过设置background属性来改变Popup的背景颜色、透明度或者使用图片。 然后,在Popup上添加一个Rectangle组件,作为灯箱效果的遮罩层。设置该矩形的颜色和透明度,以达到遮罩效果。可以将其颜色设置为黑色或其他暗色,并将其透明度设置为较低的值,以增加其遮罩效果。 接下来,在Popup中添加其他组件或内容,以实现灯箱效果。可以在Popup中添加一些文本、图像或其他UI元素,以实现更复杂的灯箱效果。 最后,通过设置Popup组件的打开和关闭信号,以实现对弹出和关闭操作的控制。可以根据需求在代码中通过控制Popup的visible属性来控制灯箱效果的显示和隐藏。 总之,通过使用QML中的Popup组件和透明的遮罩,可以实现灯箱效果。可以根据具体的需求,调整Popup的属性和添加其他组件,来达到想要的视觉效果。 ### 回答3: QML中的灯箱效果是通过Popup元素实现的。Popup是一种轻量级的弹出窗口组件,可以用于在屏幕上模态或非模态地显示其他QML组件。 要实现灯箱效果,我们可以按照以下步骤进行操作: 1. 创建一个新的QML文件,例如PopupDialog.qml,并在该文件中定义灯箱的外观和布局。 2. 在PopupDialog.qml文件中,使用Rectangle或其他合适的组件作为背景,设置其颜色为半透明的黑色,以实现遮罩背景的效果。 3. 在背景组件的上方,添加另一个组件作为内容容器,用于放置实际显示的内容,如文本、图像或其他自定义组件。 4. 添加必要的属性和信号,以便在外部使用PopupDialog时能够控制其显示和隐藏。 5. 在主QML文件中,引入PopupDialog组件,并根据需要设置其属性和信号。 6. 当需要显示灯箱时,通过设置PopupDialog的visible属性为true来显示它。可以使用动画效果来提供更流畅的过渡效果。 7. 当需要隐藏灯箱时,通过设置PopupDialog的visible属性为false来隐藏它。同样,可以使用动画效果使灯箱在淡出之前渐渐消失。 总结起来,实现一个QML的灯箱效果可以通过创建一个PopupDialog组件,使用遮罩背景和内容容器,通过控制其visible属性来显示和隐藏。这样可以在屏幕上以模态或非模态的方式显示其他QML组件,并通过动画效果提供更加流畅和美观的用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值