当前位置: 首页 > news >正文

Qt Quick 与 QML(四)qml中的Delegate系列委托组件

、概念

QML中,Delegate是一种非常重要的组件,特别是在使用ListViewGridViewPathView等视图组件时。Delegate用于定义每个列表或网格中的项目是如何展示的。通过自定义Delegate,你可以控制每个项目的外观和行为。

Delegate通常是一个自定义的ItemComponent,它定义了如何在列表或网格中显示每个项目。例如,你可以在Delegate中定义文本标签、图片、按钮等。

、常用委托控件

常用委托控件:ItemDelegate、CheckDelegate、RadioDelegate、SwitchDelegate和SwipeDelegate。

1.‌ItemDelegate

属性/信号/方法类型说明示例
‌highlighted‌bool当前项高亮状态,常用于列表选中项highlighted: ListView.isCurrentItem
‌text‌string显示的主文本内容text: model.name
‌icon‌var左侧图标资源(QtQuick.Icon类型)icon.source: "icon.png"
‌spacing‌real图标与文本间距(像素)spacing: 10
‌padding‌real内容区域内边距padding: 12
‌onClicked‌signal点击时触发onClicked: console.log(index)
‌background‌Component自定义背景组件background: Rectangle{color:"red"}

代码示例:

    //ItemDelegateListView {id: listViewItemx: 0; y: 0width: 300height: 300model: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]delegate: ItemDelegate {text: modelDatawidth: listViewItem.widthicon.source:"qrc:/image/user.png"icon.color: "transparent"//icon.name: "edit-cut"display: AbstractButton.TextBesideIconhighlighted: ListView.isCurrentItemonClicked: {listViewItem.currentIndex = index;console.log("clicked:", modelData)}}}

运行结果:

2.CheckDelegate

属性/信号/方法类型说明示例
‌checkState‌enum三态状态(Checked/Unchecked/PartiallyChecked)checkState: Qt.Checked
‌tristate‌bool是否启用三态模式tristate: true
‌checked‌bool当前选中状态checked: model.selected
‌onToggled‌signal状态变化时触发onToggled: model.checked=checked
‌indicator‌Component自定义复选框样式indicator: Rectangle{...}
‌nextCheckState‌method控制点击时的状态切换逻辑function nextCheckState(){...}

代码示例:

    //CheckDelegateproperty var selectedItems: []ListView {id: listViewCheckx: 320; y: 0width: 300height: 300model: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]delegate: CheckDelegate {text: modelDatawidth: listViewCheck.widthonCheckedChanged: {if (checked) {selectedItems.push(modelData)} else {selectedItems = selectedItems.filter(item => item !== modelData)}}}}Button {text: "Selected Items"anchors.bottom: listViewCheck.bottomanchors.horizontalCenter: listViewCheck.horizontalCenteronClicked: {console.log("Selected items:", selectedItems)}}

运行结果:

3.RadioDelegate

属性/信号/方法类型说明示例
‌autoExclusive‌bool同组单选按钮自动互斥autoExclusive: true
‌checked‌bool当前选中状态checked: model.isSelected
‌onClicked‌signal点击时触发onClicked: group.update(index)
‌indicator‌Component自定义单选按钮样式indicator: Circle{...}
‌text‌string显示文本标签text: model.option

代码示例:

//RadioDelegateproperty string selectedItem: "Item 1"  // 默认值需与初始checked项匹配ListView {id: listViewRadiox: 640; y: 0width: 300height: 300model: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]delegate: RadioDelegate {text: modelDatawidth: listViewRadio.widthchecked: selectedItem === modelDataonCheckedChanged: {if (checked) {selectedItem = modelData}}}}Button {text: "Selected Item"anchors.bottom: listViewRadio.bottomanchors.horizontalCenter: listViewRadio.horizontalCenteronClicked: console.log("Selected Item:", selectedItem)}

运行结果:

4.SwitchDelegate

属性/信号/方法类型说明示例
‌position‌real滑块位置(0.0-1.0)position: 0.7
‌checked‌bool当前开关状态checked: model.active
‌onToggled‌signal状态变化时触发onToggled: settings.save()
‌indicator‌Component自定义滑块组件indicator: Slider{...}
‌visualPosition‌real动画过渡中的可视位置visualPosition: 0.5

代码示例:

    //SwitchDelegateListView {id: listViewSwitchx: 0; y: 320width: 300height: 300model: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]delegate: SwitchDelegate {text: modelDatawidth: listViewSwitch.width}}

运行结果:

 5.SwipeDelegate

属性/信号/方法类型说明示例
‌swipe.left‌Component左滑时显示的组件swipe.left: Rectangle{...}
‌swipe.right‌Component右滑时显示的组件swipe.right: Image{...}
onCompletedsignal完成滑动操作时触发onCompleted: list.remove(index)
‌swipe.position‌real当前滑动进度(-1.0到1.0)swipe.position: 0.3
‌behind‌Component滑动后显示的背景组件behind: DeleteButton{...}

代码示例:

