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++ 老炮儿的技术栈2 小时前
Qt 编写 TcpClient 程序 详细步骤
c语言·开发语言·数据库·c++·qt·算法
打工哪有不疯的3 小时前
使用 MSYS2 为 Qt (MinGW 32/64位) 完美配置 OpenSSL
c++·qt
LYOBOYI1235 小时前
qtcpSocket详解
c++·qt
无小道7 小时前
Qt——网络编程
开发语言·qt
云中飞鸿7 小时前
VS编写QT程序,如何向linux中移植?
linux·开发语言·qt
草莓熊Lotso7 小时前
Qt 主窗口核心组件实战:菜单栏、工具栏、状态栏、浮动窗口全攻略
运维·开发语言·人工智能·python·qt·ui
云中飞鸿18 小时前
QTCreator快捷键
qt
十五年专注C++开发20 小时前
QStyleItemDelegate:自定义列表控件类神器
qt·model·view·delegate
无小道21 小时前
Qt——事件简单介绍
开发语言·前端·qt
mengzhi啊1 天前
QUndoView 本质是一个 Qt 界面控件(继承自 QListView),专门适配 QUndoStack
qt