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代码
Mar 13 Python
python实现2048小游戏
Mar 30 Python
python+matplotlib绘制3D条形图实例代码
Jan 17 Python
Python实现判断并移除列表指定位置元素的方法
Apr 13 Python
django中静态文件配置static的方法
May 20 Python
django初始化数据库的实例
May 27 Python
windows下pycharm安装、创建文件、配置默认模板
Jul 31 Python
Python Matplotlib库安装与基本作图示例
Jan 09 Python
python爬虫解决验证码的思路及示例
Aug 01 Python
Django用户认证系统 User对象解析
Aug 02 Python
Python3.5 win10环境下导入kera/tensorflow报错的解决方法
Dec 19 Python
Python内置的数据类型及使用方法
Apr 13 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
使用字符串函数输出整数化的PHP版本号
2006/10/09 PHP
ThinkPHP使用smarty模板引擎的方法
2014/07/01 PHP
PHP单例模式是什么 php实现单例模式的方法
2016/05/14 PHP
javascript之ESC(第二类混淆)
2007/05/06 Javascript
老生常谈JavaScript数组的用法
2016/06/10 Javascript
JS实现屏蔽网页右键复制及ctrl+c复制的方法【2种方法】
2016/09/04 Javascript
jQuery 插件封装的方法
2016/11/16 Javascript
JQ中$(window).load和$(document).ready区别与执行顺序
2017/03/01 Javascript
Angular中自定义Debounce Click指令防止重复点击
2017/07/26 Javascript
详解Node.js异步处理的各种写法
2019/06/09 Javascript
js实现点击图片在屏幕中间弹出放大效果
2019/09/11 Javascript
p5.js绘制旋转的正方形
2019/10/23 Javascript
python中lambda与def用法对比实例分析
2015/04/30 Python
python实现马耳可夫链算法实例分析
2015/05/20 Python
python opencv之SURF算法示例
2018/02/24 Python
python简单贪吃蛇开发
2019/01/28 Python
Python类的继承用法示例
2019/01/31 Python
Python3.5迭代器与生成器用法实例分析
2019/04/30 Python
python计算无向图节点度的实例代码
2019/11/22 Python
详解PyQt5中textBrowser显示print语句输出的简单方法
2020/08/07 Python
python3跳出一个循环的实例操作
2020/08/18 Python
如何在python中处理配置文件代码实例
2020/09/27 Python
flask框架中的cookie和session使用
2021/01/31 Python
澳大利亚UGG工厂直销:Australian Ugg Boots
2017/10/14 全球购物
Farfetch中文官网:奢侈品牌时尚购物平台
2020/03/15 全球购物
若干个Java基础面试题
2015/05/19 面试题
重阳节登山活动方案
2014/02/03 职场文书
公司承诺书范文
2014/05/19 职场文书
艾滋病宣传标语
2014/06/25 职场文书
重阳节活动总结
2014/08/27 职场文书
执法作风整顿剖析材料
2014/10/11 职场文书
加强机关作风建设心得体会
2014/10/22 职场文书
2014年公路养护工作总结
2014/12/04 职场文书
平安建设汇报材料
2014/12/29 职场文书
js实现上传图片到服务器
2021/04/11 Javascript
postgresql中如何执行sql文件
2023/05/08 PostgreSQL