QML ListView悬浮标题栏
来源:互联网 发布:soap rest 知乎 编辑:程序博客网 时间:2024/05/17 02:47
前言
随着Qt版本的不断升级,现在用QML做移动开发越来越方便,并且代码也非常简洁容易理解,Android原生开发中的材料设计界面很普遍,很多软件都是走这个风格设计,并且随着Android开发很多开源代码不断的共享,使得用原生开发Android程序变得越来越简单并且还越来越漂亮,而QML中其实也有,只是没那么成熟,并且风格也没有Android原生开发的那么漂亮。
正文
今天要做的是关于QML的ListView控件悬浮标题栏,有点模仿Android原生开发中的界面形式,先来看看效果图。
(由于上传的图片限制大小,所以没有录制太长时间)
来看看源码
import QtQuick 2.6import QtQuick.Window 2.2import QtQuick.Controls 2.1Window { id:mainwindow visible: true width: 400 height: 600 Rectangle{ id:titlebar visible: true width: parent.width height: 50 z:3 opacity: 0 color:"red" Item{ id:item anchors.fill: parent opacity:1 Label{ anchors.centerIn: parent text:"hello world!!" font.pixelSize: 27 } } } NumberAnimation { id:ani1 target: view property: "contentY" duration: 200 to:-view.headerItem.height running: false easing.type: Easing.InOutQuad } NumberAnimation { id:ani2 target: view property: "contentY" duration: 200 to:-titlebar.height running: false easing.type: Easing.InOutQuad } ListView{ id:view anchors.fill: parent model:20 onContentYChanged:{ if(view.contentY < -titlebar.height){ titlebar.opacity = 1-(-view.contentY - titlebar.height)/100. titlebar.y = -view.contentY - titlebar.height } else{ item.opacity = 1 titlebar.y = 0 } } onMovementEnded: { if(view.contentY < -view.headerItem.height/2.){ ani1.running = true } else if(view.contentY < -titlebar.height){ ani2.running = true } } header:Rectangle{ width: view.width height: 150 color:"red" Label{ id:label text:"this is header" font.pixelSize: 27 color:"white" anchors.horizontalCenter: parent.horizontalCenter anchors.top: parent.top anchors.topMargin: 30 } } delegate:Rectangle{ id:delegate width: view.width height: 50 border.width: 1 Label{ anchors.fill: parent text: index } } footer: Rectangle{ id:foot width: parent.width } Component.onCompleted: { var t_h = view.model.count * 50 + titlebar.height if(t_h < view.height){ view.footerItem.height = view.height - t_h } } ScrollIndicator.vertical: ScrollIndicator { } }}
这里是用了ListView和Rectangle组合起来实现的,首先用Rectangle来实现一个悬浮标题栏并设置为不可见,而ListView添加了一个header用于做列表的大标题栏,当滑动列表的时候控制悬浮标题栏的透明度以及位置。这里还添加了一个footer,这个主要是用于在列表数量比较少的情况下通过footer来设置高度 然后可以控制列表滑动,而footer的高度应该是在ListView的Model数量变化的时候动态改变,这里测试代为了简单,我写了固定的model数量,实际情况下应该检测model数量变化来改变footer的高度。比如说
model:ListModel{ id:listModel onCountChanged: { var t_h = count * delegate.height + titleBar.height if(t_h < listView.height){ listView.footerItem.height = listView.height - t_h } } }
代码很简单,不再赘述。
阅读全文
2 0
- QML ListView悬浮标题栏
- QML-ListView
- qml自定义标题栏
- qml去标题栏
- QML自定义标题栏
- qml实现自定义标题栏按钮
- table标题栏悬浮一直显示
- ScrollView实现悬浮标题栏效果
- QML ListView翻译
- QML ListView Element
- qml drag listview
- Qt qml listview刷新
- QML之ListView
- ListView悬浮头布局
- ListView顶部悬浮效果
- ListView 列表悬浮
- 实现 QMl MouseArea鼠标悬浮效果
- 悬浮:Listview多tab上滑悬浮
- 5. ESP8266固件的编译(RTOS SDK固件)
- 最长公共子序列 java 代码实现
- leetcode 268r
- 「游族杯」上海市高校程序设计邀请赛暨华东师范大学第九届 ECNU Coder 程序设计竞赛 (重现)
- java各种类型占字节数
- QML ListView悬浮标题栏
- Qt学习笔记1:初步认识控件
- Java重写与重载
- PyQt5学习记录(2)---QMainWindow菜单栏、状态栏和工具栏
- VMware虚拟机三种网络模式详解-Host-Only(仅主机模式)
- 链队列的定义及相关操作
- 44. Struts2_表单的重复提交问题
- Eclipse实用快捷键个人总结
- Makefile中的死循环问题--学习李云《驾驭Makefile》中的疑惑解决