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.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數據庫…