QML实现网页左右滑动的轮播图效果

本文介绍如何使用QML实现图片轮播效果,并通过SwipeView组件配合PageIndicator或TabBar进行页面指示与切换。提供了具体代码实例,展示左右滑动动画及不同样式指示器的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网页中有很多的左右滑动的图片轮动的效果。QML实现此效果的两种方式。
PageIndicator和TabBar 也对应两种样式。
其中左右滑动的动画效果是利用SwipeView的默认切换动画效果

import QtQuick 2.9
import QtQuick.Controls 2.2

ApplicationWindow {
    visible= true
    width= 640
    height= 480
    title= qsTr("图片切换")


    SwipeView {
        id= swipeView
        anchors.fill= parent
        currentIndex= indicator.currentIndex

        Rectangle{
            Image {
                //id= name
                source= "./img/0.jpg"
                anchors.fill= parent
            }
        }
        Rectangle{
            Image {
                //id= name
                source= "./img/1.jpg"
                anchors.fill= parent
            }
        }
        Rectangle{
            Image {
                //id= name
                source= "./img/2.jpg"
                anchors.fill= parent
            }
        }
        Rectangle{
            Image {
                //id= name
                source= "./img/3.jpg"
                anchors.fill= parent
            }
        }



    }

    //方法1
    PageIndicator {
          id= indicator
          count= swipeView.count
          currentIndex= swipeView.currentIndex
          interactive= true //可以点击
          anchors.bottom= swipeView.bottom
          anchors.horizontalCenter= parent.horizontalCenter
      }


    //方法2
//    TabBar {
//        id= indicator
//        currentIndex= swipeView.currentIndex
//        anchors.bottom= parent.bottom
//        anchors.right= parent.right
//        width= 400
//        opacity = 0.5


//        TabButton {
//            text= qsTr("Page 1")
//        }
//        TabButton {
//            text= qsTr("Page 2")
//        }
//        TabButton {
//            text= qsTr("Page3")
//        }
//        TabButton {
//            text= qsTr("Page 4")
//        }
//    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值