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.

相关推荐
笨笨马甲1 小时前
Qt network开发
开发语言·qt
mengzhi啊18 小时前
Qt Designer UI 界面 拖的两个 QLineEdit,想按 Tab 从第一个跳到第二个
qt
笨笨马甲20 小时前
Qt MQTT
开发语言·qt
姓刘的哦1 天前
Qt实现蚂蚁线
开发语言·qt
Ivy_belief1 天前
Qt网络编程实战:从零掌握 QUdpSocket 及 UDP 通信
网络·qt·udp
丁劲犇1 天前
在Trae Solo模式下用Qt HttpServer和Concurrent升级MCP服务器绘制6G互联网覆盖区域
服务器·开发语言·qt·ai·6g·mcp·trae
笨笨马甲1 天前
Qt MODBUS协议
开发语言·qt
我喜欢就喜欢1 天前
Word 模板匹配与样式同步技术详解
开发语言·c++·qt·word·模板匹配
Ronin3051 天前
【Qt常用控件】容器类控件和布局管理器
开发语言·qt·常用控件·布局管理器·容器类控件
2301_803554522 天前
qt信号槽机制以及底层实现原理
开发语言·qt