当前位置:网站首页>QML之GridView实现滑动的网格布局
QML之GridView实现滑动的网格布局
2022-04-22 06:05:00 【PRML_MAN】
最近在做关于滑动网格的需求,正好用到了GridView实现对应的功能。

1、GridView 关键属性
GridView相关的属性比较多,下面是一些关键的属性:
cellHeight: 表格中单个格子的高度
cellWeight: 表格中单个格子的宽度
currentIndex: 当前焦点所在的索引
delegate: 代理,即格子内的实现方式
layoutDirection: 所有格子的排列顺序
model: 表格的数据
2、GridView实现滑动表格

实现思路:
1、创建一个GridView,设置格子的大小
2、创建一个model(即数据库),一个delegate(格子内的显示形式)
实现代码:
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
id:root
width: 640
height: 480
visible: true
title: qsTr("Hello World")
Rectangle {
anchors.fill: parent
anchors.margins: 10
border.color: "black"
GridView {
id: grid
anchors.fill: parent
cellWidth: 200
cellHeight: 200
anchors.margins: 20
model: ShowModel{
}
delegate: ShowDelegate{
}
clip: true // 超出边界的进行裁剪,即不显示,默认为false
boundsBehavior: Flickable.StopAtBounds // 滑动不超出父框的大小
}
}
}
ShowModel.qml
import QtQuick 2.0
ListModel {
id: fruitModel
ListElement {
name:"apple"
picSrc: "pic/apple.jpg"
cost: 2
}
ListElement {
name:"banana"
picSrc: "pic/香蕉.jpg"
cost: 2
}
ListElement {
name:"orange"
picSrc:"pic/橘子.jpg"
cost: 2
}
ListElement {
name:"pair"
picSrc:"pic/梨.jpg"
cost: 2
}
ListElement {
name:"apple"
picSrc: "pic/apple.jpg"
cost: 2
}
ListElement {
name:"banana"
picSrc: "pic/香蕉.jpg"
cost: 2
}
ListElement {
name:"orange"
picSrc:"pic/橘子.jpg"
cost: 2
}
ListElement {
name:"pair"
picSrc:"pic/梨.jpg"
cost: 2
}
}
ShowDelegate.qml
import QtQuick 2.0
Rectangle {
width: grid.cellWidth
height: grid.cellHeight
anchors.margins: 10
radius: 10
border.color: "black"
Image{
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
source: model.picSrc
sourceSize.width: parent.width * 0.90
sourceSize.height: parent.height * 0.90
}
MouseArea {
anchors.fill: parent
onClicked: {
console.log(parent.width, parent.height)
}
}
}
版权声明
本文为[PRML_MAN]所创,转载请带上原文链接,感谢
https://blog.csdn.net/PRML_MAN/article/details/116796831
边栏推荐
- 【AI视野·今日NLP 自然语言处理论文速览 第二十八期】Wed, 1 Dec 2021
- 新型实例分割网络PANet(Path Aggregation Network for Instance Segmentation)源码和更新详解
- 通用型枚举常量类
- Flink的安装部署及WordCount测试
- ROS系列(四):ROS通信机制系列(5):服务通信实操
- 肠-肝轴:宿主-微生物群相互作用影响肝癌发生
- 多标签分类问题中的评价指标:准确率,交叉熵代价函数
- 饮食-肠道微生物群对心血管疾病的相互作用
- Handwritten universal anti chattering and throttling function
- ACMer中的大神都很喜欢数学(题目方法的研究)
猜你喜欢
随机推荐
pip3安装requests库-最全踩坑整理
新型实例分割网络PANet(Path Aggregation Network for Instance Segmentation)源码和更新详解
隐藏在发表的宏基因组文章背后故事,如何发掘和学习
spark小案例——RDD,sparkSQL
编译代码时缺少头文件No such file or directory #include “XXXXXX.h“
win10下MinGW安装及配置
饮食-肠道微生物群对心血管疾病的相互作用
解决seq2seq+attention机器翻译中的技术小难题
铁与肠道菌群
使用keras框架编写三层神经网络解决房价预测问题
Two-stage目标检测技术发展(二)Fast R-CNN和Faster R-CNN
【AI视野·今日Robot 机器人论文速览 第三十二期】Wed, 20 Apr 2022
衣原体感染——原因、症状、治疗及预防
How is the diversity of flora formed, the relationship with health, and how to improve it?
ROS系列(三):ROS架构简介
ROS系列(四):ROS通信机制系列(5):服务通信实操
宏基因组组装质量评估新方法-MAGISTA
如何在CMake项目中引入OpenCV
使用stream load向doris写数据的案例
【AI视野·今日Robot 机器人论文速览 第二十九期】Mon, 14 Feb 2022









