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.

相关推荐
前端市界9 小时前
前端视角: PyQt6+Vue3 跨界开发实战
前端·qt·pyqt
誰能久伴不乏14 小时前
Qt 动态属性(Dynamic Property)详解
开发语言·qt
枫叶丹415 小时前
【Qt开发】常用控件(四)
开发语言·qt
茉莉玫瑰花茶1 天前
Qt 常用控件 - 9
开发语言·qt
sqmeeting2 天前
QT6 如何在Linux Wayland 桌面系统抓屏和分享屏幕
linux·qt
姓刘的哦2 天前
Win10上Qt使用Libcurl库
开发语言·qt
hellokandy2 天前
QT QVersionNumber 比较版本号大小
qt·版本号·qversionnumber
常乐か2 天前
VS2022+QT5.15.2+OCCT7.9.1的开发环境搭建流程
开发语言·qt·opencascade
誰能久伴不乏2 天前
Qt TCP 客户端对象生命周期与连接断开问题解析
网络·qt·tcp/ip
kyle~2 天前
Qt---Qt函数库
开发语言·qt