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.

相关推荐
C++ 老炮儿的技术栈3 小时前
GCC编译时无法向/tmp 目录写入临时汇编文件,因为设备空间不足,解决
linux·运维·开发语言·汇编·c++·git·qt
米优7 小时前
qt+vlc实现国标客户端测试工具
qt·gb28181·vlc
米优8 小时前
qt+vlc实现解码h264/h265裸码流播放
开发语言·qt·vlc
小小码农Come on9 小时前
QT面试题总结
开发语言·qt
特立独行的猫a9 小时前
HarmonyOS鸿蒙PC的QT应用开发:(二、开发环境搭建及第一个HelloWorld)
qt·华为·harmonyos·鸿蒙·鸿蒙pc
史迪仔01129 小时前
[QML] QT5和QT6 圆角的不同设置方法
前端·javascript·qt
一只小小的土拨鼠10 小时前
【避坑指南】Qt + MSVC + CUDA 项目链接与发布报错全记录
开发语言·qt
code_pgf10 小时前
RPC数据集整理与 Scalabel 标注说明
qt·网络协议·rpc
白杆杆红伞伞10 小时前
Qt Lock&Semaphore
qt·线程同步
专注VB编程开发20年10 小时前
WPS 2024 Windows版UI用QT5和自研DirectUI-vba,jsa
qt·vba·wps·jsa·directui