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.

相关推荐
zhanghaha13144 小时前
Qt 中的类是什么?保姆级详解(新手版)
qt
看看我呀看看我6 小时前
基于Qt的虚拟键盘
c++·qt·qt5·qt6.3
kaixin_learn_qt_ing7 小时前
Qt抽屉窗体实现demo
开发语言·qt
huanbailu11 小时前
Qt 跨线程信号槽:connect 返回 true 但槽函数不执行的坑
开发语言·c++·qt·信号槽·跨线程·元类型
Quz14 小时前
QML SplitView:可拖拽分隔面板与自定义 Handle
qt
Quz14 小时前
QML SplitView 进阶:嵌套布局与状态持久化
qt
时间的拾荒人15 小时前
Qt 显示类控件详解:从 QLabel 到 QCalendarWidget 的实战指南
开发语言·qt·面试
kaixin_learn_qt_ing1 天前
Qt插件机制demo示例
qt
Quz1 天前
QML Flickable:内容滚动与边界行为
qt
Quz1 天前
QML Flipable:卡片翻转与登录设置
qt