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;
352#if QT_CONFIG(validator)
361 return popup && popup->isVisible();
369 if (popup && !popup->isVisible())
380 emit q->activated(highlightedIndex);
382 if (popup && popup->isVisible())
388 if (!popup || !popup->isVisible())
397 if (isPopupVisible())
398 QGuiApplication::inputMethod()->reset();
400#if QT_CONFIG(quick_itemview)
401 QQuickItemView *itemView = popup->findChild<QQuickItemView *>();
403 itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
408#if QT_CONFIG(quick_itemview)
410 itemView->positionViewAtIndex(highlightedIndex, QQuickItemView::Beginning);
414 q->setDown(pressed || isPopupVisible());
423 emit q->popupChanged();
442 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender());
443 if (!button || !button->isHovered() || !button->isEnabled() || QQuickAbstractButtonPrivate::get(button)->touchId != -1)
450#if QT_CONFIG(quick_itemview)
451 if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
452 itemView->positionViewAtIndex(index, QQuickItemView::Contain);
460 QQuickItem *item = qobject_cast<QQuickItem *>(object);
461 if (item && !item->parentItem()) {
463 item->setParentItem(popup->contentItem());
465 item->setParentItem(q);
466 QQuickItemPrivate::get(item)->setCulled(
true);
469 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(object);
471 button->setFocusPolicy(Qt::NoFocus);
476 if (index == currentIndex && !q->isEditable())
482 if (componentComplete && (!extra.isAllocated() || !extra->accepting)) {
485 if (implicitContentWidthPolicy == QQuickComboBox::WidestText)
486 updateImplicitContentSize();
493 if (q->count() == 0) {
494 if (currentElementCriteria == CurrentElementCriteria::CurrentValue)
499 emit q->countChanged();
504 return textRole.isEmpty() ? QStringLiteral(
"modelData") : textRole;
510 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
514 const QString text = input->text();
516 if (extra.isAllocated() && extra->allowComplete && !text.isEmpty()) {
517 const QString completed = tryComplete(text);
518 if (completed.size() > text.size()) {
519 input->setText(completed);
521 input->select(completed.size(), text.size());
525 q->setEditText(text);
531 const int index = q->indexOfValue(currentValue);
536 emit q->currentIndexChanged();
542 const QString text = q->textAt(currentIndex);
543 if (currentText != text) {
546 q->maybeSetAccessibleName(text);
547 emit q->currentTextChanged();
549 if (!hasDisplayText && displayText != text) {
551 emit q->displayTextChanged();
553 if (!extra.isAllocated() || !extra->accepting)
554 q->setEditText(currentText);
560 const QVariant value = q->valueAt(currentIndex);
561 if (currentValue == value)
564 currentValue = value;
565 emit q->currentValueChanged();
570 switch (currentElementCriteria) {
571 case CurrentElementCriteria::None:
573 case CurrentElementCriteria::CurrentIndex:
577 case CurrentElementCriteria::CurrentValue:
591 const QQuickTextInput *textInputContentItem = qobject_cast<QQuickTextInput *>(contentItem);
593 if (!textInputContentItem)
596 const bool newValue = textInputContentItem->hasAcceptableInput();
600 emit q->acceptableInputChanged();
619 int idx = q->find(extra.value().editText, Qt::MatchFixedString);
624 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
626 const auto text = input->text();
627 input->select(text.size(), text.size());
631 extra.value().accepting =
true;
637 idx = q->find(extra.value().editText, Qt::MatchFixedString);
640 extra.value().accepting =
false;
648 const int itemCount = q->count();
649 for (
int idx = 0; idx < itemCount; ++idx) {
650 const QString text = q->textAt(idx);
651 if (!text.startsWith(input, Qt::CaseInsensitive))
655 if (match.isEmpty() || text.size() < match.size())
662 return input + match.mid(input.size());
672 emit q->currentIndexChanged();
674 if (componentComplete)
685 emit q->currentIndexChanged();
691 emit q->activated(index);
697 if (extra.isAllocated())
698 extra->allowComplete =
false;
700 if (highlightedIndex < q->count() - 1)
703 if (currentIndex < q->count() - 1)
706 if (extra.isAllocated())
707 extra->allowComplete =
true;
712 if (extra.isAllocated())
713 extra->allowComplete =
false;
721 if (extra.isAllocated())
722 extra->allowComplete =
true;
737 emit q->highlightedIndexChanged();
740 emit q->highlighted(index);
746 const int index = match(startIndex + 1, text, Qt::MatchStartsWith | Qt::MatchWrap);
757 Q_Q(
const QQuickComboBox);
758 uint matchType = flags & 0x0F;
759 bool wrap = flags & Qt::MatchWrap;
760 Qt::CaseSensitivity cs = flags & Qt::MatchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
761 QRegularExpression::PatternOptions options = flags & Qt::MatchCaseSensitive ? QRegularExpression::NoPatternOption
762 : QRegularExpression::CaseInsensitiveOption;
767 for (
int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
768 for (
int idx = from; idx < to; ++idx) {
769 QString t = q->textAt(idx);
771 case Qt::MatchExactly:
775 case Qt::MatchRegularExpression: {
776 QRegularExpression rx(QRegularExpression::anchoredPattern(text), options);
777 if (rx.match(t).hasMatch())
781 case Qt::MatchWildcard: {
782 QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(text),
784 if (rx.match(t).hasMatch())
788 case Qt::MatchStartsWith:
789 if (t.startsWith(text, cs))
792 case Qt::MatchEndsWith:
793 if (t.endsWith(text, cs))
796 case Qt::MatchFixedString:
797 if (t.compare(text, cs) == 0)
800 case Qt::MatchContains:
802 if (t.contains(text, cs))
826 delegateModel = model.value<QQmlInstanceModel *>();
828 if (!delegateModel && model.isValid()) {
829 QQmlDelegateModel *dataModel =
new QQuickComboBoxDelegateModel(q);
830 dataModel->setModel(model);
832 if (q->isComponentComplete())
833 dataModel->componentComplete();
845 emit q->delegateModelChanged();
854 QQuickControlPrivate::handlePress(point, timestamp);
862 QQuickControlPrivate::handleMove(point, timestamp);
863 q->setPressed(q->contains(point));
870 QQuickControlPrivate::handleRelease(point, timestamp);
872 q->setPressed(
false);
881 QQuickControlPrivate::handleUngrab();
882 q->setPressed(
false);
888 quickCancelDeferred(q, indicatorName());
894 if (indicator.wasExecuted())
897 if (!indicator || complete)
898 quickBeginDeferred(q, indicatorName(), indicator);
900 quickCompleteDeferred(q, indicatorName(), indicator);
903static inline QString
popupName() {
return QStringLiteral(
"popup"); }
908 quickCancelDeferred(q, popupName());
914 if (popup.wasExecuted())
917 if (!popup || complete)
918 quickBeginDeferred(q, popupName(), popup);
920 quickCompleteDeferred(q, popupName(), popup);
926 QQuickControlPrivate::itemImplicitWidthChanged(item);
927 if (item == indicator)
928 emit q->implicitIndicatorWidthChanged();
934 if (!force && hints == q->inputMethodHints())
937 extra.value().inputMethodHints = hints;
938 emit q->inputMethodHintsChanged();
944 QQuickControlPrivate::itemImplicitHeightChanged(item);
945 if (item == indicator)
946 emit q->implicitIndicatorHeightChanged();
952 QQuickControlPrivate::itemDestroyed(item);
953 if (item == indicator) {
955 emit q->indicatorChanged();
961 if (componentComplete) {
962 switch (implicitContentWidthPolicy) {
963 case QQuickComboBox::WidestText:
964 return calculateWidestTextWidth();
965 case QQuickComboBox::WidestTextWhenCompleted:
966 if (!hasCalculatedWidestText)
967 return calculateWidestTextWidth();
974 return QQuickControlPrivate::getContentWidth();
979 Q_Q(
const QQuickComboBox);
980 if (!componentComplete)
983 const int count = q->count();
987 auto textInput = qobject_cast<QQuickTextInput*>(contentItem);
991 qCDebug(lcCalculateWidestTextWidth) <<
"calculating widest text from" << count <<
"items...";
995 const QString textRole = effectiveTextRole();
996 auto textInputPrivate = QQuickTextInputPrivate::get(textInput);
998 for (
int i = 0; i < count; ++i) {
999 const QString text = delegateModel->stringValue(i, textRole);
1000 const qreal textImplicitWidth = textInputPrivate->calculateImplicitWidthForText(text);
1001 widest = qMax(widest, textImplicitWidth);
1004 qCDebug(lcCalculateWidestTextWidth) <<
"... widest text is" << widest;
1009
1010
1011
1012
1013
1016 if (!componentComplete)
1019 if (implicitContentWidthPolicy == QQuickComboBox::ContentItemImplicitWidth
1020 || (implicitContentWidthPolicy == QQuickComboBox::WidestTextWhenCompleted && hasCalculatedWidestText))
1023 updateImplicitContentWidth();
1032 qCDebug(lcItemManagement) <<
"hiding old popup" << popup;
1034 popup->setVisible(
false);
1035 popup->setParentItem(
nullptr);
1036#if QT_CONFIG(accessibility)
1038 QQuickAccessibleAttached *accessible = accessibleAttached(popup);
1040 accessible->setIgnored(
true);
1044QQuickComboBox::QQuickComboBox(QQuickItem *parent)
1045 : QQuickControl(*(
new QQuickComboBoxPrivate), parent)
1047 setFocusPolicy(Qt::StrongFocus);
1048 setFlag(QQuickItem::ItemIsFocusScope);
1049 setAcceptedMouseButtons(Qt::LeftButton);
1050#if QT_CONFIG(cursor)
1051 setCursor(Qt::ArrowCursor);
1053 Q_D(QQuickComboBox);
1054 d->setInputMethodHints(Qt::ImhNoPredictiveText,
true);
1055 d->setSizePolicy(QLayoutPolicy::Preferred, QLayoutPolicy::Fixed);
1058QQuickComboBox::~QQuickComboBox()
1060 Q_D(QQuickComboBox);
1061 d->removeImplicitSizeListener(d->indicator);
1065 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1066 QQuickComboBoxPrivate::hideOldPopup(d->popup);
1072
1073
1074
1075
1076
1077int QQuickComboBox::count()
const
1079 Q_D(
const QQuickComboBox);
1080 return d->delegateModel ? d->delegateModel->count() : 0;
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101QVariant QQuickComboBox::model()
const
1103 Q_D(
const QQuickComboBox);
1104 if (!d->qobjectModelGuard.has_value()) {
1110 return !d->qobjectModelGuard->isNull() ? d->model : QVariant();
1113void QQuickComboBox::setModel(
const QVariant& m)
1115 Q_D(QQuickComboBox);
1116 QVariant newModel = m;
1117 if (newModel.userType() == qMetaTypeId<QJSValue>())
1118 newModel = newModel.value<QJSValue>().toVariant();
1120 if (d->model == newModel)
1123 if (d->qobjectModelGuard.has_value() && !d->qobjectModelGuard->isNull()) {
1124 if (QAbstractItemModel* aim = qvariant_cast<QAbstractItemModel *>(d->model)) {
1125 QObjectPrivate::disconnect(aim, &QAbstractItemModel::dataChanged,
1126 d, &QQuickComboBoxPrivate::onDataChanged);
1129 if (QObject* newModelAsQObject = qvariant_cast<QObject *>(newModel)) {
1133 d->qobjectModelGuard = QPointer<QObject>(newModelAsQObject);
1135 if (QAbstractItemModel* aim = qvariant_cast<QAbstractItemModel *>(newModel)) {
1136 QObjectPrivate::connect(aim, &QAbstractItemModel::dataChanged,
1137 d, &QQuickComboBoxPrivate::onDataChanged);
1141 d->qobjectModelGuard.reset();
1144 d->model = newModel;
1145 d->createDelegateModel();
1146 emit countChanged();
1147 if (isComponentComplete()) {
1148 if (d->currentElementCriteria != QQuickComboBoxPrivate::CurrentElementCriteria::CurrentValue)
1149 d->setCurrentIndex(count() > 0 ? 0 : -1);
1150 d->updateCurrentElements();
1152 emit modelChanged();
1154 d->maybeUpdateImplicitContentWidth();
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168QQmlInstanceModel *QQuickComboBox::delegateModel()
const
1170 Q_D(
const QQuickComboBox);
1171 return d->delegateModel;
1176
1177
1178
1179
1180
1181
1182
1183
1184bool QQuickComboBox::isPressed()
const
1186 Q_D(
const QQuickComboBox);
1190void QQuickComboBox::setPressed(
bool pressed)
1192 Q_D(QQuickComboBox);
1193 if (d->pressed == pressed)
1196 d->pressed = pressed;
1197 emit pressedChanged();
1200 setDown(d->pressed || d->isPopupVisible());
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217int QQuickComboBox::highlightedIndex()
const
1219 Q_D(
const QQuickComboBox);
1220 return d->highlightedIndex;
1224
1225
1226
1227
1228
1229
1230
1231
1232int QQuickComboBox::currentIndex()
const
1234 Q_D(
const QQuickComboBox);
1235 return d->currentIndex;
1238void QQuickComboBox::setCurrentIndex(
int index)
1240 Q_D(QQuickComboBox);
1241 d->currentElementCriteria = QQuickComboBoxPrivate::CurrentElementCriteria::CurrentIndex;
1242 d->setCurrentIndex(index);
1246
1247
1248
1249
1250
1251
1252
1253QString QQuickComboBox::currentText()
const
1255 Q_D(
const QQuickComboBox);
1256 return d->currentText;
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278QString QQuickComboBox::displayText()
const
1280 Q_D(
const QQuickComboBox);
1281 return d->displayText;
1284void QQuickComboBox::setDisplayText(
const QString &text)
1286 Q_D(QQuickComboBox);
1287 d->hasDisplayText =
true;
1288 if (d->displayText == text)
1291 d->displayText = text;
1292 maybeSetAccessibleName(text);
1293 emit displayTextChanged();
1296void QQuickComboBox::resetDisplayText()
1298 Q_D(QQuickComboBox);
1299 if (!d->hasDisplayText)
1302 d->hasDisplayText =
false;
1303 d->updateCurrentText();
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317QString QQuickComboBox::textRole()
const
1319 Q_D(
const QQuickComboBox);
1323void QQuickComboBox::setTextRole(
const QString &role)
1325 Q_D(QQuickComboBox);
1326 if (d->textRole == role)
1330 if (isComponentComplete())
1331 d->updateCurrentText();
1332 emit textRoleChanged();
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346QString QQuickComboBox::valueRole()
const
1348 Q_D(
const QQuickComboBox);
1349 return d->valueRole;
1352void QQuickComboBox::setValueRole(
const QString &role)
1354 Q_D(QQuickComboBox);
1355 if (d->valueRole == role)
1358 d->valueRole = role;
1359 if (isComponentComplete())
1360 d->updateCurrentElements();
1361 emit valueRoleChanged();
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389QQmlComponent *QQuickComboBox::delegate()
const
1391 Q_D(
const QQuickComboBox);
1395void QQuickComboBox::setDelegate(QQmlComponent* delegate)
1397 Q_D(QQuickComboBox);
1398 if (d->delegate == delegate)
1401 d->delegate = delegate;
1402 QQmlDelegateModel *delegateModel = qobject_cast<QQmlDelegateModel*>(d->delegateModel);
1404 delegateModel->setDelegate(d->delegate);
1405 emit delegateChanged();
1409
1410
1411
1412
1413
1414
1415QQuickItem *QQuickComboBox::indicator()
const
1417 QQuickComboBoxPrivate *d =
const_cast<QQuickComboBoxPrivate *>(d_func());
1419 d->executeIndicator();
1420 return d->indicator;
1423void QQuickComboBox::setIndicator(QQuickItem *indicator)
1425 Q_D(QQuickComboBox);
1426 if (d->indicator == indicator)
1429 QQuickControlPrivate::warnIfCustomizationNotSupported(
this, indicator, QStringLiteral(
"indicator"));
1431 if (!d->indicator.isExecuting())
1432 d->cancelIndicator();
1434 const qreal oldImplicitIndicatorWidth = implicitIndicatorWidth();
1435 const qreal oldImplicitIndicatorHeight = implicitIndicatorHeight();
1437 d->removeImplicitSizeListener(d->indicator);
1438 QQuickControlPrivate::hideOldItem(d->indicator);
1439 d->indicator = indicator;
1441 if (!indicator->parentItem())
1442 indicator->setParentItem(
this);
1443 d->addImplicitSizeListener(indicator);
1446 if (!qFuzzyCompare(oldImplicitIndicatorWidth, implicitIndicatorWidth()))
1447 emit implicitIndicatorWidthChanged();
1448 if (!qFuzzyCompare(oldImplicitIndicatorHeight, implicitIndicatorHeight()))
1449 emit implicitIndicatorHeightChanged();
1450 if (!d->indicator.isExecuting())
1451 emit indicatorChanged();
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467QQuickPopup *QQuickComboBox::popup()
const
1469 QQuickComboBoxPrivate *d =
const_cast<QQuickComboBoxPrivate *>(d_func());
1471 d->executePopup(isComponentComplete());
1475void QQuickComboBox::setPopup(QQuickPopup *popup)
1477 Q_D(QQuickComboBox);
1478 if (d->popup == popup)
1481 if (!d->popup.isExecuting())
1485 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::destroyed, d, &QQuickComboBoxPrivate::popupDestroyed);
1486 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1487 QQuickComboBoxPrivate::hideOldPopup(d->popup);
1490#if QT_CONFIG(wayland)
1491 QQuickPopupPrivate::get(popup)->extendedWindowType = QNativeInterface::Private::QWaylandWindow::ComboBox;
1493 QQuickPopupPrivate::get(popup)->allowVerticalFlip =
true;
1494 popup->setClosePolicy(QQuickPopup::CloseOnEscape | QQuickPopup::CloseOnPressOutsideParent);
1495 QObjectPrivate::connect(popup, &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1498 QObjectPrivate::connect(popup, &QQuickPopup::destroyed, d, &QQuickComboBoxPrivate::popupDestroyed);
1500#if QT_CONFIG(quick_itemview)
1501 if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
1502 itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
1506 if (!d->popup.isExecuting())
1507 emit popupChanged();
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524bool QQuickComboBox::isFlat()
const
1526 Q_D(
const QQuickComboBox);
1530void QQuickComboBox::setFlat(
bool flat)
1532 Q_D(QQuickComboBox);
1533 if (d->flat == flat)
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552bool QQuickComboBox::isDown()
const
1554 Q_D(
const QQuickComboBox);
1558void QQuickComboBox::setDown(
bool down)
1560 Q_D(QQuickComboBox);
1563 if (d->down == down)
1570void QQuickComboBox::resetDown()
1572 Q_D(QQuickComboBox);
1576 setDown(d->pressed || d->isPopupVisible());
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590bool QQuickComboBox::isEditable()
const
1592 Q_D(
const QQuickComboBox);
1593 return d->extra.isAllocated() && d->extra->editable;
1596void QQuickComboBox::setEditable(
bool editable)
1598 Q_D(QQuickComboBox);
1599 if (editable == isEditable())
1602 if (d->contentItem) {
1604 d->contentItem->installEventFilter(
this);
1605 if (QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem)) {
1606 QObjectPrivate::connect(input, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
1607 QObjectPrivate::connect(input, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
1609#if QT_CONFIG(cursor)
1610 d->contentItem->setCursor(Qt::IBeamCursor);
1613 d->contentItem->removeEventFilter(
this);
1614 if (QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem)) {
1615 QObjectPrivate::disconnect(input, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
1616 QObjectPrivate::disconnect(input, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
1618#if QT_CONFIG(cursor)
1619 d->contentItem->unsetCursor();
1624 d->extra.value().editable = editable;
1625 setAccessibleProperty(
"editable", editable);
1626 emit editableChanged();
1630
1631
1632
1633
1634
1635
1636
1637QString QQuickComboBox::editText()
const
1639 Q_D(
const QQuickComboBox);
1640 return d->extra.isAllocated() ? d->extra->editText : QString();
1643void QQuickComboBox::setEditText(
const QString &text)
1645 Q_D(QQuickComboBox);
1646 if (text == editText())
1649 d->extra.value().editText = text;
1650 emit editTextChanged();
1653void QQuickComboBox::resetEditText()
1655 setEditText(QString());
1658#if QT_CONFIG(validator)
1660
1661
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
1688QValidator *QQuickComboBox::validator()
const
1690 Q_D(
const QQuickComboBox);
1691 return d->extra.isAllocated() ? d->extra->validator :
nullptr;
1694void QQuickComboBox::setValidator(QValidator *validator)
1696 Q_D(QQuickComboBox);
1697 if (validator == QQuickComboBox::validator())
1700 d->extra.value().validator = validator;
1701#if QT_CONFIG(validator)
1703 validator->setLocale(d->locale);
1705 emit validatorChanged();
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720Qt::InputMethodHints QQuickComboBox::inputMethodHints()
const
1722 Q_D(
const QQuickComboBox);
1723 return d->extra.isAllocated() ? d->extra->inputMethodHints : Qt::ImhNoPredictiveText;
1726void QQuickComboBox::setInputMethodHints(Qt::InputMethodHints hints)
1728 Q_D(QQuickComboBox);
1729 d->setInputMethodHints(hints);
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743bool QQuickComboBox::isInputMethodComposing()
const
1745 Q_D(
const QQuickComboBox);
1746 return d->contentItem && d->contentItem->property(
"inputMethodComposing").toBool();
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761bool QQuickComboBox::hasAcceptableInput()
const
1763 Q_D(
const QQuickComboBox);
1764 return d->m_acceptableInput;
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781qreal QQuickComboBox::implicitIndicatorWidth()
const
1783 Q_D(
const QQuickComboBox);
1786 return d->indicator->implicitWidth();
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803qreal QQuickComboBox::implicitIndicatorHeight()
const
1805 Q_D(
const QQuickComboBox);
1808 return d->indicator->implicitHeight();
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826QVariant QQuickComboBox::currentValue()
const
1828 Q_D(
const QQuickComboBox);
1829 return d->currentValue;
1832void QQuickComboBox::setCurrentValue(
const QVariant &value)
1834 Q_D(QQuickComboBox);
1835 d->currentElementCriteria = QQuickComboBoxPrivate::CurrentElementCriteria::CurrentValue;
1836 if (value == d->currentValue)
1839 d->currentValue = value;
1840 emit currentValueChanged();
1842 if (d->componentComplete)
1843 d->updateCurrentElements();
1847
1848
1849
1850
1851
1852
1853
1854
1855QVariant QQuickComboBox::valueAt(
int index)
const
1857 Q_D(
const QQuickComboBox);
1858 if (!d->isValidIndex(index))
1861 const QString effectiveValueRole = d->valueRole.isEmpty() ? QStringLiteral(
"modelData") : d->valueRole;
1862 return d->delegateModel->variantValue(index, effectiveValueRole);
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877int QQuickComboBox::indexOfValue(
const QVariant &value)
const
1879 for (
int i = 0; i < count(); ++i) {
1880 const QVariant ourValue = valueAt(i);
1881 if (value == ourValue)
1888
1889
1890
1891
1892
1893
1894
1895
1896bool QQuickComboBox::selectTextByMouse()
const
1898 Q_D(
const QQuickComboBox);
1899 return d->extra.isAllocated() ? d->extra->selectTextByMouse :
false;
1902void QQuickComboBox::setSelectTextByMouse(
bool canSelect)
1904 Q_D(QQuickComboBox);
1905 if (canSelect == selectTextByMouse())
1908 d->extra.value().selectTextByMouse = canSelect;
1909 emit selectTextByMouseChanged();
1913
1914
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
1966QQuickComboBox::ImplicitContentWidthPolicy QQuickComboBox::implicitContentWidthPolicy()
const
1968 Q_D(
const QQuickComboBox);
1969 return d->implicitContentWidthPolicy;
1972void QQuickComboBox::setImplicitContentWidthPolicy(QQuickComboBox::ImplicitContentWidthPolicy policy)
1974 Q_D(QQuickComboBox);
1975 if (policy == d->implicitContentWidthPolicy)
1978 d->implicitContentWidthPolicy = policy;
1979 d->maybeUpdateImplicitContentWidth();
1980 emit implicitContentWidthPolicyChanged();
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994QString QQuickComboBox::textAt(
int index)
const
1996 Q_D(
const QQuickComboBox);
1997 if (!d->isValidIndex(index))
2000 return d->delegateModel->stringValue(index, d->effectiveTextRole());
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027int QQuickComboBox::find(
const QString &text, Qt::MatchFlags flags)
const
2029 Q_D(
const QQuickComboBox);
2030 return d->match(0, text, flags);
2034
2035
2036
2037
2038
2039
2040
2041void QQuickComboBox::incrementCurrentIndex()
2043 Q_D(QQuickComboBox);
2044 d->incrementCurrentIndex();
2048
2049
2050
2051
2052
2053
2054
2055void QQuickComboBox::decrementCurrentIndex()
2057 Q_D(QQuickComboBox);
2058 d->decrementCurrentIndex();
2062
2063
2064
2065
2066
2067
2068
2069void QQuickComboBox::selectAll()
2071 Q_D(QQuickComboBox);
2072 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem);
2078bool QQuickComboBox::eventFilter(QObject *object, QEvent *event)
2080 Q_D(QQuickComboBox);
2081 switch (event->type()) {
2082 case QEvent::MouseButtonRelease:
2083 if (d->isPopupVisible())
2084 d->hidePopup(
false);
2086 case QEvent::KeyPress: {
2087 QKeyEvent *ke =
static_cast<QKeyEvent *>(event);
2088 if (d->filterKeyEvent(ke,
false))
2091 if (d->extra.isAllocated())
2092 d->extra->allowComplete = ke->key() != Qt::Key_Backspace && ke->key() != Qt::Key_Delete;
2095 case QEvent::FocusOut: {
2096 const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
2097 const bool usingPopupWindows =
2098 d->popup ? QQuickPopupPrivate::get(d->popup)->usePopupWindow() :
false;
2099 if (qGuiApp->focusObject() !=
this && !(hasActiveFocus && !usingPopupWindows)) {
2103 d->hidePopup(
false);
2108 const int indexForEditText = find(d->extra.value().editText, Qt::MatchFixedString);
2109 if (indexForEditText > -1)
2110 d->setCurrentItemAtIndex(indexForEditText, Activate);
2115 case QEvent::InputMethod:
2116 if (d->extra.isAllocated())
2117 d->extra->allowComplete = !
static_cast<QInputMethodEvent*>(event)->commitString().isEmpty();
2123 return QQuickControl::eventFilter(object, event);
2126void QQuickComboBox::focusInEvent(QFocusEvent *event)
2128 Q_D(QQuickComboBox);
2129 QQuickControl::focusInEvent(event);
2133 if ((event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason ||
2134 event->reason() == Qt::ShortcutFocusReason) && d->contentItem && isEditable())
2135 d->contentItem->forceActiveFocus(event->reason());
2138void QQuickComboBox::focusOutEvent(QFocusEvent *event)
2140 Q_D(QQuickComboBox);
2141 QQuickControl::focusOutEvent(event);
2143 const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
2144 const bool usingPopupWindows = d->popup && QQuickPopupPrivate::get(d->popup)->usePopupWindow();
2145 if (qGuiApp->focusObject() != d->contentItem && !(hasActiveFocus && !usingPopupWindows)) {
2149 d->hidePopup(
false);
2155void QQuickComboBox::inputMethodEvent(QInputMethodEvent *event)
2157 Q_D(QQuickComboBox);
2158 QQuickControl::inputMethodEvent(event);
2159 if (!isEditable() && !event->commitString().isEmpty())
2160 d->keySearch(event->commitString());
2166void QQuickComboBox::keyPressEvent(QKeyEvent *event)
2168 Q_D(QQuickComboBox);
2169 QQuickControl::keyPressEvent(event);
2171 const auto key = event->key();
2172 if (!isEditable()) {
2173 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
2174 if (buttonPressKeys.contains(key)) {
2175 if (!event->isAutoRepeat())
2183 case Qt::Key_Escape:
2185 d->acceptedEscKeyPress = d->isPopupVisible();
2186 d->receivedEscKeyPress =
true;
2187 if (d->acceptedEscKeyPress) {
2188 d->hidePopup(
false);
2194 case Qt::Key_Return:
2195 if (d->isPopupVisible())
2200 d->keyNavigating =
true;
2201 d->decrementCurrentIndex();
2205 d->keyNavigating =
true;
2206 d->incrementCurrentIndex();
2210 d->keyNavigating =
true;
2211 if (d->isPopupVisible())
2212 d->setHighlightedIndex(0, Highlight);
2214 d->setCurrentItemAtIndex(0, Activate);
2218 d->keyNavigating =
true;
2219 if (d->isPopupVisible())
2220 d->setHighlightedIndex(count() - 1, Highlight);
2222 d->setCurrentItemAtIndex(count() - 1, Activate);
2226 if (!isEditable() && !event->text().isEmpty())
2227 d->keySearch(event->text());
2234void QQuickComboBox::keyReleaseEvent(QKeyEvent *event)
2236 Q_D(QQuickComboBox);
2237 QQuickControl::keyReleaseEvent(event);
2238 d->keyNavigating =
false;
2239 if (event->isAutoRepeat())
2242 const auto key = event->key();
2243 if (!isEditable()) {
2244 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
2245 if (buttonPressKeys.contains(key)) {
2246 if (!isEditable() && isPressed())
2247 d->togglePopup(
true);
2256 case Qt::Key_Return:
2257 if (!isEditable() || d->isPopupVisible())
2258 d->hidePopup(d->isPopupVisible());
2262 case Qt::Key_Escape:
2268 if (d->acceptedEscKeyPress || !d->receivedEscKeyPress)
2270 d->acceptedEscKeyPress =
false;
2271 d->receivedEscKeyPress =
false;
2278#if QT_CONFIG(wheelevent)
2279void QQuickComboBox::wheelEvent(QWheelEvent *event)
2281 Q_D(QQuickComboBox);
2282 QQuickControl::wheelEvent(event);
2283 if (d->wheelEnabled && !d->isPopupVisible()) {
2284 if (event->angleDelta().y() > 0)
2285 d->decrementCurrentIndex();
2287 d->incrementCurrentIndex();
2292bool QQuickComboBox::event(QEvent *e)
2294 Q_D(QQuickComboBox);
2295 if (e->type() == QEvent::LanguageChange)
2296 d->updateCurrentElements();
2297 return QQuickControl::event(e);
2300void QQuickComboBox::componentComplete()
2302 Q_D(QQuickComboBox);
2303 d->executeIndicator(
true);
2304 QQuickControl::componentComplete();
2306 d->executePopup(
true);
2308 if (d->delegateModel && d->ownModel)
2309 static_cast<QQmlDelegateModel *>(d->delegateModel)->componentComplete();
2312 if (d->currentElementCriteria == QQuickComboBoxPrivate::CurrentElementCriteria::None && d->currentIndex == -1)
2313 d->setCurrentIndex(0);
2315 d->updateCurrentElements();
2319 if (!d->hasCalculatedWidestText)
2320 d->maybeUpdateImplicitContentWidth();
2324void QQuickComboBox::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &value)
2326 Q_D(QQuickComboBox);
2327 QQuickControl::itemChange(change, value);
2328 if (change == ItemVisibleHasChanged && !value.boolValue) {
2329 d->hidePopup(
false);
2334void QQuickComboBox::fontChange(
const QFont &newFont,
const QFont &oldFont)
2336 Q_D(QQuickComboBox);
2337 QQuickControl::fontChange(newFont, oldFont);
2338 d->maybeUpdateImplicitContentWidth();
2341void QQuickComboBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
2343 Q_D(QQuickComboBox);
2345 oldItem->removeEventFilter(
this);
2346 if (QQuickTextInput *oldInput = qobject_cast<QQuickTextInput *>(oldItem)) {
2347 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
2348 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
2349 disconnect(oldInput, &QQuickTextInput::inputMethodComposingChanged,
this, &QQuickComboBox::inputMethodComposingChanged);
2350 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::acceptableInputChanged, d, &QQuickComboBoxPrivate::updateAcceptableInput);
2353 if (newItem && isEditable()) {
2354 newItem->installEventFilter(
this);
2355 if (QQuickTextInput *newInput = qobject_cast<QQuickTextInput *>(newItem)) {
2356 QObjectPrivate::connect(newInput, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
2357 QObjectPrivate::connect(newInput, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
2358 connect(newInput, &QQuickTextInput::inputMethodComposingChanged,
this, &QQuickComboBox::inputMethodComposingChanged);
2359 QObjectPrivate::connect(newInput, &QQuickTextInput::acceptableInputChanged, d, &QQuickComboBoxPrivate::updateAcceptableInput);
2361#if QT_CONFIG(cursor)
2362 newItem->setCursor(Qt::IBeamCursor);
2366 d->updateAcceptableInput();
2369void QQuickComboBox::localeChange(
const QLocale &newLocale,
const QLocale &oldLocale)
2371 QQuickControl::localeChange(newLocale, oldLocale);
2372#if QT_CONFIG(validator)
2373 if (QValidator *v = validator())
2374 v->setLocale(newLocale);
2378QFont QQuickComboBox::defaultFont()
const
2380 return QQuickTheme::font(QQuickTheme::ComboBox);
2383#if QT_CONFIG(accessibility)
2384QAccessible::Role QQuickComboBox::accessibleRole()
const
2386 return QAccessible::ComboBox;
2389void QQuickComboBox::accessibilityActiveChanged(
bool active)
2391 Q_D(QQuickComboBox);
2392 QQuickControl::accessibilityActiveChanged(active);
2395 maybeSetAccessibleName(d->hasDisplayText ? d->displayText : d->currentText);
2396 setAccessibleProperty(
"editable", isEditable());
2403#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()