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.

相关推荐
FPGAI10 小时前
Qt编程之信号与槽
开发语言·qt
只因在人海中多看了你一眼12 小时前
B.50.10.09-RPC核心原理与电商应用
qt·网络协议·rpc
yudiandian201414 小时前
【QT 5.12.12 下载 Windows 版本】
开发语言·qt
炮院李教员17 小时前
使用Qt Core模块(无GUI依赖),确保程序作为后台服务/daemon运行,与任何GUI完全无交互。
开发语言·qt
歪歪10017 小时前
Qt Creator 打包应用程序时经常会遇到各种问题
开发语言·c++·qt·架构·编辑器
滴滴滴嘟嘟嘟.17 小时前
Qt自定义列表项与QListWidget学习
开发语言·qt·学习
滴滴滴嘟嘟嘟.20 小时前
Qt对话框与文件操作学习
开发语言·qt·学习
滴滴滴嘟嘟嘟.1 天前
Qt图表功能学习
开发语言·qt·学习
钱彬 (Qian Bin)1 天前
一文掌握工业缺陷检测项目实战(Pytorch算法训练、部署、C++ DLL制作、Qt集成)
c++·pytorch·python·qt·实战·工业缺陷检测·faster rcnn
FPGAI1 天前
Qt的入门
笔记·qt·学习