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;
1496 QQuickPopupPrivate::get(popup)->wmWindowType = QNativeInterface::Private::QXcbWindow::Combo;
1498 QQuickPopupPrivate::get(popup)->allowVerticalFlip =
true;
1499 popup->setClosePolicy(QQuickPopup::CloseOnEscape | QQuickPopup::CloseOnPressOutsideParent);
1500 QObjectPrivate::connect(popup, &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1503 QObjectPrivate::connect(popup, &QQuickPopup::destroyed, d, &QQuickComboBoxPrivate::popupDestroyed);
1505#if QT_CONFIG(quick_itemview)
1506 if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
1507 itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
1511 if (!d->popup.isExecuting())
1512 emit popupChanged();
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529bool QQuickComboBox::isFlat()
const
1531 Q_D(
const QQuickComboBox);
1535void QQuickComboBox::setFlat(
bool flat)
1537 Q_D(QQuickComboBox);
1538 if (d->flat == flat)
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557bool QQuickComboBox::isDown()
const
1559 Q_D(
const QQuickComboBox);
1563void QQuickComboBox::setDown(
bool down)
1565 Q_D(QQuickComboBox);
1568 if (d->down == down)
1575void QQuickComboBox::resetDown()
1577 Q_D(QQuickComboBox);
1581 setDown(d->pressed || d->isPopupVisible());
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595bool QQuickComboBox::isEditable()
const
1597 Q_D(
const QQuickComboBox);
1598 return d->extra.isAllocated() && d->extra->editable;
1601void QQuickComboBox::setEditable(
bool editable)
1603 Q_D(QQuickComboBox);
1604 if (editable == isEditable())
1607 if (d->contentItem) {
1609 d->contentItem->installEventFilter(
this);
1610 if (QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem)) {
1611 QObjectPrivate::connect(input, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
1612 QObjectPrivate::connect(input, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
1614#if QT_CONFIG(cursor)
1615 d->contentItem->setCursor(Qt::IBeamCursor);
1618 d->contentItem->removeEventFilter(
this);
1619 if (QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem)) {
1620 QObjectPrivate::disconnect(input, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
1621 QObjectPrivate::disconnect(input, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
1623#if QT_CONFIG(cursor)
1624 d->contentItem->unsetCursor();
1629 d->extra.value().editable = editable;
1630 setAccessibleProperty(
"editable", editable);
1631 emit editableChanged();
1635
1636
1637
1638
1639
1640
1641
1642QString QQuickComboBox::editText()
const
1644 Q_D(
const QQuickComboBox);
1645 return d->extra.isAllocated() ? d->extra->editText : QString();
1648void QQuickComboBox::setEditText(
const QString &text)
1650 Q_D(QQuickComboBox);
1651 if (text == editText())
1654 d->extra.value().editText = text;
1655 emit editTextChanged();
1658void QQuickComboBox::resetEditText()
1660 setEditText(QString());
1663#if QT_CONFIG(validator)
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
1690
1691
1692
1693QValidator *QQuickComboBox::validator()
const
1695 Q_D(
const QQuickComboBox);
1696 return d->extra.isAllocated() ? d->extra->validator :
nullptr;
1699void QQuickComboBox::setValidator(QValidator *validator)
1701 Q_D(QQuickComboBox);
1702 if (validator == QQuickComboBox::validator())
1705 d->extra.value().validator = validator;
1706#if QT_CONFIG(validator)
1708 validator->setLocale(d->locale);
1710 emit validatorChanged();
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725Qt::InputMethodHints QQuickComboBox::inputMethodHints()
const
1727 Q_D(
const QQuickComboBox);
1728 return d->extra.isAllocated() ? d->extra->inputMethodHints : Qt::ImhNoPredictiveText;
1731void QQuickComboBox::setInputMethodHints(Qt::InputMethodHints hints)
1733 Q_D(QQuickComboBox);
1734 d->setInputMethodHints(hints);
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748bool QQuickComboBox::isInputMethodComposing()
const
1750 Q_D(
const QQuickComboBox);
1751 return d->contentItem && d->contentItem->property(
"inputMethodComposing").toBool();
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766bool QQuickComboBox::hasAcceptableInput()
const
1768 Q_D(
const QQuickComboBox);
1769 return d->m_acceptableInput;
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786qreal QQuickComboBox::implicitIndicatorWidth()
const
1788 Q_D(
const QQuickComboBox);
1791 return d->indicator->implicitWidth();
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808qreal QQuickComboBox::implicitIndicatorHeight()
const
1810 Q_D(
const QQuickComboBox);
1813 return d->indicator->implicitHeight();
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831QVariant QQuickComboBox::currentValue()
const
1833 Q_D(
const QQuickComboBox);
1834 return d->currentValue;
1837void QQuickComboBox::setCurrentValue(
const QVariant &value)
1839 Q_D(QQuickComboBox);
1840 d->currentElementCriteria = QQuickComboBoxPrivate::CurrentElementCriteria::CurrentValue;
1841 if (value == d->currentValue)
1844 d->currentValue = value;
1845 emit currentValueChanged();
1847 if (d->componentComplete)
1848 d->updateCurrentElements();
1852
1853
1854
1855
1856
1857
1858
1859
1860QVariant QQuickComboBox::valueAt(
int index)
const
1862 Q_D(
const QQuickComboBox);
1863 if (!d->isValidIndex(index))
1866 const QString effectiveValueRole = d->valueRole.isEmpty() ? QStringLiteral(
"modelData") : d->valueRole;
1867 return d->delegateModel->variantValue(index, effectiveValueRole);
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882int QQuickComboBox::indexOfValue(
const QVariant &value)
const
1884 for (
int i = 0; i < count(); ++i) {
1885 const QVariant ourValue = valueAt(i);
1886 if (value == ourValue)
1893
1894
1895
1896
1897
1898
1899
1900
1901bool QQuickComboBox::selectTextByMouse()
const
1903 Q_D(
const QQuickComboBox);
1904 return d->extra.isAllocated() ? d->extra->selectTextByMouse :
false;
1907void QQuickComboBox::setSelectTextByMouse(
bool canSelect)
1909 Q_D(QQuickComboBox);
1910 if (canSelect == selectTextByMouse())
1913 d->extra.value().selectTextByMouse = canSelect;
1914 emit selectTextByMouseChanged();
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
1968
1969
1970
1971QQuickComboBox::ImplicitContentWidthPolicy QQuickComboBox::implicitContentWidthPolicy()
const
1973 Q_D(
const QQuickComboBox);
1974 return d->implicitContentWidthPolicy;
1977void QQuickComboBox::setImplicitContentWidthPolicy(QQuickComboBox::ImplicitContentWidthPolicy policy)
1979 Q_D(QQuickComboBox);
1980 if (policy == d->implicitContentWidthPolicy)
1983 d->implicitContentWidthPolicy = policy;
1984 d->maybeUpdateImplicitContentWidth();
1985 emit implicitContentWidthPolicyChanged();
1989
1990
1991
1992
1993
1994
1995
1996
1997bool QQuickComboBox::highlightOnHover()
const
1999 Q_D(
const QQuickComboBox);
2000 return d->highlightOnHover;
2003void QQuickComboBox::setHighlightOnHover(
bool value)
2005 Q_D(QQuickComboBox);
2006 if (value == highlightOnHover())
2009 d->highlightOnHover = value;
2010 emit highlightOnHoverChanged();
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024QString QQuickComboBox::textAt(
int index)
const
2026 Q_D(
const QQuickComboBox);
2027 if (!d->isValidIndex(index))
2030 return d->delegateModel->stringValue(index, d->effectiveTextRole());
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057int QQuickComboBox::find(
const QString &text, Qt::MatchFlags flags)
const
2059 Q_D(
const QQuickComboBox);
2060 return d->match(0, text, flags);
2064
2065
2066
2067
2068
2069
2070
2071void QQuickComboBox::incrementCurrentIndex()
2073 Q_D(QQuickComboBox);
2074 d->incrementCurrentIndex();
2078
2079
2080
2081
2082
2083
2084
2085void QQuickComboBox::decrementCurrentIndex()
2087 Q_D(QQuickComboBox);
2088 d->decrementCurrentIndex();
2092
2093
2094
2095
2096
2097
2098
2099void QQuickComboBox::selectAll()
2101 Q_D(QQuickComboBox);
2102 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem);
2108bool QQuickComboBox::eventFilter(QObject *object, QEvent *event)
2110 Q_D(QQuickComboBox);
2111 switch (event->type()) {
2112 case QEvent::MouseButtonRelease:
2113 if (d->isPopupVisible())
2114 d->hidePopup(
false);
2116 case QEvent::KeyPress: {
2117 QKeyEvent *ke =
static_cast<QKeyEvent *>(event);
2118 if (d->filterKeyEvent(ke,
false))
2121 if (d->extra.isAllocated())
2122 d->extra->allowComplete = ke->key() != Qt::Key_Backspace && ke->key() != Qt::Key_Delete;
2125 case QEvent::FocusOut: {
2126 const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
2127 const bool usingPopupWindows =
2128 d->popup ? QQuickPopupPrivate::get(d->popup)->usePopupWindow() :
false;
2129 if (qGuiApp->focusObject() !=
this && !(hasActiveFocus && !usingPopupWindows)) {
2133 d->hidePopup(
false);
2138 const int indexForEditText = find(d->extra.value().editText, Qt::MatchFixedString);
2139 if (indexForEditText > -1)
2140 d->setCurrentItemAtIndex(indexForEditText, Activate);
2145 case QEvent::InputMethod:
2146 if (d->extra.isAllocated())
2147 d->extra->allowComplete = !
static_cast<QInputMethodEvent*>(event)->commitString().isEmpty();
2153 return QQuickControl::eventFilter(object, event);
2156void QQuickComboBox::focusInEvent(QFocusEvent *event)
2158 Q_D(QQuickComboBox);
2159 QQuickControl::focusInEvent(event);
2163 if ((event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason ||
2164 event->reason() == Qt::ShortcutFocusReason) && d->contentItem && isEditable())
2165 d->contentItem->forceActiveFocus(event->reason());
2168void QQuickComboBox::focusOutEvent(QFocusEvent *event)
2170 Q_D(QQuickComboBox);
2171 QQuickControl::focusOutEvent(event);
2173 const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
2174 const bool usingPopupWindows = d->popup && QQuickPopupPrivate::get(d->popup)->usePopupWindow();
2175 if (qGuiApp->focusObject() != d->contentItem && !(hasActiveFocus && !usingPopupWindows)) {
2179 d->hidePopup(
false);
2185void QQuickComboBox::inputMethodEvent(QInputMethodEvent *event)
2187 Q_D(QQuickComboBox);
2188 QQuickControl::inputMethodEvent(event);
2189 if (!isEditable() && !event->commitString().isEmpty())
2190 d->keySearch(event->commitString());
2196void QQuickComboBox::keyPressEvent(QKeyEvent *event)
2198 Q_D(QQuickComboBox);
2199 QQuickControl::keyPressEvent(event);
2201 const auto key = event->key();
2202 if (!isEditable()) {
2203 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
2204 if (buttonPressKeys.contains(key)) {
2205 if (!event->isAutoRepeat())
2213 case Qt::Key_Escape:
2215 d->acceptedEscKeyPress = d->isPopupVisible();
2216 d->receivedEscKeyPress =
true;
2217 if (d->acceptedEscKeyPress) {
2218 d->hidePopup(
false);
2224 case Qt::Key_Return:
2225 if (d->isPopupVisible())
2230 d->keyNavigating =
true;
2231 d->decrementCurrentIndex();
2235 d->keyNavigating =
true;
2236 d->incrementCurrentIndex();
2240 d->keyNavigating =
true;
2241 if (d->isPopupVisible())
2242 d->setHighlightedIndex(0, Highlight);
2244 d->setCurrentItemAtIndex(0, Activate);
2248 d->keyNavigating =
true;
2249 if (d->isPopupVisible())
2250 d->setHighlightedIndex(count() - 1, Highlight);
2252 d->setCurrentItemAtIndex(count() - 1, Activate);
2256 if (!isEditable() && !event->text().isEmpty())
2257 d->keySearch(event->text());
2264void QQuickComboBox::keyReleaseEvent(QKeyEvent *event)
2266 Q_D(QQuickComboBox);
2267 QQuickControl::keyReleaseEvent(event);
2268 d->keyNavigating =
false;
2269 if (event->isAutoRepeat())
2272 const auto key = event->key();
2273 if (!isEditable()) {
2274 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
2275 if (buttonPressKeys.contains(key)) {
2276 if (!isEditable() && isPressed())
2277 d->togglePopup(
true);
2286 case Qt::Key_Return:
2287 if (!isEditable() || d->isPopupVisible())
2288 d->hidePopup(d->isPopupVisible());
2292 case Qt::Key_Escape:
2298 if (d->acceptedEscKeyPress || !d->receivedEscKeyPress)
2300 d->acceptedEscKeyPress =
false;
2301 d->receivedEscKeyPress =
false;
2308#if QT_CONFIG(wheelevent)
2309void QQuickComboBox::wheelEvent(QWheelEvent *event)
2311 Q_D(QQuickComboBox);
2312 QQuickControl::wheelEvent(event);
2313 if (d->wheelEnabled && !d->isPopupVisible()) {
2314 if (event->angleDelta().y() > 0)
2315 d->decrementCurrentIndex();
2317 d->incrementCurrentIndex();
2322bool QQuickComboBox::event(QEvent *e)
2324 Q_D(QQuickComboBox);
2325 if (e->type() == QEvent::LanguageChange)
2326 d->updateCurrentElements();
2327 return QQuickControl::event(e);
2330void QQuickComboBox::componentComplete()
2332 Q_D(QQuickComboBox);
2333 d->executeIndicator(
true);
2334 QQuickControl::componentComplete();
2336 d->executePopup(
true);
2338 if (d->delegateModel && d->ownModel)
2339 static_cast<QQmlDelegateModel *>(d->delegateModel)->componentComplete();
2342 if (d->currentElementCriteria == QQuickComboBoxPrivate::CurrentElementCriteria::None && d->currentIndex == -1)
2343 d->setCurrentIndex(0);
2345 d->updateCurrentElements();
2349 if (!d->hasCalculatedWidestText)
2350 d->maybeUpdateImplicitContentWidth();
2354void QQuickComboBox::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &value)
2356 Q_D(QQuickComboBox);
2357 QQuickControl::itemChange(change, value);
2358 if (change == ItemVisibleHasChanged && !value.boolValue) {
2359 d->hidePopup(
false);
2364void QQuickComboBox::fontChange(
const QFont &newFont,
const QFont &oldFont)
2366 Q_D(QQuickComboBox);
2367 QQuickControl::fontChange(newFont, oldFont);
2368 d->maybeUpdateImplicitContentWidth();
2371void QQuickComboBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
2373 Q_D(QQuickComboBox);
2375 oldItem->removeEventFilter(
this);
2376 if (QQuickTextInput *oldInput = qobject_cast<QQuickTextInput *>(oldItem)) {
2377 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
2378 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
2379 disconnect(oldInput, &QQuickTextInput::inputMethodComposingChanged,
this, &QQuickComboBox::inputMethodComposingChanged);
2380 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::acceptableInputChanged, d, &QQuickComboBoxPrivate::updateAcceptableInput);
2383 if (newItem && isEditable()) {
2384 newItem->installEventFilter(
this);
2385 if (QQuickTextInput *newInput = qobject_cast<QQuickTextInput *>(newItem)) {
2386 QObjectPrivate::connect(newInput, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
2387 QObjectPrivate::connect(newInput, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
2388 connect(newInput, &QQuickTextInput::inputMethodComposingChanged,
this, &QQuickComboBox::inputMethodComposingChanged);
2389 QObjectPrivate::connect(newInput, &QQuickTextInput::acceptableInputChanged, d, &QQuickComboBoxPrivate::updateAcceptableInput);
2391#if QT_CONFIG(cursor)
2392 newItem->setCursor(Qt::IBeamCursor);
2396 d->updateAcceptableInput();
2399void QQuickComboBox::localeChange(
const QLocale &newLocale,
const QLocale &oldLocale)
2401 QQuickControl::localeChange(newLocale, oldLocale);
2402#if QT_CONFIG(validator)
2403 if (QValidator *v = validator())
2404 v->setLocale(newLocale);
2408QFont QQuickComboBox::defaultFont()
const
2410 return QQuickTheme::font(QQuickTheme::ComboBox);
2413#if QT_CONFIG(accessibility)
2414QAccessible::Role QQuickComboBox::accessibleRole()
const
2416 return QAccessible::ComboBox;
2419void QQuickComboBox::accessibilityActiveChanged(
bool active)
2421 Q_D(QQuickComboBox);
2422 QQuickControl::accessibilityActiveChanged(active);
2425 maybeSetAccessibleName(d->hasDisplayText ? d->displayText : d->currentText);
2426 setAccessibleProperty(
"editable", isEditable());
2433#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()