1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qquicksearchfield_p.h"
#include "qquickcontrol_p_p.h"
#include <private/qquickindicatorbutton_p.h>
#include <QtQuickTemplates2/private/qquicktextfield_p.h>
#include "qquickpopup_p_p.h"
#include "qquickdeferredexecute_p_p.h"
#include <private/qqmldelegatemodel_p.h>
#include "qquickabstractbutton_p.h"
#include "qquickabstractbutton_p_p.h"
#include <QtQuick/private/qquickaccessibleattached_p.h>
#if QT_CONFIG(quick_itemview)
# include <QtQuick/private/qquickitemview_p.h>
#endif
QT_BEGIN_NAMESPACE
/*!
\qmltype SearchField
\inherits Control
//! \nativetype QQuickSearchField
\inqmlmodule QtQuick.Controls
\since 6.10
\ingroup qtquickcontrols-input
\ingroup qtquickcontrols-focusscopes
\brief A specialized input field designed to use for search functionality.
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.
\image qtquickcontrols-searchfield.gif
\section1 SearchField Model Roles
SearchField is able to visualize standard \l {qml-data-models}{data models}
that provide the \c modelData role:
\list
\li models that have only one role
\li models that do not have named roles (JavaScript array, integer)
\endlist
When using models that have multiple named roles, SearchField must be configured
to use a specific \l {textRole}{text role} for its \l {text}{text}
and \l delegate instances.
\code
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
}
\endcode
*/
/*!
\qmlsignal void QtQuick.Controls::SearchField::activated(int index)
This signal is emitted when the item at \a 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 \l currentIndex to change).
The \l currentIndex property is set to \a index.
\sa currentIndex
*/
/*!
\qmlsignal void QtQuick.Controls::SearchField::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.
\sa searchTriggered()
*/
/*!
\qmlsignal void QtQuick.Controls::SearchField::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 \l live property is set to \c 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.
\sa accepted(), textEdited()
*/
/*!
\qmlsignal void QtQuick.Controls::SearchField::textEdited()
This signal is emitted every time the user modifies the text in the
search field, typically with each keystroke.
\sa searchTriggered()
*/
class QQuickSearchFieldPrivate : public QQuickControlPrivate
{
public:
Q_DECLARE_PUBLIC(QQuickSearchField)
bool isPopupVisible() const;
void showPopup();
void hidePopup();
static void hideOldPopup(QQuickPopup *popup);
void popupVisibleChanged();
void popupDestroyed();
void itemClicked();
void itemHovered();
void createdItem(int index, QObject *object);
void suggestionCountChanged();
void increaseCurrentIndex();
void decreaseCurrentIndex();
void setCurrentIndex(int index);
void createDelegateModel();
QString currentTextRole() const;
void selectAll();
void updateText();
void updateDisplayText();
QString textAt(int index) const;
bool isValidIndex(int index) const;
void cancelPopup();
void executePopup(bool complete = false);
bool handlePress(const QPointF &point, ulong timestamp) override;
bool handleRelease(const QPointF &point, ulong timestamp) override;
void startSearch();
void startClear();
void itemImplicitWidthChanged(QQuickItem *item) override;
void itemImplicitHeightChanged(QQuickItem *item) override;
void itemDestroyed(QQuickItem *item) override;
static inline QString popupName() { return QStringLiteral("popup"); }
QVariant suggestionModel;
bool hasCurrentIndex = false;
int currentIndex = -1;
QString text;
QString textRole;
bool live = true;
bool searchPressed = false;
bool clearPressed = false;
bool searchFlat = false;
bool clearFlat = false;
bool searchDown = false;
bool clearDown = false;
bool hasSearchDown = false;
bool hasClearDown = false;
bool ownModel = false;
QQmlInstanceModel *delegateModel = nullptr;
QQmlComponent *delegate = nullptr;
QQuickIndicatorButton *searchIndicator = nullptr;
QQuickIndicatorButton *clearIndicator = nullptr;
QQuickDeferredPointer<QQuickPopup> popup;
};
bool QQuickSearchFieldPrivate::isPopupVisible() const
{
return popup && popup->isVisible();
}
void QQuickSearchFieldPrivate::showPopup()
{
if (!popup)
executePopup(true);
if (popup && !popup->isVisible())
popup->open();
}
void QQuickSearchFieldPrivate::hidePopup()
{
if (popup && popup->isVisible())
popup->close();
}
void QQuickSearchFieldPrivate::hideOldPopup(QQuickPopup *popup)
{
if (!popup)
return;
qCDebug(lcItemManagement) << "hiding old popup" << popup;
popup->setVisible(false);
popup->setParentItem(nullptr);
#if QT_CONFIG(accessibility)
// Remove the item from the accessibility tree.
QQuickAccessibleAttached *accessible = accessibleAttached(popup);
if (accessible)
accessible->setIgnored(true);
#endif
}
void QQuickSearchFieldPrivate::popupVisibleChanged()
{
if (isPopupVisible())
QGuiApplication::inputMethod()->reset();
#if QT_CONFIG(quick_itemview)
QQuickItemView *itemView = popup->findChild<QQuickItemView *>();
if (itemView)
itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
#endif
if (popup->isVisible())
setCurrentIndex(currentIndex);
else
setCurrentIndex(0);
#if QT_CONFIG(quick_itemview)
if (itemView)
itemView->positionViewAtIndex(currentIndex, QQuickItemView::Beginning);
#endif
}
void QQuickSearchFieldPrivate::popupDestroyed()
{
Q_Q(QQuickSearchField);
popup = nullptr;
emit q->popupChanged();
}
void QQuickSearchFieldPrivate::itemClicked()
{
Q_Q(QQuickSearchField);
int index = delegateModel->indexOf(q->sender(), nullptr);
if (index != -1) {
setCurrentIndex(index);
updateDisplayText();
hidePopup();
emit q->activated(index);
}
}
void QQuickSearchFieldPrivate::itemHovered()
{
Q_Q(QQuickSearchField);
QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender());
if (!button || !button->isHovered() || !button->isEnabled()
|| QQuickAbstractButtonPrivate::get(button)->touchId != -1)
return;
int index = delegateModel->indexOf(button, nullptr);
if (index != -1) {
setCurrentIndex(index);
#if QT_CONFIG(quick_itemview)
if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
itemView->positionViewAtIndex(index, QQuickItemView::Contain);
#endif
}
}
void QQuickSearchFieldPrivate::createdItem(int index, QObject *object)
{
Q_UNUSED(index);
Q_Q(QQuickSearchField);
QQuickItem *item = qobject_cast<QQuickItem *>(object);
if (item && !item->parentItem()) {
if (popup)
item->setParentItem(popup->contentItem());
else
item->setParentItem(q);
QQuickItemPrivate::get(item)->setCulled(true);
}
QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(object);
if (button) {
button->setFocusPolicy(Qt::NoFocus);
connect(button, &QQuickAbstractButton::clicked, this,
&QQuickSearchFieldPrivate::itemClicked);
connect(button, &QQuickAbstractButton::hoveredChanged, this,
&QQuickSearchFieldPrivate::itemHovered);
}
}
void QQuickSearchFieldPrivate::suggestionCountChanged()
{
Q_Q(QQuickSearchField);
if (q->suggestionCount() == 0)
q->setCurrentIndex(-1);
emit q->suggestionCountChanged();
}
void QQuickSearchFieldPrivate::increaseCurrentIndex()
{
Q_Q(QQuickSearchField);
if (currentIndex < q->suggestionCount() - 1)
setCurrentIndex(currentIndex + 1);
else if (currentIndex == q->suggestionCount() - 1)
setCurrentIndex(0);
}
void QQuickSearchFieldPrivate::decreaseCurrentIndex()
{
if (currentIndex > 0)
setCurrentIndex(currentIndex - 1);
}
void QQuickSearchFieldPrivate::setCurrentIndex(int index)
{
Q_Q(QQuickSearchField);
if (currentIndex == index)
return;
currentIndex = index;
emit q->currentIndexChanged();
}
void QQuickSearchFieldPrivate::createDelegateModel()
{
Q_Q(QQuickSearchField);
bool ownedOldModel = ownModel;
QQmlInstanceModel *oldModel = delegateModel;
if (oldModel) {
disconnect(delegateModel, &QQmlInstanceModel::countChanged, this,
&QQuickSearchFieldPrivate::suggestionCountChanged);
disconnect(delegateModel, &QQmlInstanceModel::createdItem, this,
&QQuickSearchFieldPrivate::createdItem);
}
ownModel = false;
delegateModel = suggestionModel.value<QQmlInstanceModel *>();
if (!delegateModel && suggestionModel.isValid()) {
QQmlDelegateModel *dataModel = new QQmlDelegateModel(qmlContext(q), q);
dataModel->setModel(suggestionModel);
dataModel->setDelegate(delegate);
if (q->isComponentComplete())
dataModel->componentComplete();
ownModel = true;
delegateModel = dataModel;
}
if (delegateModel) {
connect(delegateModel, &QQmlInstanceModel::countChanged, this,
&QQuickSearchFieldPrivate::suggestionCountChanged);
connect(delegateModel, &QQmlInstanceModel::createdItem, this,
&QQuickSearchFieldPrivate::createdItem);
}
emit q->delegateModelChanged();
if (ownedOldModel)
delete oldModel;
}
QString QQuickSearchFieldPrivate::currentTextRole() const
{
return textRole.isEmpty() ? QStringLiteral("modelData") : textRole;
}
void QQuickSearchFieldPrivate::selectAll()
{
QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
if (!input)
return;
input->selectAll();
}
void QQuickSearchFieldPrivate::updateText()
{
Q_Q(QQuickSearchField);
QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
if (!input)
return;
const QString textInput = input->text();
if (text != textInput) {
q->setText(textInput);
emit q->textEdited();
if (live)
emit q->searchTriggered();
}
if (!text.isEmpty() && !isPopupVisible())
showPopup();
else if (text.isEmpty() && isPopupVisible())
hidePopup();
}
void QQuickSearchFieldPrivate::updateDisplayText()
{
Q_Q(QQuickSearchField);
const QString currentText = textAt(currentIndex);
if (text != currentText)
q->setText(currentText);
}
QString QQuickSearchFieldPrivate::textAt(int index) const
{
if (!isValidIndex(index))
return QString();
return delegateModel->stringValue(index, currentTextRole());
}
bool QQuickSearchFieldPrivate::isValidIndex(int index) const
{
return delegateModel && index >= 0 && index < delegateModel->count();
}
void QQuickSearchFieldPrivate::cancelPopup()
{
Q_Q(QQuickSearchField);
quickCancelDeferred(q, popupName());
}
void QQuickSearchFieldPrivate::executePopup(bool complete)
{
Q_Q(QQuickSearchField);
if (popup.wasExecuted())
return;
if (!popup || complete)
quickBeginDeferred(q, popupName(), popup);
if (complete)
quickCompleteDeferred(q, popupName(), popup);
}
bool QQuickSearchFieldPrivate::handlePress(const QPointF &point, ulong timestamp)
{
Q_Q(QQuickSearchField);
QQuickControlPrivate::handlePress(point, timestamp);
QQuickItem *si = searchIndicator->indicator();
QQuickItem *ci = clearIndicator->indicator();
const bool isSearch = si && si->isEnabled() && si->contains(q->mapToItem(si, point));
const bool isClear = ci && ci->isEnabled() && ci->contains(q->mapToItem(ci, point));
if (isSearch) {
searchIndicator->setPressed(true);
startSearch();
} else if (isClear) {
clearIndicator->setPressed(true);
startClear();
}
return true;
}
bool QQuickSearchFieldPrivate::handleRelease(const QPointF &point, ulong timestamp)
{
QQuickControlPrivate::handleRelease(point, timestamp);
if (searchIndicator->isPressed())
searchIndicator->setPressed(false);
else if (clearIndicator->isPressed())
clearIndicator->setPressed(false);
return true;
}
void QQuickSearchFieldPrivate::startSearch()
{
Q_Q(QQuickSearchField);
QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
if (!input)
return;
input->forceActiveFocus();
emit q->searchButtonPressed();
}
void QQuickSearchFieldPrivate::startClear()
{
Q_Q(QQuickSearchField);
if (text.isEmpty())
return;
// if text is not null then clear, also update suggestionModel
if (!text.isEmpty()) {
suggestionModel.clear();
q->setText(QString());
if (isPopupVisible())
hidePopup();
emit q->clearButtonPressed();
}
}
void QQuickSearchFieldPrivate::itemImplicitWidthChanged(QQuickItem *item)
{
QQuickControlPrivate::itemImplicitWidthChanged(item);
if (item == searchIndicator->indicator())
emit searchIndicator->implicitIndicatorWidthChanged();
if (item == clearIndicator->indicator())
emit clearIndicator->implicitIndicatorWidthChanged();
}
void QQuickSearchFieldPrivate::itemImplicitHeightChanged(QQuickItem *item)
{
QQuickControlPrivate::itemImplicitHeightChanged(item);
if (item == searchIndicator->indicator())
emit searchIndicator->implicitIndicatorHeightChanged();
if (item == clearIndicator->indicator())
emit clearIndicator->implicitIndicatorHeightChanged();
}
void QQuickSearchFieldPrivate::itemDestroyed(QQuickItem *item)
{
QQuickControlPrivate::itemDestroyed(item);
if (item == searchIndicator->indicator())
searchIndicator->setIndicator(nullptr);
if (item == clearIndicator->indicator())
clearIndicator->setIndicator(nullptr);
}
QQuickSearchField::QQuickSearchField(QQuickItem *parent)
: QQuickControl(*(new QQuickSearchFieldPrivate), parent)
{
Q_D(QQuickSearchField);
d->searchIndicator = new QQuickIndicatorButton(this);
d->clearIndicator = new QQuickIndicatorButton(this);
setFocusPolicy(Qt::StrongFocus);
setFlag(QQuickItem::ItemIsFocusScope);
setAcceptedMouseButtons(Qt::LeftButton);
#if QT_CONFIG(cursor)
setCursor(Qt::ArrowCursor);
#endif
d->init();
}
QQuickSearchField::~QQuickSearchField()
{
Q_D(QQuickSearchField);
d->removeImplicitSizeListener(d->searchIndicator->indicator());
d->removeImplicitSizeListener(d->clearIndicator->indicator());
if (d->popup) {
QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::visibleChanged, d,
&QQuickSearchFieldPrivate::popupVisibleChanged);
d->hideOldPopup(d->popup);
d->popup = nullptr;
}
}
/*!
\qmlproperty model QtQuick.Controls::SearchField::suggestionModel
This property holds the data model used to display search suggestions in the popup menu.
\code
SearchField {
textRole: "age"
suggestionModel: ListModel {
ListElement { name: "Karen"; age: "66" }
ListElement { name: "Jim"; age: "32" }
ListElement { name: "Pamela"; age: "28" }
}
}
\endcode
\sa textRole
*/
QVariant QQuickSearchField::suggestionModel() const
{
Q_D(const QQuickSearchField);
return d->suggestionModel;
}
void QQuickSearchField::setSuggestionModel(const QVariant &model)
{
Q_D(QQuickSearchField);
QVariant suggestionModel = model;
if (suggestionModel.userType() == qMetaTypeId<QJSValue>())
suggestionModel = get<QJSValue>(std::move(suggestionModel)).toVariant();
if (d->suggestionModel == suggestionModel)
return;
d->suggestionModel = suggestionModel;
d->createDelegateModel();
emit suggestionCountChanged();
if (isComponentComplete()) {
setCurrentIndex(suggestionCount() > 0 ? 0 : -1);
}
emit suggestionModelChanged();
}
/*!
\readonly
\qmlproperty model QtQuick.Controls::SearchField::delegateModel
This property holds the model that provides delegate instances for the search field.
It is typically assigned to a \l ListView in the \l {Popup::}{contentItem}
of the \l popup.
*/
QQmlInstanceModel *QQuickSearchField::delegateModel() const
{
Q_D(const QQuickSearchField);
return d->delegateModel;
}
/*!
\readonly
\qmlproperty int QtQuick.Controls::SearchField::suggestionCount
This property holds the number of suggestions to display from the suggestion model.
*/
int QQuickSearchField::suggestionCount() const
{
Q_D(const QQuickSearchField);
return d->delegateModel ? d->delegateModel->count() : 0;
}
/*!
\qmlproperty int QtQuick.Controls::SearchField::currentIndex
This property holds the index of the currently selected suggestion in the popup list.
The default value is \c -1 when count is \c 0, and \c 0 otherwise.
*/
int QQuickSearchField::currentIndex() const
{
Q_D(const QQuickSearchField);
return d->currentIndex;
}
void QQuickSearchField::setCurrentIndex(int index)
{
Q_D(QQuickSearchField);
d->hasCurrentIndex = true;
d->setCurrentIndex(index);
}
/*!
\qmlproperty string QtQuick.Controls::SearchField::text
This property holds the current input text in the search field.
Text is bound to the user input, triggering suggestion updates or search logic.
\sa searchTriggered(), textEdited()
*/
QString QQuickSearchField::text() const
{
Q_D(const QQuickSearchField);
return d->text;
}
void QQuickSearchField::setText(const QString &text)
{
Q_D(QQuickSearchField);
if (d->text == text)
return;
d->text = text;
emit textChanged();
}
/*!
\qmlproperty string QtQuick.Controls::SearchField::textRole
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, \c textRole can be set to determine
which role should be displayed.
*/
QString QQuickSearchField::textRole() const
{
Q_D(const QQuickSearchField);
return d->textRole;
}
void QQuickSearchField::setTextRole(const QString &textRole)
{
Q_D(QQuickSearchField);
if (d->textRole == textRole)
return;
d->textRole = textRole;
}
/*!
\qmlproperty bool QtQuick.Controls::SearchField::live
This property holds a boolean value that determines whether the search is triggered
on every text edit.
When set to \c true, the \l searchTriggered() signal is emitted on each text change,
allowing you to respond to every keystroke.
When set to \c false, the \l searchTriggered() is only emitted when the user presses
the Enter or Return key.
\sa searchTriggered()
*/
bool QQuickSearchField::isLive() const
{
Q_D(const QQuickSearchField);
return d->live;
}
void QQuickSearchField::setLive(const bool live)
{
Q_D(QQuickSearchField);
if (d->live == live)
return;
d->live = live;
}
/*!
\qmlproperty real QtQuick.Controls::SearchField::searchIndicator
\readonly
This property holds the search indicator.
*/
QQuickIndicatorButton *QQuickSearchField::searchIndicator() const
{
Q_D(const QQuickSearchField);
return d->searchIndicator;
}
/*!
\qmlproperty real QtQuick.Controls::SearchField::clearIndicator
\readonly
This property holds the clear indicator.
*/
QQuickIndicatorButton *QQuickSearchField::clearIndicator() const
{
Q_D(const QQuickSearchField);
return d->clearIndicator;
}
/*!
\qmlproperty Popup QtQuick.Controls::SearchField::popup
This property holds the popup.
The popup can be opened or closed manually, if necessary:
\code
onSpecialEvent: searchField.popup.close()
\endcode
*/
QQuickPopup *QQuickSearchField::popup() const
{
QQuickSearchFieldPrivate *d = const_cast<QQuickSearchFieldPrivate *>(d_func());
if (!d->popup)
d->executePopup(isComponentComplete());
return d->popup;
}
void QQuickSearchField::setPopup(QQuickPopup *popup)
{
Q_D(QQuickSearchField);
if (d->popup == popup)
return;
if (!d->popup.isExecuting())
d->cancelPopup();
if (d->popup) {
QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::destroyed, d,
&QQuickSearchFieldPrivate::popupDestroyed);
QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::visibleChanged, d,
&QQuickSearchFieldPrivate::popupVisibleChanged);
QQuickSearchFieldPrivate::hideOldPopup(d->popup);
}
if (popup) {
QQuickPopupPrivate::get(popup)->allowVerticalFlip = true;
popup->setClosePolicy(QQuickPopup::CloseOnEscape | QQuickPopup::CloseOnPressOutsideParent);
QObjectPrivate::connect(popup, &QQuickPopup::visibleChanged, d,
&QQuickSearchFieldPrivate::popupVisibleChanged);
// QQuickPopup does not derive from QQuickItemChangeListener, so we cannot use
// QQuickItemChangeListener::itemDestroyed so we have to use QObject::destroyed
QObjectPrivate::connect(popup, &QQuickPopup::destroyed, d,
&QQuickSearchFieldPrivate::popupDestroyed);
#if QT_CONFIG(quick_itemview)
if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
#endif
}
d->popup = popup;
if (!d->popup.isExecuting())
emit popupChanged();
}
/*!
\qmlproperty Component QtQuick.Controls::SearchField::delegate
This property holds a delegate that presents an item in the search field popup.
It is recommended to use \l ItemDelegate (or any other \l 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 \l MouseArea is used:
\code
delegate: Rectangle {
// ...
MouseArea {
// ...
onClicked: searchField.popup.close()
}
}
\endcode
\include delegate-ownership.qdocinc {no-ownership-since-6.11} {SearchField}
*/
QQmlComponent *QQuickSearchField::delegate() const
{
Q_D(const QQuickSearchField);
return d->delegate;
}
void QQuickSearchField::setDelegate(QQmlComponent *delegate)
{
Q_D(QQuickSearchField);
if (d->delegate == delegate)
return;
d->delegate = delegate;
QQmlDelegateModel *delegateModel = qobject_cast<QQmlDelegateModel *>(d->delegateModel);
if (delegateModel)
delegateModel->setDelegate(d->delegate);
emit delegateChanged();
}
bool QQuickSearchField::eventFilter(QObject *object, QEvent *event)
{
Q_D(QQuickSearchField);
switch (event->type()) {
case QEvent::MouseButtonRelease: {
QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem);
if (input->hasFocus()) {
if (!d->text.isEmpty() && !d->isPopupVisible())
d->showPopup();
}
break;
}
case QEvent::FocusOut: {
const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
const bool usingPopupWindows =
d->popup ? QQuickPopupPrivate::get(d->popup)->usePopupWindow() : false;
if (qGuiApp->focusObject() != this && !(hasActiveFocus && !usingPopupWindows)) {
d->hidePopup();
}
break;
}
default:
break;
}
return QQuickControl::eventFilter(object, event);
}
void QQuickSearchField::focusInEvent(QFocusEvent *event)
{
Q_D(QQuickSearchField);
QQuickControl::focusInEvent(event);
if ((event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason
|| event->reason() == Qt::ShortcutFocusReason)
&& d->contentItem)
d->contentItem->forceActiveFocus(event->reason());
}
void QQuickSearchField::focusOutEvent(QFocusEvent *event)
{
Q_D(QQuickSearchField);
QQuickControl::focusOutEvent(event);
const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
const bool usingPopupWindows = d->popup && QQuickPopupPrivate::get(d->popup)->usePopupWindow();
if (qGuiApp->focusObject() != d->contentItem && !(hasActiveFocus && !usingPopupWindows))
d->hidePopup();
}
void QQuickSearchField::keyPressEvent(QKeyEvent *event)
{
Q_D(QQuickSearchField);
const auto key = event->key();
if (!d->suggestionModel.isNull() && !d->text.isEmpty()) {
switch (key) {
case Qt::Key_Escape:
case Qt::Key_Back:
if (d->isPopupVisible()) {
d->hidePopup();
event->accept();
} else {
setText(QString());
}
break;
case Qt::Key_Return:
case Qt::Key_Enter:
d->updateDisplayText();
emit accepted();
emit searchTriggered();
event->accept();
break;
case Qt::Key_Up:
d->decreaseCurrentIndex();
event->accept();
break;
case Qt::Key_Down:
d->increaseCurrentIndex();
event->accept();
break;
case Qt::Key_Home:
d->setCurrentIndex(0);
event->accept();
break;
case Qt::Key_End:
d->setCurrentIndex(suggestionCount() - 1);
event->accept();
break;
default:
QQuickControl::keyPressEvent(event);
break;
}
}
}
void QQuickSearchField::classBegin()
{
Q_D(QQuickSearchField);
QQuickControl::classBegin();
QQmlContext *context = qmlContext(this);
if (context) {
QQmlEngine::setContextForObject(d->searchIndicator, context);
QQmlEngine::setContextForObject(d->clearIndicator, context);
}
}
void QQuickSearchField::componentComplete()
{
Q_D(QQuickSearchField);
QQuickIndicatorButtonPrivate::get(d->searchIndicator)->executeIndicator(true);
QQuickIndicatorButtonPrivate::get(d->clearIndicator)->executeIndicator(true);
QQuickControl::componentComplete();
if (d->popup)
d->executePopup(true);
if (d->delegateModel && d->ownModel)
static_cast<QQmlDelegateModel *>(d->delegateModel)->componentComplete();
if (suggestionCount() > 0) {
if (!d->hasCurrentIndex && d->currentIndex == -1)
setCurrentIndex(0);
}
}
void QQuickSearchField::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
{
Q_D(QQuickSearchField);
if (oldItem) {
oldItem->removeEventFilter(this);
if (QQuickTextInput *oldInput = qobject_cast<QQuickTextInput *>(oldItem)) {
QObjectPrivate::disconnect(oldInput, &QQuickTextInput::textChanged, d,
&QQuickSearchFieldPrivate::updateText);
}
}
if (newItem) {
newItem->installEventFilter(this);
if (QQuickTextInput *newInput = qobject_cast<QQuickTextInput *>(newItem)) {
QObjectPrivate::connect(newInput, &QQuickTextInput::textChanged, d,
&QQuickSearchFieldPrivate::updateText);
}
#if QT_CONFIG(cursor)
newItem->setCursor(Qt::IBeamCursor);
#endif
}
}
void QQuickSearchField::itemChange(ItemChange change, const ItemChangeData &data)
{
Q_D(QQuickSearchField);
QQuickControl::itemChange(change, data);
if (change == ItemVisibleHasChanged && !data.boolValue) {
d->hidePopup();
// TO-DO: CHECK When the popup isn't visible, there shouldn't be any current item
d->setCurrentIndex(-1);
}
}
QT_END_NAMESPACE
#include "moc_qquicksearchfield_p.cpp"
|