14#include <QtCore/qregularexpression.h>
15#include <QtCore/qabstractitemmodel.h>
16#include <QtCore/qglobal.h>
17#include <QtGui/qinputmethod.h>
18#include <QtGui/qtextdocument.h>
19#include <QtGui/qguiapplication.h>
20#include <QtGui/private/qguiapplication_p.h>
21#include <QtGui/qpa/qplatformtheme.h>
22#include <QtQml/qjsvalue.h>
23#include <QtQml/qqmlcontext.h>
24#include <QtQml/qqmlcomponent.h>
25#include <QtQml/private/qlazilyallocated_p.h>
26#include <private/qqmldelegatemodel_p.h>
27#include <QtQuick/private/qquickaccessibleattached_p.h>
28#include <QtQuick/private/qquickevents_p_p.h>
29#include <QtQuick/private/qquicktextinput_p.h>
30#include <QtQuick/private/qquicktextinput_p_p.h>
31#if QT_CONFIG(quick_itemview)
32#include <QtQuick/private/qquickitemview_p.h>
37Q_STATIC_LOGGING_CATEGORY(lcCalculateWidestTextWidth,
"qt.quick.controls.combobox.calculatewidesttextwidth")
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
117
118
119
120
121
122
123
124
125
126
127
128
131
132
133
134
135
136
137
138
139
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
180 QQuickComboBox *combo =
nullptr;
195 if (role == QLatin1String(
"modelData")) {
196 const QVariant model = combo->model();
197 if (model.metaType() == QMetaType::fromType<QVariantList>()) {
198 const QVariant object = model.toList().value(index);
199 if (object.metaType() == QMetaType::fromType<QVariantMap>()) {
200 const QVariantMap data = object.toMap();
201 if (data.size() == 1)
207 return QQmlDelegateModel::variantValue(index, role);
213 Q_DECLARE_PUBLIC(QQuickComboBox)
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
275 void onDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QList<
int> &roles);
290 int match(
int start,
const QString &text, Qt::MatchFlags flags)
const;
295 bool handleMove(
const QPointF &point, ulong timestamp)
override;
357#if QT_CONFIG(validator)
366 return popup && popup->isVisible();
374 if (popup && !popup->isVisible())
385 emit q->activated(highlightedIndex);
387 if (popup && popup->isVisible())
393 if (!popup || !popup->isVisible())
402 if (isPopupVisible())
403 QGuiApplication::inputMethod()->reset();
405#if QT_CONFIG(quick_itemview)
406 QQuickItemView *itemView = popup->findChild<QQuickItemView *>();
408 itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
413#if QT_CONFIG(quick_itemview)
415 itemView->positionViewAtIndex(highlightedIndex, QQuickItemView::Beginning);
419 q->setDown(pressed || isPopupVisible());
428 emit q->popupChanged();
447 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender());
448 if (!button || !button->isHovered() || !button->isEnabled() || QQuickAbstractButtonPrivate::get(button)->touchId != -1)
456#if QT_CONFIG(quick_itemview)
457 if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
458 itemView->positionViewAtIndex(index, QQuickItemView::Contain);
466 QQuickItem *item = qobject_cast<QQuickItem *>(object);
467 if (item && !item->parentItem()) {
469 item->setParentItem(popup->contentItem());
471 item->setParentItem(q);
472 QQuickItemPrivate::get(item)->setCulled(
true);
475 QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(object);
477 button->setFocusPolicy(Qt::NoFocus);
482 if (!textFormat && !textFormatWarningDisplayed && Qt::mightBeRichText(q->textAt(index))) {
487 if (index == currentIndex && !q->isEditable())
493 if (componentComplete && (!extra.isAllocated() || !extra->accepting)) {
496 if (implicitContentWidthPolicy == QQuickComboBox::WidestText)
497 updateImplicitContentSize();
504 if (q->count() == 0) {
510 emit q->countChanged();
515 return textRole.isEmpty() ? QStringLiteral(
"modelData") : textRole;
521 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
525 const QString text = input->text();
527 if (extra.isAllocated() && extra->allowComplete && !text.isEmpty()) {
528 const QString completed = tryComplete(text);
529 if (completed.size() > text.size()) {
530 input->setText(completed);
532 input->select(completed.size(), text.size());
536 q->setEditText(text);
542 const int index = q->indexOfValue(currentValue);
547 emit q->currentIndexChanged();
553 const QString text = q->textAt(currentIndex);
554 if (currentText != text) {
557 q->maybeSetAccessibleName(text);
558 emit q->currentTextChanged();
560 if (!hasDisplayText && displayText != text) {
562 emit q->displayTextChanged();
564 if (!extra.isAllocated() || !extra->accepting)
565 q->setEditText(currentText);
571 const QVariant value = q->valueAt(currentIndex);
572 if (currentValue == value)
575 currentValue = value;
576 emit q->currentValueChanged();
602 const QQuickTextInput *textInputContentItem = qobject_cast<QQuickTextInput *>(contentItem);
604 if (!textInputContentItem)
607 const bool newValue = textInputContentItem->hasAcceptableInput();
611 emit q->acceptableInputChanged();
630 int idx = q->find(extra.value().editText, Qt::MatchFixedString);
635 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(contentItem);
637 const auto text = input->text();
638 input->select(text.size(), text.size());
642 extra.value().accepting =
true;
648 idx = q->find(extra.value().editText, Qt::MatchFixedString);
651 extra.value().accepting =
false;
659 const int itemCount = q->count();
660 for (
int idx = 0; idx < itemCount; ++idx) {
661 const QString text = q->textAt(idx);
662 if (!text.startsWith(input, Qt::CaseInsensitive))
666 if (match.isEmpty() || text.size() < match.size())
673 return input + match.mid(input.size());
683 emit q->currentIndexChanged();
685 if (componentComplete)
696 emit q->currentIndexChanged();
702 emit q->activated(index);
708 if (extra.isAllocated())
709 extra->allowComplete =
false;
711 if (highlightedIndex < q->count() - 1)
714 if (currentIndex < q->count() - 1)
717 if (extra.isAllocated())
718 extra->allowComplete =
true;
723 if (extra.isAllocated())
724 extra->allowComplete =
false;
732 if (extra.isAllocated())
733 extra->allowComplete =
true;
748 emit q->highlightedIndexChanged();
751 emit q->highlighted(index);
757 const int index = match(startIndex + 1, text, Qt::MatchStartsWith | Qt::MatchWrap);
768 Q_Q(
const QQuickComboBox);
769 uint matchType = flags & 0x0F;
770 bool wrap = flags & Qt::MatchWrap;
771 Qt::CaseSensitivity cs = flags & Qt::MatchCaseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive;
772 QRegularExpression::PatternOptions options = flags & Qt::MatchCaseSensitive ? QRegularExpression::NoPatternOption
773 : QRegularExpression::CaseInsensitiveOption;
778 for (
int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
779 for (
int idx = from; idx < to; ++idx) {
780 QString t = q->textAt(idx);
782 case Qt::MatchExactly:
786 case Qt::MatchRegularExpression: {
787 QRegularExpression rx(QRegularExpression::anchoredPattern(text), options);
788 if (rx.match(t).hasMatch())
792 case Qt::MatchWildcard: {
793 QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(text),
795 if (rx.match(t).hasMatch())
799 case Qt::MatchStartsWith:
800 if (t.startsWith(text, cs))
803 case Qt::MatchEndsWith:
804 if (t.endsWith(text, cs))
807 case Qt::MatchFixedString:
808 if (t.compare(text, cs) == 0)
811 case Qt::MatchContains:
813 if (t.contains(text, cs))
839 if (!delegateModel && model.isValid()) {
840 QQmlDelegateModel *dataModel =
new QQuickComboBoxDelegateModel(q);
841 dataModel->setModel(model);
843 if (q->isComponentComplete())
844 dataModel->componentComplete();
856 emit q->delegateModelChanged();
865 QQuickControlPrivate::handlePress(point, timestamp);
873 QQuickControlPrivate::handleMove(point, timestamp);
874 q->setPressed(q->contains(point));
881 QQuickControlPrivate::handleRelease(point, timestamp);
883 q->setPressed(
false);
892 QQuickControlPrivate::handleUngrab();
893 q->setPressed(
false);
899 quickCancelDeferred(q, indicatorName());
905 if (indicator.wasExecuted())
908 if (!indicator || complete)
909 quickBeginDeferred(q, indicatorName(), indicator);
911 quickCompleteDeferred(q, indicatorName(), indicator);
914static inline QString
popupName() {
return QStringLiteral(
"popup"); }
919 quickCancelDeferred(q, popupName());
925 if (popup.wasExecuted())
928 if (!popup || complete)
929 quickBeginDeferred(q, popupName(), popup);
931 quickCompleteDeferred(q, popupName(), popup);
937 QQuickControlPrivate::itemImplicitWidthChanged(item);
938 if (item == indicator)
939 emit q->implicitIndicatorWidthChanged();
945 if (!force && hints == q->inputMethodHints())
948 extra.value().inputMethodHints = hints;
949 emit q->inputMethodHintsChanged();
955 QQuickControlPrivate::itemImplicitHeightChanged(item);
956 if (item == indicator)
957 emit q->implicitIndicatorHeightChanged();
963 QQuickControlPrivate::itemDestroyed(item);
964 if (item == indicator) {
966 emit q->indicatorChanged();
972 if (componentComplete) {
973 switch (implicitContentWidthPolicy) {
974 case QQuickComboBox::WidestText:
975 return calculateWidestTextWidth();
976 case QQuickComboBox::WidestTextWhenCompleted:
977 if (!hasCalculatedWidestText)
978 return calculateWidestTextWidth();
985 return QQuickControlPrivate::getContentWidth();
990 Q_Q(
const QQuickComboBox);
991 if (!componentComplete)
994 const int count = q->count();
998 auto textInput = qobject_cast<QQuickTextInput*>(contentItem);
1002 qCDebug(lcCalculateWidestTextWidth) <<
"calculating widest text from" << count <<
"items...";
1006 const QString textRole = effectiveTextRole();
1007 auto textInputPrivate = QQuickTextInputPrivate::get(textInput);
1009 for (
int i = 0; i < count; ++i) {
1010 const QString text = delegateModel->stringValue(i, textRole);
1011 const qreal textImplicitWidth = textInputPrivate->calculateImplicitWidthForText(text);
1012 widest = qMax(widest, textImplicitWidth);
1015 qCDebug(lcCalculateWidestTextWidth) <<
"... widest text is" << widest;
1020
1021
1022
1023
1024
1027 if (!componentComplete)
1030 if (implicitContentWidthPolicy == QQuickComboBox::ContentItemImplicitWidth
1031 || (implicitContentWidthPolicy == QQuickComboBox::WidestTextWhenCompleted && hasCalculatedWidestText))
1034 updateImplicitContentWidth();
1043 qCDebug(lcItemManagement) <<
"hiding old popup" << popup;
1045 popup->setVisible(
false);
1046 popup->setParentItem(
nullptr);
1047#if QT_CONFIG(accessibility)
1049 QQuickAccessibleAttached *accessible = accessibleAttached(popup);
1051 accessible->setIgnored(
true);
1055QQuickComboBox::QQuickComboBox(QQuickItem *parent)
1056 : QQuickControl(*(
new QQuickComboBoxPrivate), parent)
1058 setFocusPolicy(Qt::StrongFocus);
1059 setFlag(QQuickItem::ItemIsFocusScope);
1060 setAcceptedMouseButtons(Qt::LeftButton);
1061#if QT_CONFIG(cursor)
1062 setCursor(Qt::ArrowCursor);
1064 Q_D(QQuickComboBox);
1065 d->setInputMethodHints(Qt::ImhNoPredictiveText,
true);
1066 d->setSizePolicy(QLayoutPolicy::Preferred, QLayoutPolicy::Fixed);
1069QQuickComboBox::~QQuickComboBox()
1071 Q_D(QQuickComboBox);
1072 d->removeImplicitSizeListener(d->indicator);
1076 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1077 QQuickComboBoxPrivate::hideOldPopup(d->popup);
1083
1084
1085
1086
1087
1088int QQuickComboBox::count()
const
1090 Q_D(
const QQuickComboBox);
1091 return d->delegateModel ? d->delegateModel->count() : 0;
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112QVariant QQuickComboBox::model()
const
1114 Q_D(
const QQuickComboBox);
1115 if (!d->qobjectModelGuard.has_value()) {
1121 return !d->qobjectModelGuard->isNull() ? d->model : QVariant();
1124void QQuickComboBox::setModel(
const QVariant& m)
1126 Q_D(QQuickComboBox);
1127 QVariant newModel = m;
1128 if (newModel.userType() == qMetaTypeId<QJSValue>())
1129 newModel = newModel.value<QJSValue>().toVariant();
1131 if (d->model == newModel)
1134 if (d->qobjectModelGuard.has_value() && !d->qobjectModelGuard->isNull()) {
1135 if (QAbstractItemModel* aim = qvariant_cast<QAbstractItemModel *>(d->model)) {
1136 QObjectPrivate::disconnect(aim, &QAbstractItemModel::dataChanged,
1137 d, &QQuickComboBoxPrivate::onDataChanged);
1140 if (QObject* newModelAsQObject = qvariant_cast<QObject *>(newModel)) {
1144 d->qobjectModelGuard = QPointer<QObject>(newModelAsQObject);
1146 if (QAbstractItemModel* aim = qvariant_cast<QAbstractItemModel *>(newModel)) {
1147 QObjectPrivate::connect(aim, &QAbstractItemModel::dataChanged,
1148 d, &QQuickComboBoxPrivate::onDataChanged);
1152 d->qobjectModelGuard.reset();
1155 d->model = newModel;
1156 d->createDelegateModel();
1157 emit countChanged();
1158 if (isComponentComplete()) {
1159 if (d->currentElementCriteria != QQuickComboBoxPrivate::CurrentElementCriteria::CurrentValue)
1160 d->setCurrentIndex(count() > 0 ? 0 : -1);
1161 d->updateCurrentElements();
1163 emit modelChanged();
1165 d->maybeUpdateImplicitContentWidth();
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179QQmlInstanceModel *QQuickComboBox::delegateModel()
const
1181 Q_D(
const QQuickComboBox);
1182 return d->delegateModel;
1187
1188
1189
1190
1191
1192
1193
1194
1195bool QQuickComboBox::isPressed()
const
1197 Q_D(
const QQuickComboBox);
1201void QQuickComboBox::setPressed(
bool pressed)
1203 Q_D(QQuickComboBox);
1204 if (d->pressed == pressed)
1207 d->pressed = pressed;
1208 emit pressedChanged();
1211 setDown(d->pressed || d->isPopupVisible());
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228int QQuickComboBox::highlightedIndex()
const
1230 Q_D(
const QQuickComboBox);
1231 return d->highlightedIndex;
1235
1236
1237
1238
1239
1240
1241
1242
1243int QQuickComboBox::currentIndex()
const
1245 Q_D(
const QQuickComboBox);
1246 return d->currentIndex;
1249void QQuickComboBox::setCurrentIndex(
int index)
1251 Q_D(QQuickComboBox);
1252 d->currentElementCriteria = QQuickComboBoxPrivate::CurrentElementCriteria::CurrentIndex;
1253 d->setCurrentIndex(index);
1257
1258
1259
1260
1261
1262
1263
1264QString QQuickComboBox::currentText()
const
1266 Q_D(
const QQuickComboBox);
1267 return d->currentText;
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289QString QQuickComboBox::displayText()
const
1291 Q_D(
const QQuickComboBox);
1292 return d->displayText;
1295void QQuickComboBox::setDisplayText(
const QString &text)
1297 Q_D(QQuickComboBox);
1298 d->hasDisplayText =
true;
1299 if (d->displayText == text)
1302 d->displayText = text;
1303 maybeSetAccessibleName(text);
1304 emit displayTextChanged();
1307void QQuickComboBox::resetDisplayText()
1309 Q_D(QQuickComboBox);
1310 if (!d->hasDisplayText)
1313 d->hasDisplayText =
false;
1314 d->updateCurrentText();
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328QString QQuickComboBox::textRole()
const
1330 Q_D(
const QQuickComboBox);
1334void QQuickComboBox::setTextRole(
const QString &role)
1336 Q_D(QQuickComboBox);
1337 if (d->textRole == role)
1341 if (isComponentComplete())
1342 d->updateCurrentText();
1343 emit textRoleChanged();
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357QString QQuickComboBox::valueRole()
const
1359 Q_D(
const QQuickComboBox);
1360 return d->valueRole;
1363void QQuickComboBox::setValueRole(
const QString &role)
1365 Q_D(QQuickComboBox);
1366 if (d->valueRole == role)
1369 d->valueRole = role;
1370 if (isComponentComplete())
1371 d->updateCurrentElements();
1372 emit valueRoleChanged();
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389QQuickText::TextFormat QQuickComboBox::textFormat()
const
1391 Q_D(
const QQuickComboBox);
1392 return d->textFormat.value_or(QQuickText::AutoText);
1395void QQuickComboBox::setTextFormat(QQuickText::TextFormat textFormat)
1397 Q_D(QQuickComboBox);
1398 const bool changed = textFormat !=
this->textFormat();
1399 d->textFormat = textFormat;
1401 emit textFormatChanged();
1404void QQuickComboBox::resetTextFormat()
1406 Q_D(QQuickComboBox);
1407 const bool changed = textFormat() != QQuickText::AutoText;
1408 d->textFormat.reset();
1410 emit textFormatChanged();
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438QQmlComponent *QQuickComboBox::delegate()
const
1440 Q_D(
const QQuickComboBox);
1444void QQuickComboBox::setDelegate(QQmlComponent* delegate)
1446 Q_D(QQuickComboBox);
1447 if (d->delegate == delegate)
1450 d->delegate = delegate;
1451 QQmlDelegateModel *delegateModel = qobject_cast<QQmlDelegateModel*>(d->delegateModel);
1453 delegateModel->setDelegate(d->delegate);
1454 emit delegateChanged();
1458
1459
1460
1461
1462
1463
1464QQuickItem *QQuickComboBox::indicator()
const
1466 QQuickComboBoxPrivate *d =
const_cast<QQuickComboBoxPrivate *>(d_func());
1468 d->executeIndicator();
1469 return d->indicator;
1472void QQuickComboBox::setIndicator(QQuickItem *indicator)
1474 Q_D(QQuickComboBox);
1475 if (d->indicator == indicator)
1478 QQuickControlPrivate::warnIfCustomizationNotSupported(
this, indicator, QStringLiteral(
"indicator"));
1480 if (!d->indicator.isExecuting())
1481 d->cancelIndicator();
1483 const qreal oldImplicitIndicatorWidth = implicitIndicatorWidth();
1484 const qreal oldImplicitIndicatorHeight = implicitIndicatorHeight();
1486 d->removeImplicitSizeListener(d->indicator);
1487 QQuickControlPrivate::hideOldItem(d->indicator);
1488 d->indicator = indicator;
1490 if (!indicator->parentItem())
1491 indicator->setParentItem(
this);
1492 d->addImplicitSizeListener(indicator);
1495 if (!qFuzzyCompare(oldImplicitIndicatorWidth, implicitIndicatorWidth()))
1496 emit implicitIndicatorWidthChanged();
1497 if (!qFuzzyCompare(oldImplicitIndicatorHeight, implicitIndicatorHeight()))
1498 emit implicitIndicatorHeightChanged();
1499 if (!d->indicator.isExecuting())
1500 emit indicatorChanged();
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516QQuickPopup *QQuickComboBox::popup()
const
1518 QQuickComboBoxPrivate *d =
const_cast<QQuickComboBoxPrivate *>(d_func());
1520 d->executePopup(isComponentComplete());
1524void QQuickComboBox::setPopup(QQuickPopup *popup)
1526 Q_D(QQuickComboBox);
1527 if (d->popup == popup)
1530 if (!d->popup.isExecuting())
1534 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::destroyed, d, &QQuickComboBoxPrivate::popupDestroyed);
1535 QObjectPrivate::disconnect(d->popup.data(), &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1536 QQuickComboBoxPrivate::hideOldPopup(d->popup);
1539#if QT_CONFIG(wayland)
1540 QQuickPopupPrivate::get(popup)->extendedWindowType = QNativeInterface::Private::QWaylandWindow::ComboBox;
1543 QQuickPopupPrivate::get(popup)->wmWindowType = QNativeInterface::Private::QXcbWindow::Combo;
1545 QQuickPopupPrivate::get(popup)->allowVerticalFlip =
true;
1546 popup->setClosePolicy(QQuickPopup::CloseOnEscape | QQuickPopup::CloseOnPressOutsideParent);
1547 QObjectPrivate::connect(popup, &QQuickPopup::visibleChanged, d, &QQuickComboBoxPrivate::popupVisibleChanged);
1550 QObjectPrivate::connect(popup, &QQuickPopup::destroyed, d, &QQuickComboBoxPrivate::popupDestroyed);
1552#if QT_CONFIG(quick_itemview)
1553 if (QQuickItemView *itemView = popup->findChild<QQuickItemView *>())
1554 itemView->setHighlightRangeMode(QQuickItemView::NoHighlightRange);
1558 if (!d->popup.isExecuting())
1559 emit popupChanged();
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576bool QQuickComboBox::isFlat()
const
1578 Q_D(
const QQuickComboBox);
1582void QQuickComboBox::setFlat(
bool flat)
1584 Q_D(QQuickComboBox);
1585 if (d->flat == flat)
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604bool QQuickComboBox::isDown()
const
1606 Q_D(
const QQuickComboBox);
1610void QQuickComboBox::setDown(
bool down)
1612 Q_D(QQuickComboBox);
1615 if (d->down == down)
1622void QQuickComboBox::resetDown()
1624 Q_D(QQuickComboBox);
1628 setDown(d->pressed || d->isPopupVisible());
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642bool QQuickComboBox::isEditable()
const
1644 Q_D(
const QQuickComboBox);
1645 return d->extra.isAllocated() && d->extra->editable;
1648void QQuickComboBox::setEditable(
bool editable)
1650 Q_D(QQuickComboBox);
1651 if (editable == isEditable())
1654 if (d->contentItem) {
1656 d->contentItem->installEventFilter(
this);
1657 if (QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem)) {
1658 QObjectPrivate::connect(input, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
1659 QObjectPrivate::connect(input, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
1661#if QT_CONFIG(cursor)
1662 d->contentItem->setCursor(Qt::IBeamCursor);
1665 d->contentItem->removeEventFilter(
this);
1666 if (QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem)) {
1667 QObjectPrivate::disconnect(input, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
1668 QObjectPrivate::disconnect(input, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
1670#if QT_CONFIG(cursor)
1671 d->contentItem->unsetCursor();
1676 d->extra.value().editable = editable;
1677 setAccessibleProperty(
"editable", editable);
1678 emit editableChanged();
1682
1683
1684
1685
1686
1687
1688
1689QString QQuickComboBox::editText()
const
1691 Q_D(
const QQuickComboBox);
1692 return d->extra.isAllocated() ? d->extra->editText : QString();
1695void QQuickComboBox::setEditText(
const QString &text)
1697 Q_D(QQuickComboBox);
1698 if (text == editText())
1701 d->extra.value().editText = text;
1702 emit editTextChanged();
1705void QQuickComboBox::resetEditText()
1707 setEditText(QString());
1710#if QT_CONFIG(validator)
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740QValidator *QQuickComboBox::validator()
const
1742 Q_D(
const QQuickComboBox);
1743 return d->extra.isAllocated() ? d->extra->validator :
nullptr;
1746void QQuickComboBox::setValidator(QValidator *validator)
1748 Q_D(QQuickComboBox);
1749 if (validator == QQuickComboBox::validator())
1752 d->extra.value().validator = validator;
1753#if QT_CONFIG(validator)
1755 validator->setLocale(d->locale);
1757 emit validatorChanged();
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772Qt::InputMethodHints QQuickComboBox::inputMethodHints()
const
1774 Q_D(
const QQuickComboBox);
1775 return d->extra.isAllocated() ? d->extra->inputMethodHints : Qt::ImhNoPredictiveText;
1778void QQuickComboBox::setInputMethodHints(Qt::InputMethodHints hints)
1780 Q_D(QQuickComboBox);
1781 d->setInputMethodHints(hints);
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795bool QQuickComboBox::isInputMethodComposing()
const
1797 Q_D(
const QQuickComboBox);
1798 return d->contentItem && d->contentItem->property(
"inputMethodComposing").toBool();
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813bool QQuickComboBox::hasAcceptableInput()
const
1815 Q_D(
const QQuickComboBox);
1816 return d->m_acceptableInput;
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833qreal QQuickComboBox::implicitIndicatorWidth()
const
1835 Q_D(
const QQuickComboBox);
1838 return d->indicator->implicitWidth();
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855qreal QQuickComboBox::implicitIndicatorHeight()
const
1857 Q_D(
const QQuickComboBox);
1860 return d->indicator->implicitHeight();
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878QVariant QQuickComboBox::currentValue()
const
1880 Q_D(
const QQuickComboBox);
1881 return d->currentValue;
1884void QQuickComboBox::setCurrentValue(
const QVariant &value)
1886 Q_D(QQuickComboBox);
1887 d->currentElementCriteria = QQuickComboBoxPrivate::CurrentElementCriteria::CurrentValue;
1888 if (value == d->currentValue)
1891 d->currentValue = value;
1892 emit currentValueChanged();
1894 if (d->componentComplete)
1895 d->updateCurrentElements();
1899
1900
1901
1902
1903
1904
1905
1906
1907QVariant QQuickComboBox::valueAt(
int index)
const
1909 Q_D(
const QQuickComboBox);
1910 if (!d->isValidIndex(index))
1913 const QString effectiveValueRole = d->valueRole.isEmpty() ? QStringLiteral(
"modelData") : d->valueRole;
1914 return d->delegateModel->variantValue(index, effectiveValueRole);
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929int QQuickComboBox::indexOfValue(
const QVariant &value)
const
1931 for (
int i = 0; i < count(); ++i) {
1932 const QVariant ourValue = valueAt(i);
1933 if (value == ourValue)
1940
1941
1942
1943
1944
1945
1946
1947
1948bool QQuickComboBox::selectTextByMouse()
const
1950 Q_D(
const QQuickComboBox);
1951 return d->extra.isAllocated() ? d->extra->selectTextByMouse :
false;
1954void QQuickComboBox::setSelectTextByMouse(
bool canSelect)
1956 Q_D(QQuickComboBox);
1957 if (canSelect == selectTextByMouse())
1960 d->extra.value().selectTextByMouse = canSelect;
1961 emit selectTextByMouseChanged();
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018QQuickComboBox::ImplicitContentWidthPolicy QQuickComboBox::implicitContentWidthPolicy()
const
2020 Q_D(
const QQuickComboBox);
2021 return d->implicitContentWidthPolicy;
2024void QQuickComboBox::setImplicitContentWidthPolicy(QQuickComboBox::ImplicitContentWidthPolicy policy)
2026 Q_D(QQuickComboBox);
2027 if (policy == d->implicitContentWidthPolicy)
2030 d->implicitContentWidthPolicy = policy;
2031 d->maybeUpdateImplicitContentWidth();
2032 emit implicitContentWidthPolicyChanged();
2036
2037
2038
2039
2040
2041
2042
2043
2044bool QQuickComboBox::highlightOnHover()
const
2046 Q_D(
const QQuickComboBox);
2047 return d->highlightOnHover;
2050void QQuickComboBox::setHighlightOnHover(
bool value)
2052 Q_D(QQuickComboBox);
2053 if (value == highlightOnHover())
2056 d->highlightOnHover = value;
2057 emit highlightOnHoverChanged();
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071QString QQuickComboBox::textAt(
int index)
const
2073 Q_D(
const QQuickComboBox);
2074 if (!d->isValidIndex(index))
2077 return d->delegateModel->stringValue(index, d->effectiveTextRole());
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104int QQuickComboBox::find(
const QString &text, Qt::MatchFlags flags)
const
2106 Q_D(
const QQuickComboBox);
2107 return d->match(0, text, flags);
2111
2112
2113
2114
2115
2116
2117
2118void QQuickComboBox::incrementCurrentIndex()
2120 Q_D(QQuickComboBox);
2121 d->incrementCurrentIndex();
2125
2126
2127
2128
2129
2130
2131
2132void QQuickComboBox::decrementCurrentIndex()
2134 Q_D(QQuickComboBox);
2135 d->decrementCurrentIndex();
2139
2140
2141
2142
2143
2144
2145
2146void QQuickComboBox::selectAll()
2148 Q_D(QQuickComboBox);
2149 QQuickTextInput *input = qobject_cast<QQuickTextInput *>(d->contentItem);
2155bool QQuickComboBox::eventFilter(QObject *object, QEvent *event)
2157 Q_D(QQuickComboBox);
2158 switch (event->type()) {
2159 case QEvent::MouseButtonRelease:
2160 if (d->isPopupVisible())
2161 d->hidePopup(
false);
2163 case QEvent::KeyPress: {
2164 QKeyEvent *ke =
static_cast<QKeyEvent *>(event);
2165 if (d->filterKeyEvent(ke,
false))
2168 if (d->extra.isAllocated())
2169 d->extra->allowComplete = ke->key() != Qt::Key_Backspace && ke->key() != Qt::Key_Delete;
2172 case QEvent::FocusOut: {
2173 const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
2174 const bool usingPopupWindows =
2175 d->popup ? QQuickPopupPrivate::get(d->popup)->usePopupWindow() :
false;
2176 if (qGuiApp->focusObject() !=
this && !(hasActiveFocus && !usingPopupWindows)) {
2180 d->hidePopup(
false);
2185 const int indexForEditText = find(d->extra.value().editText, Qt::MatchFixedString);
2186 if (indexForEditText > -1)
2187 d->setCurrentItemAtIndex(indexForEditText, Activate);
2192 case QEvent::InputMethod:
2193 if (d->extra.isAllocated())
2194 d->extra->allowComplete = !
static_cast<QInputMethodEvent*>(event)->commitString().isEmpty();
2200 return QQuickControl::eventFilter(object, event);
2203void QQuickComboBox::focusInEvent(QFocusEvent *event)
2205 Q_D(QQuickComboBox);
2206 QQuickControl::focusInEvent(event);
2210 if ((event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason ||
2211 event->reason() == Qt::ShortcutFocusReason) && d->contentItem && isEditable())
2212 d->contentItem->forceActiveFocus(event->reason());
2215void QQuickComboBox::focusOutEvent(QFocusEvent *event)
2217 Q_D(QQuickComboBox);
2218 QQuickControl::focusOutEvent(event);
2220 const bool hasActiveFocus = d->popup && d->popup->hasActiveFocus();
2221 const bool usingPopupWindows = d->popup && QQuickPopupPrivate::get(d->popup)->usePopupWindow();
2222 if (qGuiApp->focusObject() != d->contentItem && !(hasActiveFocus && !usingPopupWindows)) {
2226 d->hidePopup(
false);
2232void QQuickComboBox::inputMethodEvent(QInputMethodEvent *event)
2234 Q_D(QQuickComboBox);
2235 QQuickControl::inputMethodEvent(event);
2236 if (!isEditable() && !event->commitString().isEmpty())
2237 d->keySearch(event->commitString());
2243void QQuickComboBox::keyPressEvent(QKeyEvent *event)
2245 Q_D(QQuickComboBox);
2246 QQuickControl::keyPressEvent(event);
2248 const auto key = event->key();
2249 if (!isEditable()) {
2250 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
2251 if (buttonPressKeys.contains(key)) {
2252 if (!event->isAutoRepeat())
2260 case Qt::Key_Escape:
2262 d->acceptedEscKeyPress = d->isPopupVisible();
2263 d->receivedEscKeyPress =
true;
2264 if (d->acceptedEscKeyPress) {
2265 d->hidePopup(
false);
2271 case Qt::Key_Return:
2272 if (d->isPopupVisible())
2277 d->keyNavigating =
true;
2278 d->decrementCurrentIndex();
2282 d->keyNavigating =
true;
2283 d->incrementCurrentIndex();
2287 d->keyNavigating =
true;
2288 if (d->isPopupVisible())
2289 d->setHighlightedIndex(0, Highlight);
2291 d->setCurrentItemAtIndex(0, Activate);
2295 d->keyNavigating =
true;
2296 if (d->isPopupVisible())
2297 d->setHighlightedIndex(count() - 1, Highlight);
2299 d->setCurrentItemAtIndex(count() - 1, Activate);
2303 if (!isEditable() && !event->text().isEmpty())
2304 d->keySearch(event->text());
2311void QQuickComboBox::keyReleaseEvent(QKeyEvent *event)
2313 Q_D(QQuickComboBox);
2314 QQuickControl::keyReleaseEvent(event);
2315 d->keyNavigating =
false;
2316 if (event->isAutoRepeat())
2319 const auto key = event->key();
2320 if (!isEditable()) {
2321 const auto buttonPressKeys = QGuiApplicationPrivate::platformTheme()->themeHint(QPlatformTheme::ButtonPressKeys).value<QList<Qt::Key>>();
2322 if (buttonPressKeys.contains(key)) {
2323 if (!isEditable() && isPressed())
2324 d->togglePopup(
true);
2333 case Qt::Key_Return:
2334 if (!isEditable() || d->isPopupVisible())
2335 d->hidePopup(d->isPopupVisible());
2339 case Qt::Key_Escape:
2345 if (d->acceptedEscKeyPress || !d->receivedEscKeyPress)
2347 d->acceptedEscKeyPress =
false;
2348 d->receivedEscKeyPress =
false;
2355#if QT_CONFIG(wheelevent)
2356void QQuickComboBox::wheelEvent(QWheelEvent *event)
2358 Q_D(QQuickComboBox);
2359 QQuickControl::wheelEvent(event);
2360 if (d->wheelEnabled && !d->isPopupVisible()) {
2361 if (event->angleDelta().y() > 0)
2362 d->decrementCurrentIndex();
2364 d->incrementCurrentIndex();
2369bool QQuickComboBox::event(QEvent *e)
2371 Q_D(QQuickComboBox);
2372 if (e->type() == QEvent::LanguageChange)
2373 d->updateCurrentElements();
2374 return QQuickControl::event(e);
2377void QQuickComboBox::componentComplete()
2379 Q_D(QQuickComboBox);
2380 d->executeIndicator(
true);
2381 QQuickControl::componentComplete();
2383 d->executePopup(
true);
2385 if (d->delegateModel && d->ownModel)
2386 static_cast<QQmlDelegateModel *>(d->delegateModel)->componentComplete();
2389 if (d->currentElementCriteria == QQuickComboBoxPrivate::CurrentElementCriteria::None && d->currentIndex == -1)
2390 d->setCurrentIndex(0);
2392 d->updateCurrentElements();
2396 if (!d->hasCalculatedWidestText)
2397 d->maybeUpdateImplicitContentWidth();
2401void QQuickComboBox::itemChange(QQuickItem::ItemChange change,
const QQuickItem::ItemChangeData &value)
2403 Q_D(QQuickComboBox);
2404 QQuickControl::itemChange(change, value);
2405 if (change == ItemVisibleHasChanged && !value.boolValue) {
2406 d->hidePopup(
false);
2411void QQuickComboBox::fontChange(
const QFont &newFont,
const QFont &oldFont)
2413 Q_D(QQuickComboBox);
2414 QQuickControl::fontChange(newFont, oldFont);
2415 d->maybeUpdateImplicitContentWidth();
2418void QQuickComboBox::contentItemChange(QQuickItem *newItem, QQuickItem *oldItem)
2420 Q_D(QQuickComboBox);
2422 oldItem->removeEventFilter(
this);
2423 if (QQuickTextInput *oldInput = qobject_cast<QQuickTextInput *>(oldItem)) {
2424 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
2425 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
2426 disconnect(oldInput, &QQuickTextInput::inputMethodComposingChanged,
this, &QQuickComboBox::inputMethodComposingChanged);
2427 QObjectPrivate::disconnect(oldInput, &QQuickTextInput::acceptableInputChanged, d, &QQuickComboBoxPrivate::updateAcceptableInput);
2430 if (newItem && isEditable()) {
2431 newItem->installEventFilter(
this);
2432 if (QQuickTextInput *newInput = qobject_cast<QQuickTextInput *>(newItem)) {
2433 QObjectPrivate::connect(newInput, &QQuickTextInput::accepted, d, &QQuickComboBoxPrivate::acceptInput);
2434 QObjectPrivate::connect(newInput, &QQuickTextInput::textChanged, d, &QQuickComboBoxPrivate::updateEditText);
2435 connect(newInput, &QQuickTextInput::inputMethodComposingChanged,
this, &QQuickComboBox::inputMethodComposingChanged);
2436 QObjectPrivate::connect(newInput, &QQuickTextInput::acceptableInputChanged, d, &QQuickComboBoxPrivate::updateAcceptableInput);
2438#if QT_CONFIG(cursor)
2439 newItem->setCursor(Qt::IBeamCursor);
2443 d->updateAcceptableInput();
2446void QQuickComboBox::localeChange(
const QLocale &newLocale,
const QLocale &oldLocale)
2448 QQuickControl::localeChange(newLocale, oldLocale);
2449#if QT_CONFIG(validator)
2450 if (QValidator *v = validator())
2451 v->setLocale(newLocale);
2455QFont QQuickComboBox::defaultFont()
const
2457 return QQuickTheme::font(QQuickTheme::ComboBox);
2460#if QT_CONFIG(accessibility)
2461QAccessible::Role QQuickComboBox::accessibleRole()
const
2463 return QAccessible::ComboBox;
2466void QQuickComboBox::accessibilityActiveChanged(
bool active)
2468 Q_D(QQuickComboBox);
2469 QQuickControl::accessibilityActiveChanged(active);
2472 maybeSetAccessibleName(d->hasDisplayText ? d->displayText : d->currentText);
2473 setAccessibleProperty(
"editable", isEditable());
2480#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)
std::optional< QQuickText::TextFormat > textFormat
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()
bool textFormatWarningDisplayed
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()