QML用PathView实现轮播图


Posted in Python onJune 03, 2020

轮播图是一个常见的功能,在QML中,可以使用PathView来实现一个循环播放的轮播图组件。

默认情况,如果限制了加载个数,切换时第一帧会马上消失,第二帧才进入,这样会有断档的感觉。通过设置PathView中preferredHighlightBegin/End为0.5,这样当前选定的项位于路径的中间,就没有断档的感觉了。效果如下(为了测试,我没有clip,clip之后只有范围内的才可见):

QML用PathView实现轮播图

//CircleView.qml

import QtQuick 2.12
import QtQuick.Controls 2.12
 
//轮播图
Item {
 id: control
 
 property int indicatorWidth: 12
 
 //定时切换间隔
 property alias timerInterval: path_timer.interval
 //切换动画执行时间
 property alias pathDuration: path_view.highlightMoveDuration
 property alias delegate: path_view.delegate
 property alias model: path_view.model
 //页数
 property alias count: path_page.count
 
 PathView{
 id: path_view
 anchors.fill: parent
 
 //此属性保存任何时候在路径上可见的项目数。
 //将pathItemCount设置为undefined将显示路径上的所有项目。
 //因为path代码的问题,设置为2最合适
 pathItemCount: 2
 
 //测试时,把clip去掉就能看到完整的
 //clip: true
 
 //向前移动,即顺序0 1 2 3
 movementDirection: PathView.Positive
 
 //切换的时间
 highlightMoveDuration: 1000
 
 //视图中突出显示(当前项目)的首选范围,默认值PathView.StrictlyEnforceRange
 //配合preferredHighlight的范围0.5 0.5,就能显示在正中,切换更自然
 //highlightRangeMode: PathView.StrictlyEnforceRange
 
 //希望当前选定的项位于路径的中间,则将突出显示范围设置为0.5,0.5
 preferredHighlightBegin: 0.5
 preferredHighlightEnd: 0.5
 path: Path {
  startX: -path_view.width/2
  startY: path_view.height / 2
 
  PathLine {
  x: path_view.pathItemCount * path_view.width-path_view.width / 2
  y: path_view.height / 2
  }
 }
 onModelChanged: {
  if(path_timer.running){
  path_timer.restart();
  }
 }
 
 //测试用
 //model: ["red","green","blue"]
 //delegate: Rectangle{
 // width: path_view.width
 // height: path_view.height
 // color: modelData
 //}
 }
 //定时切换
 Timer{
 id: path_timer
 running: control.visible
 repeat: true
 interval: 3000
 onTriggered: {
  //至少两个才切换
  if(path_view.count>1)
  path_view.currentIndex=(path_view.currentIndex+1)%path_view.count
 }
 }
 //右下角小圆圈
 PageIndicator {
 id: path_page
 anchors{
  right: parent.right
  bottom: parent.bottom
  margins: 30
 }
 count: path_view.count
 currentIndex: path_view.currentIndex
 spacing: control.indicatorWidth
 delegate: Rectangle{
  width: control.indicatorWidth
  height: width
  radius: width/2
  color: "white"
  //非当前页就灰色
  opacity: index===path_page.currentIndex?1:0.6
  Behavior on opacity {
  OpacityAnimator{
   duration: 200
  }
  }
  //点击跳转到该页
  //还有问题,非连续的item,他会快速连续切换到目标index
  //因为不是直接切换,有闪烁的感觉
  MouseArea{
  anchors.fill: parent
  onClicked: {
   path_view.currentIndex=index;
   if(path_timer.running){
   path_timer.restart();
   }
  }
  }
 }
 }
}

//main.qml  

测试了不同的Item个数

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
 
Window {
 visible: true
 width: 700
 height: 500
 title: qsTr("龚建波 1992")
 
 color: "#021B39"
 
 Column{
 anchors.centerIn: parent
 spacing: 10
 CircleView{
  width: 100
  height: 50
  model:["red","green","blue","orange"]
 
  delegate: Rectangle {
  width: 100
  height: 50
  color: modelData
  //Component.onCompleted: console.log(modelData,"completed")
  }
 
  Rectangle{
  anchors.fill: parent
  color: "transparent"
  border.color: "white"
  }
 }
 CircleView{
  width: 100
  height: 50
  model:["red","green","blue"]
 
  delegate: Rectangle {
  width: 100
  height: 50
  color: modelData
  //Component.onCompleted: console.log(modelData,"completed")
  }
 
  Rectangle{
  anchors.fill: parent
  color: "transparent"
  border.color: "white"
  }
 }
 CircleView{
  width: 100
  height: 50
  model:["red","green"]
 
  delegate: Rectangle {
  width: 100
  height: 50
  color: modelData
  //Component.onCompleted: console.log(modelData,"completed")
  }
 
  Rectangle{
  anchors.fill: parent
  color: "transparent"
  border.color: "white"
  }
 }
 CircleView{
  width: 100
  height: 50
  model:["red"]
 
  delegate: Rectangle {
  width: 100
  height: 50
  color: modelData
  //Component.onCompleted: console.log(modelData,"completed")
  }
 
  Rectangle{
  anchors.fill: parent
  color: "transparent"
  border.color: "white"
  }
 }
 CircleView{
  width: 100
  height: 50
 
  delegate: Rectangle {
  width: 100
  height: 50
  color: modelData
  //Component.onCompleted: console.log(modelData,"completed")
  }
 
  Rectangle{
  anchors.fill: parent
  color: "transparent"
  border.color: "white"
  }
 }
 }
}

