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.

相关推荐
Source.Liu7 小时前
【QOwnNotes】QOwnNotes 介绍
qt
特立独行的猫a7 小时前
QT开发鸿蒙PC应用:环境搭建及第一个HelloWorld
开发语言·qt·harmonyos·环境搭建·鸿蒙pc
零小陈上(shouhou6668889)10 小时前
YOLOv8+PyQt5输电线路缺陷检测(目前最全面的类别检测,可以从图像、视频和摄像头三种路径检测)
python·qt·yolo
Larry_Yanan10 小时前
Qt多进程(五)QUdpSocket
开发语言·c++·qt·学习·ui
ht巷子11 小时前
Qt:容器类的迭代
开发语言·c++·qt
byxdaz12 小时前
Qt 中将 QWidget 改为模态的方法
qt
世转神风-15 小时前
qt-通信协议基础-QStirng转QByteArray-与字节序互动
开发语言·qt
特立独行的猫a16 小时前
QT开发鸿蒙PC应用:第一个Qt Widget应用入门
数据库·qt·harmonyos·鸿蒙pc·qtwidget
深蓝海拓16 小时前
PySide6从0开始学习的笔记(十八) MVC(Model-View-Controller)模式的图形渲染体系
笔记·python·qt·学习·pyqt
世转神风-17 小时前
qt-通信协议基础-固定长度-小端字节序补0x00指导
开发语言·qt