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中声明只包含一个元素的元组数据方法
Aug 25 Python
基于python内置函数与匿名函数详解
Jan 09 Python
python实现单向链表详解
Feb 08 Python
tensorflow 恢复指定层与不同层指定不同学习率的方法
Jul 26 Python
Python OOP类中的几种函数或方法总结
Feb 22 Python
Python Flask框架模板操作实例分析
May 03 Python
Python 实现数据结构-堆栈和队列的操作方法
Jul 17 Python
Python使用itchat模块实现简单的微信控制电脑功能示例
Aug 26 Python
Python列表list常用内建函数实例小结
Oct 22 Python
python通过移动端访问查看电脑界面
Jan 06 Python
Django实现将views.py中的数据传递到前端html页面,并展示
Mar 16 Python
PyCharm vs VSCode,作为python开发者,你更倾向哪种IDE呢?
Aug 17 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笔记之:基于面向对象设计的详解
2013/05/14 PHP
php校验表单检测字段是否为空的方法
2015/03/20 PHP
php语言中使用json的技巧及json的实现代码详解
2015/10/27 PHP
PHP魔术方法以及关于独立实例与相连实例的全面讲解
2016/10/18 PHP
ThinkPHP5框架中使用JWT的方法示例
2020/06/03 PHP
常用一些Javascript判断函数
2012/08/14 Javascript
获取offsetTop和offsetLeft值的js代码(兼容)
2013/04/16 Javascript
如何创建一个JavaScript弹出DIV窗口层的效果
2013/09/25 Javascript
js中的replace方法使用介绍
2013/10/28 Javascript
IE8下String的Trim()方法失效的解决方法
2013/11/08 Javascript
jquery判断元素是否隐藏的多种方法
2014/05/06 Javascript
jQuery中prop()方法用法实例
2015/01/05 Javascript
基于jQuery的AJAX和JSON实现纯html数据模板
2016/08/09 Javascript
JS实现简单的tab切换选项卡效果
2016/09/21 Javascript
分分钟学会vue中vuex的应用(入门教程)
2017/09/14 Javascript
vue.js2.0 实现better-scroll的滚动效果实例详解
2018/08/13 Javascript
Vue开发实现吸顶效果的示例代码
2018/08/21 Javascript
理解Proxy及使用Proxy实现vue数据双向绑定操作
2020/07/18 Javascript
[45:18]2018DOTA2亚洲邀请赛 4.3 突围赛 Optic vs iG 第一场
2018/04/04 DOTA
python中模块查找的原理与方法详解
2017/08/11 Python
Python 输出时去掉列表元组外面的方括号与圆括号的方法
2018/12/24 Python
实例详解Matlab 与 Python 的区别
2019/04/26 Python
详解Python对JSON中的特殊类型进行Encoder
2019/07/15 Python
Python 中使用 PyMySQL模块操作数据库的方法
2019/11/10 Python
简单了解为什么python函数后有多个括号
2019/12/19 Python
解决pycharm中opencv-python导入cv2后无法自动补全的问题(不用作任何文件上的修改)
2020/03/05 Python
win10下opencv-python特定版本手动安装与pip自动安装教程
2020/03/05 Python
使用darknet框架的imagenet数据分类预训练操作
2020/07/07 Python
会议开场欢迎词
2014/01/15 职场文书
餐饮业员工工作决心书
2014/03/11 职场文书
美食节策划方案
2014/05/26 职场文书
党委书记个人对照检查材料
2014/09/15 职场文书
店面出租协议书范本
2014/11/28 职场文书
学术会议邀请函
2015/01/30 职场文书
现货白银电话营销话术
2015/05/29 职场文书
CSS作用域(样式分割)的使用汇总
2021/11/07 HTML / CSS