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 相关文章推荐
ptyhon实现sitemap生成示例
Mar 30 Python
python调用Moxa PCOMM Lite通过串口Ymodem协议实现发送文件
Aug 15 Python
Python中的左斜杠、右斜杠(正斜杠和反斜杠)
Aug 30 Python
matplotlib调整子图间距,调整整体空白的方法
Aug 03 Python
python Pexpect 实现输密码 scp 拷贝的方法
Jan 03 Python
使用celery执行Django串行异步任务的方法步骤
Jun 06 Python
Python从入门到精通之环境搭建教程图解
Sep 26 Python
在Python中预先初始化列表内容和长度的实现
Nov 28 Python
Python Tornado核心及相关原理详解
Jun 24 Python
使用keras实现非线性回归(两种加激活函数的方式)
Jul 05 Python
Pytho爬虫中Requests设置请求头Headers的方法
Sep 22 Python
在python3.9下如何安装scrapy的方法
Feb 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
Ping服务的php实现方法,让网站快速被收录
2012/02/04 PHP
Youku 视频绝对地址获取的方法详解
2013/06/26 PHP
php 地区分类排序算法
2013/07/01 PHP
ThinkPHP惯例配置文件详解
2014/07/14 PHP
php中namespace use用法实例分析
2016/01/22 PHP
几行代码轻松实现PHP文件打包下载zip
2017/03/01 PHP
PHP ajax+jQuery 实现批量删除功能实例代码小结
2018/12/06 PHP
会自动逐行上升的文本框
2006/06/30 Javascript
js文件中调用js的实现方法小结
2009/10/23 Javascript
Ajax执行顺序流程及回调问题分析
2012/12/10 Javascript
js网页中的(运行代码)功能实现思路
2013/02/04 Javascript
浅谈EasyUI中编辑treegrid的方法
2015/03/01 Javascript
jquery.gridrotator实现响应式图片展示画廊效果
2015/06/23 Javascript
JS实现黑色风格的网页TAB选项卡效果代码
2015/10/09 Javascript
JavaScript代码因逗号不规范导致IE不兼容的问题
2016/02/25 Javascript
Vue 2.0 中依赖注入 provide/inject组合实战
2019/06/20 Javascript
vue iview的菜单组件Mune 点击不高亮的解决方案
2019/11/01 Javascript
python写入中英文字符串到文件的方法
2015/05/06 Python
Python实现的爬取百度文库功能示例
2019/02/16 Python
详解爬虫被封的问题
2019/04/23 Python
Django 实现admin后台显示图片缩略图的例子
2019/07/28 Python
python通过对字典的排序,对json字段进行排序的实例
2020/02/27 Python
使用Python发现隐藏的wifi
2020/03/04 Python
解决pyecharts运行后产生的html文件用浏览器打开空白
2020/03/11 Python
pycharm 激活码及使用方式的详细教程
2020/05/12 Python
Python如何对齐字符串
2020/07/30 Python
企业家王石演讲稿:坚持与放下
2014/04/27 职场文书
文明礼貌演讲稿
2014/05/12 职场文书
媒矿安全生产承诺书
2014/05/23 职场文书
体育教师求职信
2014/06/30 职场文书
新党章的学习心得体会
2014/11/07 职场文书
2019最新版股权转让及委托持股协议书范本
2019/08/07 职场文书
MySQL中in和exists区别详解
2021/06/03 MySQL
Redis如何实现分布式锁
2021/08/23 Redis
SQL Server查询某个字段在哪些表中存在
2022/03/03 SQL Server
多人盗宝《绿林侠盗》第三赛季4.5上线 跨平台实装
2022/04/03 其他游戏