、概念

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.pswp.cn/news/913017.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/913017.shtml
英文地址,请注明出处:http://en.pswp.cn/news/913017.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android图片优化

在 Android 中加载大图时,如果不进行优化处理,很容易导致内存溢出(OOM)和应用卡顿。以下是几种高效处理大图加载的方法和最佳实践: 1. 使用图片加载库(推荐) 成熟的第三方库已经处理了内存管理…

【机器人】复现 DOV-SG 机器人导航 | 动态开放词汇 | 3D 场景图

DOV-SG 建了动态 3D 场景图,并使用LLM大型语言模型进行任务分解,从而能够在交互式探索过程中对 3D 场景图进行局部更新。 来自RA-L 2025,适合长时间的 语言引导移动操作,动态开放词汇 3D 场景图。 论文地址:Dynamic …

mongodb 中dbs 时,local代表的是什么

在 MongoDB 中,local 是一个内置的系统数据库,用于存储当前 MongoDB 实例(或副本集节点)的元数据和内部数据,与其他数据库不同,local 数据库的数据不会被复制到副本集的其他成员。 local 数据库的核心作用 …

Spring Cloud(微服务部署与监控)

📌 摘要 在微服务架构中,随着服务数量的增长和部署复杂度的提升,如何高效部署、持续监控、快速定位问题并实现自动化运维成为保障系统稳定性的关键。 本文将围绕 Spring Cloud 微服务的部署与监控 展开,深入讲解: 微…

音频动态压缩算法曲线实现

Juce实现动态压缩曲线绘制 动态范围压缩算法(Dynamic Range Compression,DRC)是将音频信号的动态范围映射到一个较小的范围内的过程,即降低较高的峰值的信号电平,而不处理较安静的部分。DRC被广泛用于音频录制、制作工…

技术视界 | OpenLoong 控制框架:打造通用人形机器人智能系统的中枢基座

在人形机器人向通用性、智能化方向加速演进的当下,控制系统的角色正在发生根本变化:它不再只是底层驱动的接口适配层,也不只是策略调用的转译引擎,而是成为连接具身模型、异构本体与多样化任务的“中枢神经系统”。 在 2025 年张…

IOS 蓝牙连接

最近做一个硬件设备,写IOS相应的数据连接/分析代码时;发现一个问题,如果是开机,每次都能连接上。连接断开后,发现再也扫描不到了。通过第三方工具LightBlue,发现信号是-127。 此时进入设置查看蓝牙设备&am…

【硬核数学 · LLM篇】3.1 Transformer之心:自注意力机制的线性代数解构《从零构建机器学习、深度学习到LLM的数学认知》

我们已经完成了对机器学习和深度学习核心数学理论的全面探索。我们从第一阶段的经典机器学习理论,走到了第二阶段的深度学习“黑盒”内部,用线性代数、微积分、概率论、优化理论等一系列数学工具,将神经网络的每一个部件都拆解得淋漓尽致。 …

flutter封装vlcplayer的控制器

import dart:async;import package:flutter_vlc_player/flutter_vlc_player.dart; import package:flutter/material.dart;class GlobalVlcController extends ChangeNotifier {//设置单例/*static final GlobalVlcController _instance GlobalVlcController._internal();fact…

SEO-滥用元机器人、规范或 hreflang 标签

&#x1f9f1; 一、滥用 Meta Robots 标签 ❌ 常见问题&#xff1a; 问题描述设置了 noindex 不该屏蔽的页面比如产品页、分类页被意外 noindex&#xff0c;导致不被收录设置 nofollow 导致内链失效所有链接都被 nofollow&#xff0c;影响爬虫抓取路径在 <meta> 标签和…

笨方法学python -练习14

程序&#xff1a; from sys import argv script, user_name argv prompt > print(f"Hi {user_name}, Im the {script} script.") print("Id like to ask you a few questions.") print(f"Do you like me {user_name}?") likes in…

Frida:配置自动补全 in VSCode

1. 前言 编写 frida JavaScript 脚本是一件 very 普遍的事情在 Android Reverse 中。为了方便编写&#xff0c;配置相关的环境使其能够自动补全是很关键的&#xff0c;即通过类名就能够获取该类的所有对外接口信息&#xff0c;这是面向对象编程的核心优势&#xff0c;可惜我没…

FPGA矩阵算法实现

简介 现如今设计上对速度的要求越来越高&#xff0c;而矩阵相乘含有大量的乘法和加法计算&#xff0c;造成计算时间长从而影响性能&#xff0c;本章节利用FPGA实现浮点型矩阵运算&#xff0c;可在极短时间内完成矩阵运算。 知识介绍 矩阵计算公式如下&#xff1a; 需要保证A的…

C#可空类型详解:从基础到高级应用

C#可空类型详解&#xff1a;从基础到高级应用 在C#编程中&#xff0c;可空类型是一个非常重要的概念&#xff0c;它允许我们为值类型&#xff08;如int、bool、DateTime等&#xff09;分配null值&#xff0c;从而增强了代码的表达能力和灵活性。本文将详细介绍C#中可空类型的各…

Elasticsearch:异常检测入门

在我之前的文章里&#xff0c;我有讲述很多有关使用机器学习来针对数据做异常监测的文章。你可以在 “开发者上手指南” 里的 “机器学习” 章节中找到。在今天的练习中&#xff0c;我将使用最新的 Elastic Stack 9.0.2 来展示如何在 Elasticsearch 中使用机器学习的方法来进行…

ARuler3.1.3 | 高级版测量应用,利用AR技术测量所有

ARuler是一款非常便捷的测量应用程序&#xff0c;专为需要精确测量的用户设计。它不仅具备强大的3D测量功能&#xff0c;还利用增强现实&#xff08;AR&#xff09;技术&#xff0c;为用户提供多种测量选项&#xff0c;包括角度、长度、宽度、高度、面积和体积等。无论是日常生…

MapReduce分布式计算框架:从原理到实战

大家好&#xff01;今天我们来聊聊大数据处理领域的一个重要框架——MapReduce。作为Google提出的经典分布式计算模型&#xff0c;MapReduce极大地简化了海量数据的处理流程。无论你是大数据新手还是有一定经验的开发者&#xff0c;这篇文章都会让你对MapReduce有更深入的理解。…

Redis 7 及更高版本的脚本化方案

一、背景与动机 传统的 Redis 脚本机制依赖于客户端加载 EVAL 脚本&#xff0c;存在以下局限&#xff1a; 网络与编译开销 每次调用都要传输脚本源码或重新加载 SHA1。缓存失效风险 重启、主从切换、SCRIPT FLUSH 后脚本缓存丢失&#xff0c;事务易失败。调试与运维困难 SHA1…

Java项目:基于SSM框架实现的云端学习管理系统【ssm+B/S架构+源码+数据库+毕业论文】

摘 要 互联网发展至今&#xff0c;无论是其理论还是技术都已经成熟&#xff0c;而且它广泛参与在社会中的方方面面。它让信息都可以通过网络传播&#xff0c;搭配信息管理工具可以很好地为人们提供服务。针对课程学习信息管理混乱&#xff0c;出错率高&#xff0c;信息安全性差…

【压力测试之_Jmeter链接Oracle数据库链接】

Oracle数据库链接 欢迎来到挖坑避坑课堂链接数据库 欢迎来到挖坑避坑课堂 之前性能测试都是业务之类的&#xff0c;数据库压测很少涉及&#xff0c;就会出现很多各式各样的问题&#xff0c;首要问题就是Jmeter链接数据库的问题&#xff0c;本篇主要讲解Jmeter链接Oracle数据库…