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

QML ComboBox简介

1.简介

ComboBox是一个组合按钮和弹出列表。它提供了一种以占用最小屏幕空间的方式向用户显示选项列表的方法。
ComboBox用数据模型填充。数据模型通常是JavaScript数组、ListModel或整数,但也支持其他类型的数据模型。

常用属性:

  • count : int:组合框中的项目数。
  • displayText : string:当前显示的文本
  • currentIndex : int:组合框中当前项目的索引。当 count 为 0 时默认值为 -1,否则默认值为 0。
  • currentText : string:当前项的文本
  • currentValue : string:当前项的值
  • delegate : Component:代理
  • editable : bool:组合框是否可编辑。默认值为false。
  • indicator : Item:指示器项
  • model : model:数据model
  • popup : Popup:弹窗
  • textRole : string:保存用于存储与模型中每个项关联的值的模型角色
  • validator : Validator:可编辑组合框的输入文本验证器。
  • valueRole : string :此属性保存用于存储与模型中每个项关联的值的模型角色

2.示例

示例1:显示基础属性,valueRole和textRole区别,设置model等。

Window {visible: truewidth: 500height: 500title: qsTr("Hello World")ComboBox {textRole: "text"valueRole: "value"width: 100displayText: currentText + 123model: [{ value: 100, text: qsTr("aaa") },{ value: 200, text: qsTr("bbb") },{ value: 300, text: qsTr("ccc") }]onCurrentTextChanged: {console.log("text: ",currentText)}onCurrentValueChanged: {console.log("value: ",currentValue)}}}

打印结果:可以明显看到valueRole和textRole区别。

 示例2:自定义combobox

ComboBox {id: controlmodel: ["First", "Second", "Third"]//针对model项中每一项的绘制delegate: ItemDelegate {width: control.widthcontentItem: Text {text: modelDatacolor: index % 2 ?"red":"blue"  //设置交替色font: control.fontelide: Text.ElideRightverticalAlignment: Text.AlignVCenter}highlighted: control.highlightedIndex === index}//指示器的绘制(上下箭头)indicator: Canvas {id: canvasx: control.width - width - control.rightPaddingy: control.topPadding + (control.availableHeight - height) / 2width: 12height: 8contextType: "2d"Connections {target: controlfunction onPressedChanged() { canvas.requestPaint(); }}onPaint: {context.reset();context.moveTo(0, 0);context.lineTo(width, 0);context.lineTo(width / 2, height);context.closePath();context.fillStyle = control.pressed ? "#17a81a" : "#21be2b";context.fill();}}//控制控件内的内容的显示contentItem: Text {leftPadding: 0rightPadding: control.indicator.width + control.spacingtext: control.displayTextfont: control.fontcolor: control.pressed ? "red" : "blue"verticalAlignment: Text.AlignVCenter    //居中elide: Text.ElideRight}//控制控件背景的绘制background: Rectangle {implicitWidth: 120implicitHeight: 40color:"yellow"border.color: control.pressed ? "#17a81a" : "#21be2b"border.width: control.visualFocus ? 2 : 1radius: 2}//弹出下拉框,绘制整个下拉控件popup: Popup {y: control.height - 1width: control.widthimplicitHeight: contentItem.implicitHeightpadding: 1contentItem: ListView {clip: trueimplicitHeight: contentHeightmodel: control.popup.visible ? control.delegateModel : nullcurrentIndex: control.highlightedIndexScrollIndicator.vertical: ScrollIndicator { }}background: Rectangle {border.color: "gray"radius: 2layer.enabled: truelayer.effect: DropShadow{//设置弹出框阴影verticalOffset: 3radius: 8samples: 17color: "#80000000"}}}
}

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

相关文章:

  • uniapp使用webview嵌入vue页面及通信
  • 深度学习部署笔记(九): CUDA RunTime API-2.1内存管理
  • Idea+maven+spring-cloud项目搭建系列--11-2 dubbo鉴权日志记录数据统一封装
  • SOLIDWORKS免费培训 SW大型装配体模式课程
  • xxl-job registry fail
  • 【C#进阶】C# 反射
  • 公网NAT网关与VPC NAT网关介绍与实践
  • Windows中UWP、WPF和Windows窗体的区别
  • Flink从入门到精通系列(一)
  • 云原生应用风险介绍
  • 什么是测试用例设计?
  • 数据分析:基于K-近邻(KNN)对Pima人糖尿病预测分析
  • Kettle体系结构及源码解析
  • 大数据 | (二)SSH连接报错Permission denied
  • 前端——6.文本格式化标签和<div>和<span>标签
  • 浅谈Xpath注入漏洞
  • Oracle LogMiner分析归档日志
  • 趣味三角——第15章——傅里叶定理
  • 市场营销的核心是什么?
  • c/cpp - 多线程/进程 多进程
  • MySQL必知必会 | 存储过程、游标、触发器
  • 优化Facebook广告ROI的数据驱动方法:从投放到运营
  • 动态规划入门经典问题讲解
  • 快速入门深度学习1(用时1h)
  • PaddleOCR关键信息抽取(KIE)的训练(SER训练和RE训练)错误汇总
  • 信息收集之搜索引擎
  • Flutter(四)布局类组件
  • 【黑马】Java基础从入门到起飞目录合集
  • PMP考前冲刺3.10 | 2023新征程,一举拿证
  • JavaScript Math常用方法