qml中ComboBox组件onCurrentIndexChanged与onActivated的使用

在qml页面中使用ComboBox时,一般会有以下用法:

json 复制代码
ComboBox{
	id: box
	model: yourBindingModel
	onCurrentIndexChanged: {   
		//业务代码
	}	
}  

通常不会有什么问题,切换下拉列表时触发onCurrentIndexChanged,然后执行业务代码。

但是,如果有

json 复制代码
currentIndex: yourBindingIndex

即给combox组件的currentIndex进行了数据绑定,那么在初始化UI时,如果yourBindingIndex有有效值,则会触发onCurrentIndexChanged,此时并不是我们手动操作下拉列表触发的,这种情况一般是预期以外的,onCurrentIndexChanged内业务代码的执行也会造成程序异常。那如何避免呢?

使用onActivated

json 复制代码
onActivated: {}

看官方文档描述:

This signal is similar to currentIndex changed, but will only be emitted if the combo box index was changed by the user, not when set programmatically.

index is the activated model index, or -1 if a new string is accepted.

The corresponding handler is onActivated.

相关推荐
丰锋ff8 小时前
3.1 Qt事件之事件处理器
开发语言·qt
反转180度10 小时前
Qt 6 + C++17 实现 SFTP 批量部署:工业设备运维工具实战解析
运维·c++·qt
我不是疯子是傻子12 小时前
Qt CAN通信周期发送抖动?实测定时器精度校准与时间戳补偿方案
开发语言·数据库·qt
2023自学中13 小时前
Qt 简易聊天室(第二篇),单线程 改为 多线程,方便后续增加文件传输功能
c++·qt
丰锋ff18 小时前
1.7在Qt窗口中添加右键菜单
开发语言·qt
richard_yuu1 天前
JSON vs XML vs 二进制:3种序列化终极选型
xml·c++·qt·学习·json
秋田君1 天前
QT_实战TCP 聊天程序说明文档以及实现
数据库·qt·tcp/ip
byxdaz1 天前
QT中服务使用
开发语言·qt
秋田君2 天前
QT_网络编程体系
网络·arm开发·qt
Quz2 天前
QML TextArea 实战(下):大文本加载与性能优化
qt