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.

相关推荐
GOTXX26 分钟前
【Qt】Qt Creator开发基础:项目创建、界面解析与核心概念入门
开发语言·数据库·c++·qt·图形渲染·图形化界面·qt新手入门
EverestVIP7 小时前
qt中,父类中有Q_OBJECT,子类中还需要加Q_OBJECT吗
开发语言·qt
Cuit小唐8 小时前
Qt炫酷仪表盘
开发语言·qt
火山上的企鹅11 小时前
异形遮罩之QML中的 `OpacityMask` 实战
开发语言·qml·opacitymask
mahuifa13 小时前
pyqt环境配置
python·qt·pycharm·pyqt·环境配置
kchmmd13 小时前
基于QtC++音乐播放器whisper语音转文字歌词解析
c++·qt
island131414 小时前
【QT】QT界面的美容院 -- QSS
开发语言·qt
溟洵15 小时前
【C++ Qt】认识Qt、Qt 项目搭建流程(图文并茂、通俗易懂)
开发语言·c++·qt
Zfox_16 小时前
【QT】 常用控件【输入类】
c++·qt·qt5·客户端开发
FreeLikeTheWind.1 天前
Qt问题之 告别软件因系统默认中文输入法导致错误退出的烦恼
开发语言·c++·windows·经验分享·qt