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.

相关推荐
cykaw25908 小时前
QT-JSON
qt·json
Forest_HAHA9 小时前
<5>, Qt系统相关
开发语言·qt
幸运黒锦鲤11 小时前
Qt creator 设计页面控件认识与了解
开发语言·qt
Forest_HAHA18 小时前
<4>, Qt窗口
开发语言·qt
开开心心_Every19 小时前
功能丰富的PDF处理免费软件推荐
运维·笔记·qt·智能手机·pdf·音视频·ruby
uyeonashi21 小时前
【从零开始学习QT】信号和槽
数据库·c++·qt·学习
夜泉_ly1 天前
Qt -使用OpenCV得到SDF
c++·qt·算法
半青年1 天前
IEC61850规约客户端软件开发实战(第二章)
java·c++·qt·网络协议·c#·信息与通信·iec61850
@Turbo@1 天前
【QT】在QT6中读取文件的方法
开发语言·数据库·qt
156082072191 天前
在QT环境下部署FFT库
开发语言·qt