13#include <QtCore/qregularexpression.h>
14#include <QtCore/qabstractitemmodel.h>
15#include <QtCore/qglobal.h>
16#include <QtGui/qinputmethod.h>
17#include <QtGui/qguiapplication.h>
18#include <QtGui/private/qguiapplication_p.h>
19#include <QtGui/qpa/qplatformtheme.h>
20#include <QtQml/qjsvalue.h>
21#include <QtQml/qqmlcontext.h>
22#include <QtQml/qqmlcomponent.h>
23#include <QtQml/private/qlazilyallocated_p.h>
24#include <private/qqmldelegatemodel_p.h>
25#include <QtQuick/private/qquickaccessibleattached_p.h>
26#include <QtQuick/private/qquickevents_p_p.h>
27#include <QtQuick/private/qquicktextinput_p.h>
28#include <QtQuick/private/qquicktextinput_p_p.h>
29#if QT_CONFIG(quick_itemview)
30#include <QtQuick/private/qquickitemview_p.h>
35Q_STATIC_LOGGING_CATEGORY(lcCalculateWidestTextWidth,
"qt.quick.controls.combobox.calculatewidesttextwidth")
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
115
116
117
118
119
120
121
122
123
124
125
126
129
130
131
132
133
134
135
136
137
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
178 QQuickComboBox *combo =
nullptr;
193 if (role == QLatin1String(
"modelData")) {
194 const QVariant model = combo->model();
195 if (model.metaType() == QMetaType::fromType<QVariantList>()) {
196 const QVariant object = model.toList().value(index);
197 if (object.metaType() == QMetaType::fromType<QVariantMap>()) {
198 const QVariantMap data = object.toMap();
199 if (data.size() == 1)
205 return QQmlDelegateModel::variantValue(index, role);
211 Q_DECLARE_PUBLIC(QQuickComboBox)
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
273 void onDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QList<
int> &roles);
288 int match(
int start,
const QString &text, Qt::MatchFlags flags)
const;
293 bool handleMove(
const QPointF &point, ulong timestamp)
override;
353#if QT_CONFIG(validator)
362 return popup && popup->isVisible();
370 if (popup && !popup->isVisible())
381 emit q->activated(highlightedIndex);
383 if (popup && popup->isVisible())
389 if (!popup || !popup->isVisible())
398 if (isPopupVisible())
399 QGuiApplication::inputMethod()->reset();
401#if QT_CONFIG(quick_itemview)
402 QQuickItemView *itemView = popup->findChild<QQuickItemView *>();
404 itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
409#if QT_CONFIG(quick_itemview)
411 itemView->positionViewAtIndex(highlightedIndex, QQuickItemView::Beginning);
415 q->setDown(pressed || isPopupVisible());
424 emit q->popupChanged();
443 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender());
444 if (!button || !button->isHovered() || !button->isEnabled() || QQuickAbstractButtonPrivate::get(button)->touchId != -1)
452#if QT_CONFIG(quick_itemview)
453 if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
454 itemView->positionViewAtIndex(index, QQuickItemView::Contain);
462 QQuickItem *item = qobject_cast<QQuickItem *>(object);
463 if (item && !item->parentItem()) {
465 item->setParentItem(popup->contentItem());
467 item->setParentItem(q);
468 QQuickItemPrivate::get(item)->setCulled(
true);
471 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(object);
473 button->setFocusPolicy(Qt::NoFocus);
478 if (index == currentIndex && !q->isEditable())
484 if (componentComplete && (!extra.isAllocated() || !extra->accepting)) {
487 if (implicitContentWidthPolicy == QQuickComboBox::WidestText)
488 updateImplicitContentSize();
495 if (q->count() == 0) {
496 if (currentElementCriteria == CurrentElementCriteria::CurrentValue)
501 emit q->countChanged();
506 return textRole.isEmpty() ? QStringLiteral(
"modelData") : textRole;
512 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
516 const QString text = input->text();
518 if (extra.isAllocated() && extra->allowComplete && !text.isEmpty()) {
519 const QString completed = tryComplete(text);
520 if (completed.size() > text.size()) {
521 input->setText(completed);
523 input->select(completed.size(), text.size());
527 q->setEditText(text);
533 const int index = q->indexOfValue(currentValue);
538 emit q->currentIndexChanged();
544 const QString text = q->textAt(currentIndex);
545 if (currentText != text) {
548 q->maybeSetAccessibleName(text);
549 emit q->currentTextChanged();
551 if (!hasDisplayText && displayText != text) {
553 emit q->displayTextChanged();
555 if (!extra.isAllocated() || !extra->accepting)
556 q->setEditText(currentText);
562 const QVariant value = q->valueAt(currentIndex);
563 if (currentValue == value)
566 currentValue = value;
567 emit q->currentValueChanged();
572 switch (currentElementCriteria) {
573 case CurrentElementCriteria::None:
575 case CurrentElementCriteria::CurrentIndex:
579 case CurrentElementCriteria::CurrentValue:
593 const QQuickTextInput *textInputContentItem = qobject_cast<QQuickTextInput *>(contentItem);
595 if (!textInputContentItem)
598 const bool newValue = textInputContentItem->hasAcceptableInput();
602 emit q->acceptableInputChanged();
621 int idx = q->find(extra.value().editText, Qt::MatchFixedString);
626 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
628 const auto text = input->text();
629 input->select(text.size(), text.size());
633 extra.value().accepting =
true;
639 idx = q->find(extra.value().editText, Qt::MatchFixedString);
642 extra.value().accepting =
false;
650 const int itemCount = q->count();
651 for (
int idx = 0; idx < itemCount; ++idx) {
652 const QString text = q->textAt(idx);
653 if (!text.startsWith(input, Qt::CaseInsensitive))
657 if (match.isEmpty() || text.size() < match.size())
664 return input + match.mid(input.size());
674 emit q->currentIndexChanged();
676 if (componentComplete)
687 emit q->currentIndexChanged();
693 emit q->activated(index);
699 if (extra.isAllocated())
700 extra->allowComplete =
false;
702 if (highlightedIndex < q->count() - 1)
705 if (currentIndex < q->count() - 1)
708 if (extra.isAllocated())
709 extra->allowComplete =
true;
714 if (extra.isAllocated())
715 extra->allowComplete =
false;
723 if (extra.isAllocated())
724 extra->allowComplete =
true;
739 emit q->highlightedIndexChanged();
742 emit q->highlighted(index);
748 const int index = match(startIndex + 1, text, Qt::MatchStartsWith | Qt::MatchWrap);
759 Q_Q(
const QQuickComboBox);
760 uint matchType = flags & 0x0F;
761 bool wrap = flags & Qt::MatchWrap;
762 Qt::CaseSensitivity cs = flags & Qt::MatchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
763 QRegularExpression::PatternOptions options = flags & Qt::MatchCaseSensitive ? QRegularExpression::NoPatternOption
764 : QRegularExpression::CaseInsensitiveOption;
769 for (
int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
770 for (
int idx = from; idx < to; ++idx) {
771 QString t = q->textAt(idx);
773 case Qt::MatchExactly:
777 case Qt::MatchRegularExpression: {
778 QRegularExpression rx(QRegularExpression::anchoredPattern(text), options);
779 if (rx.match(t).hasMatch())
783 case Qt::MatchWildcard: {
784 QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(text),
786 if (rx.match(t).hasMatch())
790 case Qt::MatchStartsWith:
791 if (t.startsWith(text, cs))
794 case Qt::MatchEndsWith:
795 if (t.endsWith(text, cs))
798 case Qt::MatchFixedString:
799 if (t.compare(text, cs) == 0)
802 case Qt::MatchContains:
804 if (t.contains(text, cs))
828 delegateModel = model.value<QQmlInstanceModel *>();
830 if (!delegateModel && model.isValid()) {
831 QQmlDelegateModel *dataModel =
new QQuickComboBoxDelegateModel(q);
832 dataModel->setModel(model);
834 if (q->isComponentComplete())
835 dataModel->componentComplete();
847 emit q->delegateModelChanged();
856 QQuickControlPrivate::handlePress(point, timestamp);
864 QQuickControlPrivate::handleMove(point, timestamp);
865 q->setPressed(q->contains(point));
872 QQuickControlPrivate::handleRelease(point, timestamp);
874 q->setPressed(
false);
883 QQuickControlPrivate::handleUngrab();
884 q->setPressed(
false);
890 quickCancelDeferred(q, indicatorName());
896 if (indicator.wasExecuted())
899 if (!indicator || complete)
900 quickBeginDeferred(q, indicatorName(), indicator);
902 quickCompleteDeferred(q, indicatorName(), indicator);
905static inline QString
popupName() {
return QStringLiteral(
"popup"); }
910 quickCancelDeferred(q, popupName());
916 if (popup.wasExecuted())
919 if (!popup || complete)
920 quickBeginDeferred(q, popupName(), popup);
922 quickCompleteDeferred(q, popupName(), popup);
928 QQuickControlPrivate::itemImplicitWidthChanged(item);
929 if (item == indicator)
930 emit q->implicitIndicatorWidthChanged();
936 if (!force && hints == q->inputMethodHints())
939 extra.value().inputMethodHints = hints;
940 emit q->inputMethodHintsChanged();
946 QQuickControlPrivate::itemImplicitHeightChanged(item);
947 if (item == indicator)
948 emit q->implicitIndicatorHeightChanged();
954 QQuickControlPrivate::itemDestroyed(item);
955 if (item == indicator) {
957 emit q->indicatorChanged();
963 if (componentComplete) {
964 switch (implicitContentWidthPolicy) {
965 case QQuickComboBox::WidestText:
966 return calculateWidestTextWidth();
967 case QQuickComboBox::WidestTextWhenCompleted:
968 if (!hasCalculatedWidestText)
969 return calculateWidestTextWidth();
976 return QQuickControlPrivate::getContentWidth();
981 Q_Q(
const QQuickComboBox);
982 if (!componentComplete)
985 const int count = q->count();
989 auto textInput = qobject_cast<QQuickTextInput*>(contentItem);
993 qCDebug(lcCalculateWidestTextWidth) <<
"calculating widest text from" << count <<
"items...";
997 const QString textRole = effectiveTextRole();
998 auto textInputPrivate = QQuickTextInputPrivate::get(textInput);
1000 for (
int i = 0; i < count; ++i) {
1001 const QString text = delegateModel->stringValue(i, textRole);
1002 const qreal textImplicitWidth = textInputPrivate->calculateImplicitWidthForText(text);
1003 widest = qMax(widest, textImplicitWidth);
1006 qCDebug(lcCalculateWidestTextWidth) <<
"... widest text is" << widest;
1011
1012
1013
1014
1015
1018 if (!componentComplete)
1021 if (implicitContentWidthPolicy == QQuickComboBox::ContentItemImplicitWidth
1022 || (implicitContentWidthPolicy == QQuickComboBox::WidestTextWhenCompleted && hasCalculatedWidestText))
1025 updateImplicitContentWidth();
1034 qCDebug(lcItemManagement) <<
"hiding old popup" << popup;
1036 popup->setVisible(
false);
1037 popup->setParentItem(
nullptr);
1038#if QT_CONFIG(accessibility)
1040 QQuickAccessibleAttached *accessible = accessibleAttached(popup);
1042 accessible->setIgnored(
true);
1046QQuickComboBox::QQuickComboBox(QQuickItem *parent)
1047 : QQuickControl(*(
new QQuickComboBoxPrivate), parent)
1049 setFocusPolicy(Qt::StrongFocus);
1050 setFlag(QQuickItem::ItemIsFocusScope);
1051 setAcceptedMouseButtons(Qt::LeftButton);
1052#if QT_CONFIG(cursor)
1053 setCursor(Qt::ArrowCursor);
1055 Q_D(QQuickComboBox);
1056 d->setInputMethodHints(Qt::ImhNoPredictiveText,
true);
1057 d->setSizePolicy(QLayoutPolicy::Preferred, QLayoutPolicy::Fixed);
1060QQuickComboBox::~QQuickComboBox()
1062 Q_D(QQuickComboBox);
1063 d->removeImplicitSizeListener(d->indicator);
1067 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1068 QQuickComboBoxPrivate::hideOldPopup(d->popup);
1074
1075
1076
1077
1078
1079int QQuickComboBox::count()
const
1081 Q_D(
const QQuickComboBox);
1082 return d->delegateModel ? d->delegateModel->count() : 0;
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103QVariant QQuickComboBox::model()
const
1105 Q_D(
const QQuickComboBox);
1106 if (!d->qobjectModelGuard.has_value()) {
1112 return !d->qobjectModelGuard->isNull() ? d->model : QVariant();
1115void QQuickComboBox::setModel(
const QVariant& m)
1117 Q_D(QQuickComboBox);
1118 QVariant newModel = m;
1119 if (newModel.userType() == qMetaTypeId<QJSValue>())
1120 newModel = newModel.value<QJSValue>().toVariant();
1122 if (d->model == newModel)
1125 if (d->qobjectModelGuard.has_value() && !d->qobjectModelGuard->isNull()) {
1126 if (QAbstractItemModel* aim = qvariant_cast<QAbstractItemModel *>(d->model)) {
1127 QObjectPrivate::disconnect(aim, &QAbstractItemModel::dataChanged,
1128 d, &QQuickComboBoxPrivate::onDataChanged);
1131 if (QObject* newModelAsQObject = qvariant_cast<QObject *>(newModel)) {
1135 d->qobjectModelGuard = QPointer<QObject>(newModelAsQObject);
1137 if (QAbstractItemModel* aim = qvariant_cast<QAbstractItemModel *>(newModel)) {
1138 QObjectPrivate::connect(aim, &QAbstractItemModel::dataChanged,
1139 d, &QQuickComboBoxPrivate::onDataChanged);
1143 d->qobjectModelGuard.reset();
1146 d->model = newModel;
1147 d->createDelegateModel();
1148 emit countChanged();
1149 if (isComponentComplete()) {
1150 if (d->currentElementCriteria != QQuickComboBoxPrivate::CurrentElementCriteria::CurrentValue)
1151 d->setCurrentIndex(count() > 0 ? 0 : -1);
1152 d->updateCurrentElements();
1154 emit modelChanged();
1156 d->maybeUpdateImplicitContentWidth();
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170QQmlInstanceModel *QQuickComboBox::delegateModel()
const
1172 Q_D(
const QQuickComboBox);
1173 return d->delegateModel;
1178
1179
1180
1181
1182
1183
1184
1185
1186bool QQuickComboBox::isPressed()
const
1188 Q_D(
const QQuickComboBox);
1192void QQuickComboBox::setPressed(
bool pressed)
1194 Q_D(QQuickComboBox);
1195 if (d->pressed == pressed)
1198 d->pressed = pressed;
1199 emit pressedChanged();
1202 setDown(d->pressed || d->isPopupVisible());
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219int QQuickComboBox::highlightedIndex()
const
1221 Q_D(
const QQuickComboBox);
1222 return d->highlightedIndex;
1226
1227
1228
1229
1230
1231
1232
1233
1234int QQuickComboBox::currentIndex()
const
1236 Q_D(
const QQuickComboBox);
1237 return d->currentIndex;
1240void QQuickComboBox::setCurrentIndex(
int index)
1242 Q_D(QQuickComboBox);
1243 d->currentElementCriteria = QQuickComboBoxPrivate::CurrentElementCriteria::CurrentIndex;
1244 d->setCurrentIndex(index);
1248
1249
1250
1251
1252
1253
1254
1255QString QQuickComboBox::currentText()
const
1257 Q_D(
const QQuickComboBox);
1258 return d->currentText;
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280QString QQuickComboBox::displayText()
const
1282 Q_D(
const QQuickComboBox);
1283 return d->displayText;
1286void QQuickComboBox::setDisplayText(
const QString &text)
1288 Q_D(QQuickComboBox);
1289 d->hasDisplayText =
true;
1290 if (d->displayText == text)
1293 d->displayText = text;
1294 maybeSetAccessibleName(text);
1295 emit displayTextChanged();
1298void QQuickComboBox::resetDisplayText()
1300 Q_D(QQuickComboBox);
1301 if (!d->hasDisplayText)
1304 d->hasDisplayText =
false;
1305 d->updateCurrentText();
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319QString QQuickComboBox::textRole()
const
1321 Q_D(
const QQuickComboBox);
1325void QQuickComboBox::setTextRole(
const QString &role)
1327 Q_D(QQuickComboBox);
1328 if (d->textRole == role)
1332 if (isComponentComplete())
1333 d->updateCurrentText();
1334 emit textRoleChanged();
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348QString QQuickComboBox::valueRole()
const
1350 Q_D(
const QQuickComboBox);
1351 return d->valueRole;
1354void QQuickComboBox::setValueRole(
const QString &role)
1356 Q_D(QQuickComboBox);
1357 if (d->valueRole == role)
1360 d->valueRole = role;
1361 if (isComponentComplete())
1362 d->updateCurrentElements();
1363 emit valueRoleChanged();
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391QQmlComponent *QQuickComboBox::delegate()
const
1393 Q_D(
const QQuickComboBox);
1397void QQuickComboBox::setDelegate(QQmlComponent* delegate)
1399 Q_D(QQuickComboBox);
1400 if (d->delegate == delegate)
1403 d->delegate = delegate;
1404 QQmlDelegateModel *delegateModel = qobject_cast<QQmlDelegateModel*>(d->delegateModel);
1406 delegateModel->setDelegate(d->delegate);
1407 emit delegateChanged();
1411
1412
1413
1414
1415
1416
1417QQuickItem *QQuickComboBox::indicator()
const
1419 QQuickComboBoxPrivate *d =
const_cast<QQuickComboBoxPrivate *>(d_func());
1421 d->executeIndicator();
1422 return d->indicator;
1425void QQuickComboBox::setIndicator(QQuickItem *indicator)
1427 Q_D(QQuickComboBox);
1428 if (d->indicator == indicator)
1431 QQuickControlPrivate::warnIfCustomizationNotSupported(
this, indicator, QStringLiteral(
"indicator"));
1433 if (!d->indicator.isExecuting())
1434 d->cancelIndicator();
1436 const qreal oldImplicitIndicatorWidth = implicitIndicatorWidth();
1437 const qreal oldImplicitIndicatorHeight = implicitIndicatorHeight();
1439 d->removeImplicitSizeListener(d->indicator);
1440 QQuickControlPrivate::hideOldItem(d->indicator);
1441 d->indicator = indicator;
1443 if (!indicator->parentItem())
1444 indicator->setParentItem(
this);
1445 d->addImplicitSizeListener(indicator);
1448 if (!qFuzzyCompare(oldImplicitIndicatorWidth, implicitIndicatorWidth()))
1449 emit implicitIndicatorWidthChanged();
1450 if (!qFuzzyCompare(oldImplicitIndicatorHeight, implicitIndicatorHeight()))
1451 emit implicitIndicatorHeightChanged();
1452 if (!d->indicator.isExecuting())
1453 emit indicatorChanged();
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469QQuickPopup *QQuickComboBox::popup()
const
1471 QQuickComboBoxPrivate *d =
const_cast<QQuickComboBoxPrivate *>(d_func());
1473 d->executePopup(isComponentComplete());
1477void QQuickComboBox::setPopup(QQuickPopup *popup)
1479 Q_D(QQuickComboBox);
1480 if (d->popup == popup)
1483 if (!d->popup.isExecuting())
1487 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::destroyed, d, &QQuickComboBoxPrivate::popupDestroyed);
1488 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1489 QQuickComboBoxPrivate::hideOldPopup(d->popup);
1492#if QT_CONFIG(wayland)
1493 QQuickPopupPrivate::get(popup)->extendedWindowType = QNativeInterface::Private::QWaylandWindow::ComboBox;
1495 QQuickPopupPrivate::get(popup)->allowVerticalFlip =
true;
1496 popup->setClosePolicy(QQuickPopup::CloseOnEscape | QQuickPopup::CloseOnPressOutsideParent);
1497 QObjectPrivate::connect(popup, &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1500 QObjectPrivate::connect(popup, &QQuickPopup::destroyed, d, &QQuickComboBoxPrivate::popupDestroyed);
1502#if QT_CONFIG(quick_itemview)
1503 if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
1504 itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
1508 if (!d->popup.isExecuting())
1509 emit popupChanged();
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526bool QQuickComboBox::isFlat()
const
1528 Q_D(
const QQuickComboBox);
1532void QQuickComboBox::setFlat(
bool flat)
1534 Q_D(QQuickComboBox);
1535 if (d->flat == flat)
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554bool QQuickComboBox::isDown()
const
1556 Q_D(
const QQuickComboBox);
1560void QQuickComboBox::setDown(
bool down)
1562 Q_D(QQuickComboBox);
1565 if (d->down == down)
1572void QQuickComboBox::resetDown()
1574 Q_D(QQuickComboBox);
1578 setDown(d->pressed || d->isPopupVisible());
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592bool QQuickComboBox::isEditable()
const
1594 Q_D(
const QQuickComboBox);
1595 return d->extra.isAllocated() && d->extra->editable;
1598void QQuickComboBox::setEditable(
bool editable)
1600 Q_D(QQuickComboBox);
1601 if (editable == isEditable())
1604 if (d->contentItem) {
1606 d->contentItem->installEventFilter(
this);
1607 if (QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem)) {
1608 QObjectPrivate::connect(input, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
1609 QObjectPrivate::connect(input, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
1611#if QT_CONFIG(cursor)
1612 d->contentItem->setCursor(Qt::IBeamCursor);
1615 d->contentItem->removeEventFilter(
this);
1616 if (QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem)) {
1617 QObjectPrivate::disconnect(input, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
1618 QObjectPrivate::disconnect(input, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
1620#if QT_CONFIG(cursor)
1621 d->contentItem->unsetCursor();
1626 d->extra.value().editable = editable;
1627 setAccessibleProperty(
"editable", editable);
1628 emit editableChanged();
1632
1633
1634
1635
1636
1637
1638
1639QString QQuickComboBox::editText()
const
1641 Q_D(
const QQuickComboBox);
1642 return d->extra.isAllocated() ? d->extra->editText : QString();
1645void QQuickComboBox::setEditText(
const QString &text)
1647 Q_D(QQuickComboBox);
1648 if (text == editText())
1651 d->extra.value().editText = text;
1652 emit editTextChanged();
1655void QQuickComboBox::resetEditText()
1657 setEditText(QString());
1660#if QT_CONFIG(validator)
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690QValidator *QQuickComboBox::validator()
const
1692 Q_D(
const QQuickComboBox);
1693 return d->extra.isAllocated() ? d->extra->validator :
nullptr;
1696void QQuickComboBox::setValidator(QValidator *validator)
1698 Q_D(QQuickComboBox);
1699 if (validator == QQuickComboBox::validator())
1702 d->extra.value().validator = validator;
1703#if QT_CONFIG(validator)
1705 validator->setLocale(d->locale);
1707 emit validatorChanged();
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722Qt::InputMethodHints QQuickComboBox::inputMethodHints()
const
1724 Q_D(
const QQuickComboBox);
1725 return d->extra.isAllocated() ? d->extra->inputMethodHints : Qt::ImhNoPredictiveText;
1728void QQuickComboBox::setInputMethodHints(Qt::InputMethodHints hints)
1730 Q_D(QQuickComboBox);
1731 d->setInputMethodHints(hints);
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745bool QQuickComboBox::isInputMethodComposing()
const
1747 Q_D(
const QQuickComboBox);
1748 return d->contentItem && d->contentItem->property(
"inputMethodComposing").toBool();
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763bool QQuickComboBox::hasAcceptableInput()
const
1765 Q_D(
const QQuickComboBox);
1766 return d->m_acceptableInput;
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783qreal QQuickComboBox::implicitIndicatorWidth()
const
1785 Q_D(
const QQuickComboBox);
1788 return d->indicator->implicitWidth();
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805qreal QQuickComboBox::implicitIndicatorHeight()
const
1807 Q_D(
const QQuickComboBox);
1810 return d->indicator->implicitHeight();
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828QVariant QQuickComboBox::currentValue()
const
1830 Q_D(
const QQuickComboBox);
1831 return d->currentValue;
1834void QQuickComboBox::setCurrentValue(
const QVariant &value)
1836 Q_D(QQuickComboBox);
1837 d->currentElementCriteria = QQuickComboBoxPrivate::CurrentElementCriteria::CurrentValue;
1838 if (value == d->currentValue)
1841 d->currentValue = value;
1842 emit currentValueChanged();
1844 if (d->componentComplete)
1845 d->updateCurrentElements();
1849
1850
1851
1852
1853
1854
1855
1856
1857QVariant QQuickComboBox::valueAt(
int index)
const
1859 Q_D(
const QQuickComboBox);
1860 if (!d->isValidIndex(index))
1863 const QString effectiveValueRole = d->valueRole.isEmpty() ? QStringLiteral(
"modelData") : d->valueRole;
1864 return d->delegateModel->variantValue(index, effectiveValueRole);
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879int QQuickComboBox::indexOfValue(
const QVariant &value)
const
1881 for (
int i = 0; i < count(); ++i) {
1882 const QVariant ourValue = valueAt(i);
1883 if (value == ourValue)
1890
1891
1892
1893
1894
1895
1896
1897
1898bool QQuickComboBox::selectTextByMouse()
const
1900 Q_D(
const QQuickComboBox);
1901 return d->extra.isAllocated() ? d->extra->selectTextByMouse :
false;
1904void QQuickComboBox::setSelectTextByMouse(
bool canSelect)
1906 Q_D(QQuickComboBox);
1907 if (canSelect == selectTextByMouse())
1910 d->extra.value().selectTextByMouse = canSelect;
1911 emit selectTextByMouseChanged();
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968QQuickComboBox::ImplicitContentWidthPolicy QQuickComboBox::implicitContentWidthPolicy()
const
1970 Q_D(
const QQuickComboBox);
1971 return d->implicitContentWidthPolicy;
1974void QQuickComboBox::setImplicitContentWidthPolicy(QQuickComboBox::ImplicitContentWidthPolicy policy)
1976 Q_D(QQuickComboBox);
1977 if (policy == d->implicitContentWidthPolicy)
1980 d->implicitContentWidthPolicy = policy;
1981 d->maybeUpdateImplicitContentWidth();
1982 emit implicitContentWidthPolicyChanged();
1986
1987
1988
1989
1990
1991
1992
1993
1994bool QQuickComboBox::highlightOnHover()
const
1996 Q_D(
const QQuickComboBox);
1997 return d->highlightOnHover;
2000void QQuickComboBox::setHighlightOnHover(
bool value)
2002 Q_D(QQuickComboBox);
2003 if (value == highlightOnHover())
2006 d->highlightOnHover = value;
2007 emit highlightOnHoverChanged();
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021QString QQuickComboBox::textAt(
int index)
const
2023 Q_D(
const QQuickComboBox);
2024 if (!d->isValidIndex(index))
2027 return d->delegateModel->stringValue(index, d->effectiveTextRole());
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054int QQuickComboBox::find(
const QString &text, Qt::MatchFlags flags)
const
2056 Q_D(
const QQuickComboBox);
2057 return d->match(0, text, flags);
2061
2062
2063
2064
2065
2066
2067
2068void QQuickComboBox::incrementCurrentIndex()
2070 Q_D(QQuickComboBox);
2071 d->incrementCurrentIndex();
2075
2076
2077
2078
2079
2080
2081
2082void QQuickComboBox::decrementCurrentIndex()
2084 Q_D(QQuickComboBox);
2085 d->decrementCurrentIndex();
2089
2090
2091
2092
2093
2094
2095
2096void QQuickComboBox::selectAll()
2098 Q_D(QQuickComboBox);
2099 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem);
2105bool QQuickComboBox::eventFilter(QObject *object, QEvent *event)
2107 Q_D(QQuickComboBox);
2108 switch (event->type()) {
2109 case QEvent::MouseButtonRelease:
2110 if (d->isPopupVisible())
2111 d->hidePopup(
false);
2113 case QEvent::KeyPress: {
2114 QKeyEvent *ke =
static_cast<QKeyEvent *>(event);
2115 if (d->filterKeyEvent(ke,
false))
2118 if (d->extra.isAllocated())
2119 d->extra->allowComplete = ke->key() != Qt::Key_Backspace && ke->key() != Qt::Key_Delete;
2122 case QEvent::FocusOut: {
2123 const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
2124 const bool usingPopupWindows =
2125 d->popup ? QQuickPopupPrivate::get(d->popup)->usePopupWindow() :
false;
2126 if (qGuiApp->focusObject() !=
this && !(hasActiveFocus && !usingPopupWindows)) {
2130 d->hidePopup(
false);
2135 const int indexForEditText = find(d->extra.value().editText, Qt::MatchFixedString);
2136 if (indexForEditText > -1)
2137 d->setCurrentItemAtIndex(indexForEditText, Activate);
2142 case QEvent::InputMethod:
2143 if (d->extra.isAllocated())
2144 d->extra->allowComplete = !
static_cast<QInputMethodEvent*>(event)->commitString().isEmpty();
2150 return QQuickControl::eventFilter(object, event);
2153void QQuickComboBox::focusInEvent(QFocusEvent *event)
2155 Q_D(QQuickComboBox);
2156 QQuickControl::focusInEvent(event);
2160 if ((event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason ||
2161 event->reason() == Qt::ShortcutFocusReason) && d->contentItem && isEditable())
2162 d->contentItem->forceActiveFocus(event->reason());
2165void QQuickComboBox::focusOutEvent(QFocusEvent *event)
2167 Q_D(QQuickComboBox);
2168 QQuickControl::focusOutEvent(event);
2170 const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
2171 const bool usingPopupWindows = d->popup && QQuickPopupPrivate::get(d->popup)->usePopupWindow();
2172 if (qGuiApp->focusObject() != d->contentItem && !(hasActiveFocus && !usingPopupWindows)) {
2176 d->hidePopup(
false);
2182void QQuickComboBox::inputMethodEvent(QInputMethodEvent *event)
2184 Q_D(QQuickComboBox);
2185 QQuickControl::inputMethodEvent(event);
2186 if (!isEditable() && !event->commitString().isEmpty())
2187 d->keySearch(event->commitString());
2193void QQuickComboBox::keyPressEvent(QKeyEvent *event)
2195 Q_D(QQuickComboBox);
2196 QQuickControl::keyPressEvent(event);
2198 const auto key = event->key();
2199 if (!isEditable()) {
2200 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
2201 if (buttonPressKeys.contains(key)) {
2202 if (!event->isAutoRepeat())
2210 case Qt::Key_Escape:
2212 d->acceptedEscKeyPress = d->isPopupVisible();
2213 d->receivedEscKeyPress =
true;
2214 if (d->acceptedEscKeyPress) {
2215 d->hidePopup(
false);
2221 case Qt::Key_Return:
2222 if (d->isPopupVisible())
2227 d->keyNavigating =
true;
2228 d->decrementCurrentIndex();
2232 d->keyNavigating =
true;
2233 d->incrementCurrentIndex();
2237 d->keyNavigating =
true;
2238 if (d->isPopupVisible())
2239 d->setHighlightedIndex(0, Highlight);
2241 d->setCurrentItemAtIndex(0, Activate);
2245 d->keyNavigating =
true;
2246 if (d->isPopupVisible())
2247 d->setHighlightedIndex(count() - 1, Highlight);
2249 d->setCurrentItemAtIndex(count() - 1, Activate);
2253 if (!isEditable() && !event->text().isEmpty())
2254 d->keySearch(event->text());
2261void QQuickComboBox::keyReleaseEvent(QKeyEvent *event)
2263 Q_D(QQuickComboBox);
2264 QQuickControl::keyReleaseEvent(event);
2265 d->keyNavigating =
false;
2266 if (event->isAutoRepeat())
2269 const auto key = event->key();
2270 if (!isEditable()) {
2271 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
2272 if (buttonPressKeys.contains(key)) {
2273 if (!isEditable() && isPressed())
2274 d->togglePopup(
true);
2283 case Qt::Key_Return:
2284 if (!isEditable() || d->isPopupVisible())
2285 d->hidePopup(d->isPopupVisible());
2289 case Qt::Key_Escape:
2295 if (d->acceptedEscKeyPress || !d->receivedEscKeyPress)
2297 d->acceptedEscKeyPress =
false;
2298 d->receivedEscKeyPress =
false;
2305#if QT_CONFIG(wheelevent)
2306void QQuickComboBox::wheelEvent(QWheelEvent *event)
2308 Q_D(QQuickComboBox);
2309 QQuickControl::wheelEvent(event);
2310 if (d->wheelEnabled && !d->isPopupVisible()) {
2311 if (event->angleDelta().y() > 0)
2312 d->decrementCurrentIndex();
2314 d->incrementCurrentIndex();
2319bool QQuickComboBox::event(QEvent *e)
2321 Q_D(QQuickComboBox);
2322 if (e->type() == QEvent::LanguageChange)
2323 d->updateCurrentElements();
2324 return QQuickControl::event(e);
2327void QQuickComboBox::componentComplete()
2329 Q_D(QQuickComboBox);
2330 d->executeIndicator(
true);
2331 QQuickControl::componentComplete();
2333 d->executePopup(
true);
2335 if (d->delegateModel && d->ownModel)
2336 static_cast<QQmlDelegateModel *>(d->delegateModel)->componentComplete();
2339 if (d->currentElementCriteria == QQuickComboBoxPrivate::CurrentElementCriteria::None && d->currentIndex == -1)
2340 d->setCurrentIndex(0);
2342 d->updateCurrentElements();
2346 if (!d->hasCalculatedWidestText)
2347 d->maybeUpdateImplicitContentWidth();
2351void QQuickComboBox::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &value)
2353 Q_D(QQuickComboBox);
2354 QQuickControl::itemChange(change, value);
2355 if (change == ItemVisibleHasChanged && !value.boolValue) {
2356 d->hidePopup(
false);
2361void QQuickComboBox::fontChange(
const QFont &newFont,
const QFont &oldFont)
2363 Q_D(QQuickComboBox);
2364 QQuickControl::fontChange(newFont, oldFont);
2365 d->maybeUpdateImplicitContentWidth();
2368void QQuickComboBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
2370 Q_D(QQuickComboBox);
2372 oldItem->removeEventFilter(
this);
2373 if (QQuickTextInput *oldInput = qobject_cast<QQuickTextInput *>(oldItem)) {
2374 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
2375 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
2376 disconnect(oldInput, &QQuickTextInput::inputMethodComposingChanged,
this, &QQuickComboBox::inputMethodComposingChanged);
2377 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::acceptableInputChanged, d, &QQuickComboBoxPrivate::updateAcceptableInput);
2380 if (newItem && isEditable()) {
2381 newItem->installEventFilter(
this);
2382 if (QQuickTextInput *newInput = qobject_cast<QQuickTextInput *>(newItem)) {
2383 QObjectPrivate::connect(newInput, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
2384 QObjectPrivate::connect(newInput, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
2385 connect(newInput, &QQuickTextInput::inputMethodComposingChanged,
this, &QQuickComboBox::inputMethodComposingChanged);
2386 QObjectPrivate::connect(newInput, &QQuickTextInput::acceptableInputChanged, d, &QQuickComboBoxPrivate::updateAcceptableInput);
2388#if QT_CONFIG(cursor)
2389 newItem->setCursor(Qt::IBeamCursor);
2393 d->updateAcceptableInput();
2396void QQuickComboBox::localeChange(
const QLocale &newLocale,
const QLocale &oldLocale)
2398 QQuickControl::localeChange(newLocale, oldLocale);
2399#if QT_CONFIG(validator)
2400 if (QValidator *v = validator())
2401 v->setLocale(newLocale);
2405QFont QQuickComboBox::defaultFont()
const
2407 return QQuickTheme::font(QQuickTheme::ComboBox);
2410#if QT_CONFIG(accessibility)
2411QAccessible::Role QQuickComboBox::accessibleRole()
const
2413 return QAccessible::ComboBox;
2416void QQuickComboBox::accessibilityActiveChanged(
bool active)
2418 Q_D(QQuickComboBox);
2419 QQuickControl::accessibilityActiveChanged(active);
2422 maybeSetAccessibleName(d->hasDisplayText ? d->displayText : d->currentText);
2423 setAccessibleProperty(
"editable", isEditable());
2430#include "moc_qquickcombobox_p.cpp"
QQuickComboBoxDelegateModel(QQuickComboBox *combo)
QVariant variantValue(int index, const QString &role) override
void itemDestroyed(QQuickItem *item) override
bool handleRelease(const QPointF &point, ulong timestamp) override
CurrentElementCriteria currentElementCriteria
QQuickDeferredPointer< QQuickPopup > popup
qreal calculateWidestTextWidth() const
static void hideOldPopup(QQuickPopup *popup)
void setInputMethodHints(Qt::InputMethodHints hints, bool force=false)
void maybeUpdateImplicitContentWidth()
void setHighlightedIndex(int index, Highlighting highlight)
void createdItem(int index, QObject *object)
void updateHighlightedIndex()
QString effectiveTextRole() const
virtual qreal getContentWidth() const override
void keySearch(const QString &text)
void handleUngrab() override
bool hasCalculatedWidestText
void updateCurrentValue()
int match(int start, const QString &text, Qt::MatchFlags flags) const
bool isPopupVisible() const
void updateCurrentElements()
QPalette defaultPalette() const override
void createDelegateModel()
bool handleMove(const QPointF &point, ulong timestamp) override
QQmlInstanceModel * delegateModel
void itemImplicitHeightChanged(QQuickItem *item) override
void togglePopup(bool accept)
void executePopup(bool complete=false)
void itemImplicitWidthChanged(QQuickItem *item) override
void popupVisibleChanged()
void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
void executeIndicator(bool complete=false)
QQuickDeferredPointer< QQuickItem > indicator
void hidePopup(bool accept)
void updateCurrentIndex()
void setCurrentIndex(int index)
QLazilyAllocated< ExtraData > extra
std::optional< QPointer< QObject > > qobjectModelGuard
void updateAcceptableInput()
bool handlePress(const QPointF &point, ulong timestamp) override
void decrementCurrentIndex()
void setCurrentItemAtIndex(int, Activation activate)
bool isValidIndex(int index) const
QString tryComplete(const QString &inputText)
void incrementCurrentIndex()
Combined button and popup list for selecting options.
static QString popupName()