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.

相关推荐
森G4 小时前
61、信号与槽机制在 TCP 编程中的应用---------网络编程
网络·c++·qt·网络协议·tcp/ip
古德new5 小时前
鸿蒙PC迁移:Photoflare Qt 图片编辑器鸿蒙PC适配全记录
qt·编辑器·harmonyos
古德new9 小时前
鸿蒙PC迁移:Anki Qt 记忆卡片工具鸿蒙PC适配全记录
qt·华为·harmonyos
雪的季节11 小时前
Qt 进程间通信(IPC)全方案
qt
雪的季节12 小时前
Qt Graphs 2D+3D介绍
qt·3d
小鹏linux13 小时前
鸿蒙PC迁移:TupiTube Desk Qt 2D 动画工作室鸿蒙PC适配全记录
qt·华为·harmonyos
鸽芷咕13 小时前
鸿蒙PC迁移:LANDrop Qt 局域网传输工具鸿蒙PC适配全记录
qt·华为·harmonyos
Lhan.zzZ13 小时前
Qt Quick 嵌套 Dialog 与 ComboBox 层级混乱问题解决
c++·qt
森G13 小时前
67、Qt 多媒体框架概述---------多媒体
开发语言·qt
鸽芷咕13 小时前
鸿蒙PC迁移:MoonPlayer Qt 视频播放器鸿蒙PC适配全记录
qt·音视频·harmonyos