qml之轮播效果

前言:
这是一个阳光明媚的上午,温暖的阳光以45度的夹角斜射在我2k高清显示屏上,我深吸了一口经过微波炉加热后弥漫着饭菜味道的空气,望了一眼左手边美女UI的卡姿兰大眼睛,手指不停的点击着鼠标,时而发出叹气的声音,也许是给产品页配图而烦恼吧,我帮不上什么忙,也不知道该怎么温言软语的安慰她,毕竟我也不能用我老年人的审美去给她提什么建设性的意见,好吧,我终于打开了我许久未写的csdn博客页。

该博客用于记录qml(5.6.0版本)实现图片的轮播滚动效果,实现细节不复杂,共两种实现方法,第一种主要用到了Timer定时器和NumberAnimation 动画效果,第二种用到了SwipeViewPageIndicator控件。以下是具体的实现代码,复制粘贴后可以直接运行,当然了图片路径要替换成自己的,废话不多说,直接上干货!

先上效果图:
在这里插入图片描述
完整代码:

方法一:


import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQml 2.2

Window {
    id:id_root
    visible: true
    width: 800
    height: 500
    Rectangle{
        id: id_bk
        color: "black"
        anchors.fill: parent

        Rectangle{
            id: id_bk2
            color: "black"
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.horizontalCenter: parent.horizontalCenter
            width: 600
            clip: true
            ListModel {
                id:id_model
                ListElement {
                    _source:"file:///C:/Users/admin/Pictures/1.png"
                }
                ListElement {
                    _source:"file:///C:/Users/admin/Pictures/2.png"
                }
                ListElement {
                    _source:"file:///C:/Users/admin/Pictures/3.png"
                }
                ListElement {
                    _source:"file:///C:/Users/admin/Pictures/4.png"
                }
            }

            Image{
                id:id_img_last
                width: id_img_cur.width
                anchors.right: id_img_cur.left
                anchors.rightMargin: 0
                anchors.top: id_img_cur.top
                anchors.bottom: id_img_cur.bottom
                fillMode: Image.PreserveAspectFit
            }

            Image{
                id:id_img_cur
                x:(id_bk2.width - id_img_cur.width)/2
                width: 600
                fillMode: Image.PreserveAspectFit
                anchors.verticalCenter: parent.verticalCenter
                source: id_rp.count ? id_rp.model.get(0)._source : undefined
                NumberAnimation {
                    id: anim
                    target: id_img_cur
                    properties: "x"
                    from: id_bk2.width - 30
                    to: (id_bk2.width - id_img_cur.width)/2
                    duration: 2000
                    easing.type: Easing.OutQuint
                    easing.amplitude: 2.0
                    easing.period: 1.5
                    onStopped: {
                        id_img_last.source = id_img_cur.source
                    }
                }
            }

            Row {
                id:id_list
                height: 20
                anchors.top: id_img_cur.bottom
                anchors.topMargin: 5
                anchors.horizontalCenter: id_bk2.horizontalCenter
                spacing: 10

                property var curIndex: id_rp.count ? 0 : -1

                Repeater {
                    id:id_rp
                    model: id_model
                    Rectangle {
                        width: 20
                        height: 20
                        radius: 10
                        color: id_list.curIndex === index ? "gray" : "transparent"
                        border.width: 1
                        border.color: "white"
                        MouseArea{
                            id:id_mouse
                            anchors.fill: parent
                            cursorShape:Qt.PointingHandCursor
                            onClicked: {
                                if(id_list.curIndex !== index)
                                {
                                    if(anim.running)
                                        anim.stop()

                                    id_list.curIndex = index
                                    id_img_cur.source = _source
                                    anim.start()
                                }
                            }
                        }
                    }
                }
            }

            Timer {
                id: id_timer
                interval: 4000
                repeat: true
                onTriggered: {
                    if(!anim.running)
                    {
                        var count = id_rp.count
                        if(count > 1)
                        {
                            id_list.curIndex = id_list.curIndex + 1 >= count ? 0 : id_list.curIndex + 1
                            id_img_cur.source = id_rp.model.get(id_list.curIndex)._source
                            anim.start()
                        }
                    }
                }
            }
            Component.onCompleted: id_timer.start()
        }
    }
}

方法二:

import QtQuick 2.0
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import Qt.labs.controls 1.0
import QtQml 2.2

Window {
    id:id_root
    visible: true
    width: 800
    height: 500
    Rectangle{
        id: id_bk
        color: "white"
        anchors.fill: parent

        Rectangle {
            id:userwindow
            anchors.fill: parent
            //图片地址数组↓
            property var imagelist: ["file:///C:/Users/slf/Pictures/dd/1.png"
                ,"file:///C:/Users/slf/Pictures/dd/2.png"
                ,"file:///C:/Users/slf/Pictures/dd/3.png"
                ,"file:///C:/Users/slf/Pictures/dd/4.png"];
            property var i: 0;


            //图片组件
            Component{
                id:swipeImage;
                Image{
                    asynchronous: true;
                }
            }

            Rectangle{
                id:rect;
                width: parent.width/2;
                height: parent.height/2;
                anchors.top: parent.top;
                anchors.topMargin: 20;
                anchors.horizontalCenter: parent.horizontalCenter;
                clip: true;	//截断多出来的部分
                SwipeView{
                    id:swipeview;
                    anchors.fill: parent;

                    Timer{//3.5秒后切换图片
                        id:imageSwitch;
                        interval: 3500;
                        repeat: true;
                        onTriggered: {
                            swipeview.currentIndex=(swipeview.currentIndex+1)%4;
                            indicator.currentIndex=swipeview.currentIndex;
                        }
                    }
                }

                PageIndicator{
                    id:indicator;
                    count:userwindow.imagelist.length
                    interactive: true;
                    anchors.bottom: rect.bottom;
                    anchors.bottomMargin: -6;
                    anchors.horizontalCenter: rect.horizontalCenter;
                    onCurrentIndexChanged: {
                        swipeview.currentIndex=currentIndex;
                    }
                    delegate: Rectangle{
                        width: 10;height: 10
                        radius: 5
                        color: indicator.currentIndex === index ? "green" : "gray"
                    }
                }
            }

            Component.onCompleted: {
                for(i;i<userwindow.imagelist.length;i++)
                {
                    swipeImage.createObject(swipeview,{"source":userwindow.imagelist[i],"x":swipeview.x,"y":swipeview.y,
                                                "width":swipeview.width,"height":swipeview.height});
                }
                swipeview.currentIndex=0;
                imageSwitch.start();
            }
        }
    }
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: QML(Qt 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组件,并通过动画效果提供更加流畅和美观的用户体验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值