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.

相关推荐
小小码农Come on15 小时前
QT QSS语法
qt
小小码农Come on16 小时前
QT中窗口位置、相对位置、绝对位置
android·开发语言·qt
2301_8035545221 小时前
Qt中connect()实现信号与槽连接这一核心机制
java·数据库·qt
Frank学习路上21 小时前
【Qt】问题记录ld: framework ‘AGL‘ not found on MacOS 26
开发语言·qt·macos
凯子坚持 c1 天前
Qt常用控件指南(4)
开发语言·qt
uoKent1 天前
Qt C++项目基础
c++·qt
机器视觉知识推荐、就业指导1 天前
Qt 6 所有 QML 类型(官方完整清单 · 原始索引版)
开发语言·qt
少控科技1 天前
QT新手日记033
开发语言·qt
少控科技2 天前
QT新手日记 030
开发语言·qt
xmRao2 天前
Qt+FFmpeg 实现 Windows 音频采集
windows·qt·ffmpeg