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.

相关推荐
小短腿的代码世界5 小时前
Qt日志系统深度解析:从qDebug到企业级日志框架
开发语言·qt
Morwit10 小时前
QML组件之间的通信方案(暴露子组件)
c++·qt·职场和发展
金色熊族14 小时前
解析QTransform的用法
qt
追烽少年x16 小时前
Qt多线程编程:QThread与QtConcurrent的对比与应用
qt
小短腿的代码世界1 天前
Qt实时盈亏计算深度解析:从持仓数据到动态盈亏展示
开发语言·qt
Python私教1 天前
GenericAgent PySide6 桌面应用深度解析:悬浮按钮 + 聊天面板的原生 Qt 方案
开发语言·数据库·qt
用户805533698031 天前
现代Qt开发教程(新手篇)1.11——定时器
c++·qt
小短腿的代码世界1 天前
Qt券商接口封装深度解析:统一API设计与多源适配
开发语言·qt·单元测试
T0uken1 天前
基于 vcpkg 与 LLVM-MinGW 的 Qt6 静态链接开发方案
c++·windows·qt
Ulyanov1 天前
《现代 Python 桌面应用架构实战:PySide6 + QML 从入门到工程化》 开发环境搭建与工具链极简主义 —— 拒绝臃肿,构建工业级基座
开发语言·python·qt·ui·架构·系统仿真