A specialized input field designed to use for search functionality. 更多...
| import 语句: |
import QtQuick.Controls
|
| Since: | Qt 6.10 |
| 继承: |
SearchField is a specialized input field designed to use for search functionality. The control includes a text field, search and clear icons, and a popup that displays suggestions or search results.
SearchField is able to visualize standard
data models
that provide the
modelData
role:
When using models that have multiple named roles, SearchField must be configured to use a specific text role for its text and delegate 实例。
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" }
}
SortFilterProxyModel {
id: fruitFilter
model: fruitModel
sorters: [
RoleSorter {
roleName: "name"
}
]
filters: [
FunctionFilter {
component CustomData: QtObject { property string name }
property var regExp: new RegExp(fruitSearch.text, "i")
onRegExpChanged: invalidate()
function filter(data: CustomData): bool {
return regExp.test(data.name);
}
}
]
}
SearchField {
id: fruitSearch
suggestionModel: fruitFilter
textRole: "name"
anchors.horizontalCenter: parent.horizontalCenter
}
|
clearIndicator
:
real
|
This property holds the clear indicator.
|
currentIndex : int |
This property holds the index of the currently selected suggestion in the popup list.
默认值为
-1
when count is
0
,和
0
否则。
另请参阅 activated (), text ,和 highlightedIndex .
|
delegate : Component |
This property holds a delegate that presents an item in the search field popup.
It is recommended to use ItemDelegate (or any other AbstractButton derivatives) as the delegate. This ensures that the interaction works as expected, and the popup will automatically close when appropriate. When other types are used as the delegate, the popup must be closed manually. For example, if MouseArea is used:
delegate: Rectangle {
// ...
MouseArea {
// ...
onClicked: searchField.popup.close()
}
}
|
delegateModel
:
model
|
This property holds the model that provides delegate instances for the search field.
It is typically assigned to a ListView 在 contentItem 的 popup .
|
highlightedIndex
:
int
|
This property holds the index of the currently highlighted item in the popup list.
When the highlighted item is activated, the popup closes,
currentIndex
is updated to match
highlightedIndex
, and this property is reset to
-1
, indicating that no item is currently highlighted.
另请参阅 highlighted () 和 currentIndex .
|
live : bool |
This property holds a boolean value that determines whether the search is triggered on every text edit.
当设为
true
,
searchTriggered
() signal is emitted on each text change, allowing you to respond to every keystroke. When set to
false
,
searchTriggered
() is only emitted when the user presses the Enter or Return key.
另请参阅 searchTriggered ().
|
popup : Popup |
This property holds the popup.
The popup can be opened or closed manually, if necessary:
onSpecialEvent: searchField.popup.close()
|
searchIndicator
:
real
|
This property holds the search indicator.
|
suggestionCount
:
int
|
This property holds the number of suggestions to display from the suggestion model.
|
suggestionModel : model |
This property holds the data model used to display search suggestions in the popup menu.
SearchField { textRole: "age" suggestionModel: ListModel { ListElement { name: "Karen"; age: "66" } ListElement { name: "Jim"; age: "32" } ListElement { name: "Pamela"; age: "28" } } }
另请参阅 textRole .
|
text : string |
This property holds the current input text in the search field.
Text is bound to the user input, triggering suggestion updates or search logic.
另请参阅 searchTriggered () 和 textEdited ().
|
textRole : string |
This property holds the model role used to display items in the suggestion model shown in the popup list.
When the model has multiple roles,
textRole
can be set to determine which role should be displayed.
|
void accepted () |
This signal is emitted when the user confirms their input by pressing the Enter or Return key.
This signal is typically used to trigger a search or action based on the final text input, and it indicates the user's intention to complete or submit the query.
注意:
相应处理程序是
onAccepted
.
另请参阅 searchTriggered ().
|
void activated ( int index ) |
This signal is emitted when the item at index is activated by the user.
An item is activated when it is selected while the popup is open, causing the popup to close (and currentIndex to change). The currentIndex property is set to index .
注意:
相应处理程序是
onActivated
.
另请参阅 currentIndex .
|
void highlighted ( int index ) |
This signal is emitted when the item at index in the popup list is highlighted by the user.
The highlighted signal is only emitted when the popup is open and an item is highlighted, but not necessarily activated .
注意:
相应处理程序是
onHighlighted
.
另请参阅 highlightedIndex .
|
void searchTriggered () |
This signal is emitted when a search action is initiated.
It occurs in two cases: 1. When the Enter or Return key is pressed, it will be emitted together with
accepted
() signal 2. When the text is edited and if the
live
property is set to
true
, this signal will be emitted.
This signal is ideal for initiating searches both on-demand and in real-time as the user types, depending on the desired interaction model.
注意:
相应处理程序是
onSearchTriggered
.
另请参阅 accepted () 和 textEdited ().
|
void textEdited () |
This signal is emitted every time the user modifies the text in the search field, typically with each keystroke.
注意:
相应处理程序是
onTextEdited
.
另请参阅 searchTriggered ().