借鉴:链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python搭建简易服务器分析与实现
Dec 15 Python
Python编程中实现迭代器的一些技巧小结
Jun 21 Python
浅谈Django REST Framework限速
Dec 12 Python
为什么入门大数据选择Python而不是Java?
Mar 07 Python
python实现倒计时小工具
Jul 29 Python
python GUI库图形界面开发之PyQt5美化窗体与控件(异形窗体)实例
Feb 25 Python
解决jupyter notebook 出现In[*]的问题
Apr 13 Python
python中sort sorted reverse reversed函数的区别说明
May 11 Python
python 解决pycharm运行py文件只有unittest选项的问题
Sep 01 Python
Python3爬虫ChromeDriver的安装实例
Feb 06 Python
python基于机器学习预测股票交易信号
May 25 Python
分享3个非常实用的 Python 模块
Mar 03 Python
Python基于smtplib协议实现发送邮件
Jun 03 #Python
Pytorch环境搭建与基本语法
Jun 03 #Python
如何学习Python time模块
Jun 03 #Python
使用openCV去除文字中乱入的线条实例
Jun 02 #Python
Python能做什么
Jun 02 #Python
什么是Python中的匿名函数
Jun 02 #Python
学习python需要有编程基础吗
Jun 02 #Python
You might like
水质对咖图啡风味的影响具体有哪些
2021/03/03 冲泡冲煮
php实现telnet功能示例
2014/04/08 PHP
百度工程师讲PHP函数的实现原理及性能分析(一)
2015/05/13 PHP
php使用标签替换的方式生成静态页面
2015/05/21 PHP
Yii框架多语言站点配置方法分析【中文/英文切换站点】
2020/04/07 PHP
json的前台操作和后台操作实现代码
2012/01/20 Javascript
Dom 学习总结以及实例的使用介绍
2013/04/24 Javascript
JavaScript中的console.group()函数详细介绍
2014/12/29 Javascript
jquery实现邮箱自动填充提示功能
2015/11/17 Javascript
JSON遍历方式实例总结
2015/12/07 Javascript
Vue监听数据对象变化源码
2017/03/09 Javascript
javascript实现二叉树遍历的代码
2017/06/08 Javascript
Jquery和CSS实现选择框重置按钮功能
2018/11/08 jQuery
JS执行控制之节流模式实例分析
2018/12/21 Javascript
vue elementUI table表格数据 滚动懒加载的实现方法
2019/04/04 Javascript
详解微信小程序-获取用户session_key,openid,unionid - 后端为nodejs
2019/04/29 NodeJs
微信小程序实现收货地址左滑删除
2020/11/18 Javascript
Python采集腾讯新闻实例
2014/07/10 Python
Python函数式编程指南(三):迭代器详解
2015/06/24 Python
python+selenium开发环境搭建图文教程
2017/08/11 Python
Python中对象的引用与复制代码示例
2017/12/04 Python
python读取文件名称生成list的方法
2018/04/27 Python
使用python 将图片复制到系统剪贴中
2019/12/13 Python
python3读取csv文件任意行列代码实例
2020/01/13 Python
使用sklearn对多分类的每个类别进行指标评价操作
2020/06/11 Python
canvas画布实现手写签名效果的示例代码
2019/04/23 HTML / CSS
介绍一下常见的木马种类
2014/11/15 面试题
DOM和JQuery对象有什么区别
2016/11/11 面试题
英文自我鉴定
2013/12/10 职场文书
如何写一份好的自荐信
2014/01/02 职场文书
你的创业计划书怎样才能打动风投
2014/02/06 职场文书
网络工程师专家职业发展路线
2014/02/14 职场文书
企业务虚会发言材料
2014/10/20 职场文书
2015年八一建军节活动总结
2015/03/20 职场文书
公司内部升职自荐信
2015/03/27 职场文书
Win11怎么解除儿童账号限制?Win11解除微软儿童账号限制方法
2022/07/07 数码科技