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.

相关推荐
努力还债的学术吗喽几秒前
【项目】pyqt5基于python的照片整蛊项目
开发语言·python·qt
weixin_459548903 分钟前
Qt对话框设计
qt
QT 小鲜肉9 小时前
【QT/C++】Qt定时器QTimer类的实现方法详解(超详细)
开发语言·数据库·c++·笔记·qt·学习
MeowKnight9589 小时前
【Qt】Qt实践记录3——UDP通信
笔记·qt
扶尔魔ocy14 小时前
【QT自定义2D控件】QGraphics绘制仪表盘
数据库·qt·microsoft
开心-开心急了15 小时前
关于Flutter与Qt for python 的一些技术、开源、商用等问题
开发语言·python·qt·flutter
友友马15 小时前
『 QT 』按钮类控件属性解析
开发语言·数据库·qt
QT 小鲜肉17 小时前
【Git、GitHub、Gitee】按功能分类汇总Git常用命令详解(超详细)
c语言·网络·c++·git·qt·gitee·github
老歌老听老掉牙1 天前
解决 PyQt5 中 sipPyTypeDict() 弃用警告的完整指南
python·qt
共享家95272 天前
Qt窗口教程(上)
开发语言·qt