//SwipeDelegateListModel {id: listModelListElement { name: "Item 1" }ListElement { name: "Item 2" }ListElement { name: "Item 3" }ListElement { name: "Item 4" }ListElement { name: "Item 5" }}// 列表视图ListView {x: 320; y: 320width: 300height: 300model: listModeldelegate: SwipeDelegate {width: parent.widthheight: 60text: nameswipe.left: Rectangle {width: parent.widthheight: parent.heightcolor: "#ff4444"Label {anchors.centerIn: parenttext: "删除"color: "white"font.bold: true}// 点击删除按钮时移除项目MouseArea {anchors.fill: parentonClicked: listModel.remove(index)}}// 滑动完成时自动恢复位置//swipe.onCompleted: swipe.close()}// 滚动条ScrollIndicator.vertical: ScrollIndicator {}}

运行结果:

关键特性说明:

  1. 所有Delegate均继承自AbstractButton,支持点击/按压等基础交互
  2. 自定义样式推荐通过覆盖backgroundcontentItem实现
  3. SwipeDelegate需配合ListView使用才能获得完整手势支持

完整代码

代码:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 2.6Window {visible: truewidth: 960height: 640title: qsTr("Hello World")//ItemDelegateListView {id: listViewItemx: 0; y: 0width: 300height: 300model: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]delegate: ItemDelegate {text: modelDatawidth: listViewItem.widthicon.source:"qrc:/image/user.png"icon.color: "transparent"//icon.name: "edit-cut"display: AbstractButton.TextBesideIconhighlighted: ListView.isCurrentItemonClicked: {listViewItem.currentIndex = index;console.log("clicked:", modelData)}}}//CheckDelegateproperty var selectedItems: []ListView {id: listViewCheckx: 320; y: 0width: 300height: 300model: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]delegate: CheckDelegate {text: modelDatawidth: listViewCheck.widthonCheckedChanged: {if (checked) {selectedItems.push(modelData)} else {selectedItems = selectedItems.filter(item => item !== modelData)}}}}Button {text: "Selected Items"anchors.bottom: listViewCheck.bottomanchors.horizontalCenter: listViewCheck.horizontalCenteronClicked: {console.log("Selected items:", selectedItems)}}//RadioDelegateproperty string selectedItem: "Item 1"  // 默认值需与初始checked项匹配ListView {id: listViewRadiox: 640; y: 0width: 300height: 300model: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]delegate: RadioDelegate {text: modelDatawidth: listViewRadio.widthchecked: selectedItem === modelDataonCheckedChanged: {if (checked) {selectedItem = modelData}}}}Button {text: "Selected Item"anchors.bottom: listViewRadio.bottomanchors.horizontalCenter: listViewRadio.horizontalCenteronClicked: console.log("Selected Item:", selectedItem)}//SwitchDelegateListView {id: listViewSwitchx: 0; y: 320width: 300height: 300model: ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]delegate: SwitchDelegate {text: modelDatawidth: listViewSwitch.width}}//SwipeDelegateListModel {id: listModelListElement { name: "Item 1" }ListElement { name: "Item 2" }ListElement { name: "Item 3" }ListElement { name: "Item 4" }ListElement { name: "Item 5" }}// 列表视图ListView {x: 320; y: 320width: 300height: 300model: listModeldelegate: SwipeDelegate {width: parent.widthheight: 60text: nameswipe.left: Rectangle {width: parent.widthheight: parent.heightcolor: "#ff4444"Label {anchors.centerIn: parenttext: "删除"color: "white"font.bold: true}// 点击删除按钮时移除项目MouseArea {anchors.fill: parentonClicked: listModel.remove(index)}}// 滑动完成时自动恢复位置//swipe.onCompleted: swipe.close()}// 滚动条ScrollIndicator.vertical: ScrollIndicator {}}
}

运行结果:

http://www.lryc.cn/news/577227.html

相关文章:

  • Python I/O 库 包 iopath
  • ExGeo代码理解(七)main.py(运行模型进行训练和测试)
  • 生成式人工智能实战 | 变分自编码器(Variational Auto-Encoder, VAE)
  • 如何让Excel自动帮我们算加减乘除?
  • PHP语法基础篇(七):函数
  • 电脑开机加速工具,优化启动项管理
  • 深入比较 Gin 与 Beego:Go Web 框架的两大选择
  • 深度学习04 卷积神经网络CNN
  • 国科大深度学习作业2-基于 ViT 的 CIFAR10 图像分类
  • 工业级PHP任务管理系统开发:模块化设计与性能调优实践
  • DBeaver 设置阿里云中央仓库地址的操作步骤
  • 提示技术系列——链式提示
  • 数据结构入门-图的基本概念与存储结构
  • 【软考高项论文】论信息系统项目的干系人管理
  • 利用不坑盒子的Copilot,快速排值班表
  • upload-labs靶场通关详解:第15-16关
  • docker-compose部署Nacos、Seata、MySQL
  • 《Effective Python》第十一章 性能——使用 timeit 微基准测试优化性能关键代码
  • 初始CNN(卷积神经网络)
  • C++ cstring 库解析:C 风格字符串函数
  • 深入理解Webpack的灵魂:Tapable插件架构解析
  • 人工智能和云计算对金融未来的影响
  • 大模型在急性左心衰竭预测与临床方案制定中的应用研究
  • spring-ai 工作流
  • Github 2FA(Two-Factor Authentication/两因素认证)
  • 基于Flask技术的民宿管理系统的设计与实现
  • [论文阅读] Neural Architecture Search: Insights from 1000 Papers
  • macos 使用 vllm 启动模型
  • 在 VS Code 中安装与配置 Gemini CLI 的完整指南
  • java JNDI高版本绕过 工具介绍 自动化bypass