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.

相关推荐
太阳以西阿19 分钟前
【计算机图形学】01 OpenGL+Qt
开发语言·qt
ULTRA??1 小时前
Qt 数据类型体系(AI总结)
c++·qt
蓑衣夜行1 小时前
Qt QWebEngine 开发避坑手册
qt·web·qwebengine
姓刘的哦2 小时前
RK3568开发板运行Qt
开发语言·qt
LNN20223 小时前
Qt 5.8.0 下实现触摸屏热插拔功能的探索与实践(2)
开发语言·qt
꧁坚持很酷꧂4 小时前
Ubuntu系统下Qt程序连接串口设备没有问题,但运行时出现Permission denied的解决方法
linux·qt·ubuntu
开始了码6 小时前
qt配置文件::INI介绍
qt
lijiatu100866 小时前
[C++] QTimer与Qt事件循环机制 实验探究
c++·qt
水天需0107 小时前
Linux 命令查找名为 main.cpp 文件
qt
火山灿火山9 小时前
Qt信号和槽
开发语言·qt