SearchField QML Type
专门用于搜索功能的输入框。更多
Import Statement: | import QtQuick.Controls |
Since: | Qt 6.10 |
Inherits: |
属性
- clearIndicator : real
- currentIndex : int
- delegate : Component
- delegateModel : model
- highlightedIndex : int
- live : bool
- popup : Popup
- searchIndicator : real
- suggestionCount : int
- suggestionModel : model
- text : string
- textRole : string
信号
- void accepted()
- void activated(int index)
- void highlighted(int index)
- void searchTriggered()
- void textEdited()
详细说明
SearchField 是一个专门用于搜索功能的输入框。该控件包括一个文本字段、搜索和清除图标,以及一个显示建议或搜索结果的弹出窗口。
SearchField 模型的作用
SearchField 能够可视化提供modelData
角色的标准数据模型:
- 只有一个角色的模型
- 没有命名角色的模型(JavaScript 数组、整数)
在使用具有多个已命名角色的模型时,必须对 SearchField 进行配置,使其text 和delegate 实例使用特定的text role 。
ListModel { id : fruitModel ListElement { name: "Apple"; color: "green" } ListElement { name: "Cherry"; color: "red" } ListElement { name: "Banana"; color: "yellow" } ListElement { name: "Orange"; color: "orange" } ListElement { name: "WaterMelon"; color: "pink" } } QSortFilterProxyModel { id: fruitFilter sourceModel: fruitModel filterRegularExpression: RegExp(fruitSearch.text, "i") filterRole: 0 // needs to be set explicitly } SearchField { id: fruitSearch suggestionModel: fruitFilter textRole: "name" anchors.horizontalCenter: parent.horizontalCenter }
属性文档
clearIndicator : real |
该属性保存清除指示符。
currentIndex : int |
delegate : Component |
该属性包含一个在搜索栏弹出窗口中显示项目的委托。
建议使用ItemDelegate (或任何其他AbstractButton 衍生工具)作为委托。这样可以确保交互按预期进行,并在适当的时候自动关闭弹出窗口。如果使用其他类型作为委托,则必须手动关闭弹出窗口。例如,如果使用MouseArea :
delegate: Rectangle { // ... MouseArea { // ... onClicked: searchField.popup.close() } }
delegateModel : model |
该属性包含为搜索字段提供委托实例的模型。
它通常分配给popup 的contentItem 中的ListView 。
highlightedIndex : int |
该属性保存弹出列表中当前高亮显示项的索引。
高亮显示的项目被激活后,弹出窗口关闭,currentIndex 更新为与highlightedIndex
匹配,该属性重置为-1
,表示当前没有高亮显示的项目。
另请参阅 highlighted() 和currentIndex 。
live : bool |
该属性包含一个布尔值,用于决定是否在每次文本编辑时触发搜索。
当设置为true
时,每次文本更改都会发出searchTriggered() 信号,从而可以对每次按键做出响应。当设置为false
时,只有当用户按下 Enter 或 Return 键时,才会发出searchTriggered() 信号。
另请参阅 searchTriggered()。
popup : Popup |
该属性用于保存弹出窗口。
如有必要,可手动打开或关闭弹出窗口:
onSpecialEvent: searchField.popup.close()
searchIndicator : real |
该属性显示搜索指示符。
suggestionCount : int |
该属性用于保存要从建议模型中显示的建议数量。
suggestionModel : model |
该属性包含用于在弹出菜单中显示搜索建议的数据模型。
SearchField { textRole: "age" suggestionModel: ListModel { ListElement { name: "Karen"; age: "66" } ListElement { name: "Jim"; age: "32" } ListElement { name: "Pamela"; age: "28" } } }
另请参阅 textRole 。
text : string |
textRole : string |
此属性保存用于在弹出列表中显示建议模型中项目的模型角色。
当模型有多个角色时,可设置textRole
以确定应显示哪个角色。
信号文档
void accepted() |
当用户按 Enter 或 Return 键确认输入时,将发出该信号。
该信号通常用于根据最终输入的文本触发搜索或操作,并表明用户打算完成或提交查询。
注: 相应的处理程序是onAccepted
。
另请参阅 searchTriggered() 。
void activated(int index) |
当index 上的项目被用户激活时,将发出该信号。
当一个项目在弹出窗口打开时被选中并导致弹出窗口关闭(以及currentIndex 变更)时,该项目即被激活。currentIndex 属性被设置为index 。
注: 相应的处理程序是onActivated
。
另请参阅 currentIndex 。
void highlighted(int index) |
当用户高亮显示弹出列表中index 处的项目时,将发出该信号。
高亮信号仅在弹出窗口打开且项目被高亮时发出,但不一定是activated 。
注: 相应的处理程序是onHighlighted
。
另请参阅 highlightedIndex 。
void searchTriggered() |
该信号在搜索操作启动时发出。
有两种情况1.按下 Enter 或 Return 键时,它将与accepted() 信号一起发出 2.编辑文本时,如果live 属性设置为true
,则会发出该信号。
根据所需的交互模式,该信号非常适合在用户键入时按需或实时启动搜索。
注: 相应的处理程序是onSearchTriggered
。
另请参阅 accepted() 和textEdited()。
void textEdited() |
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.