191 for (
int role : roles) {
192 if (role == Qt::AccessibleTextRole
193 || (role == Qt::DisplayRole
194 && index.data(Qt::AccessibleTextRole).toString().isEmpty())) {
195 QAccessibleEvent event(q, QAccessible::NameChanged);
196 event.setChild(childIndex);
197 QAccessible::updateAccessibility(&event);
198 }
else if (role == Qt::AccessibleDescriptionRole) {
199 QAccessibleEvent event(q, QAccessible::DescriptionChanged);
200 event.setChild(childIndex);
201 QAccessible::updateAccessibility(&event);
202 }
else if (role == Qt::CheckStateRole) {
203 QAccessible::State state;
204 state.checked =
true;
205 QAccessibleStateChangeEvent event(q, state);
206 event.setChild(childIndex);
207 QAccessible::updateAccessibility(&event);
213#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
216void QAbstractItemViewPrivate::scrollerStateChanged()
218 Q_Q(QAbstractItemView);
220 if (QScroller *scroller = QScroller::scroller(viewport)) {
221 switch (scroller->state()) {
222 case QScroller::Pressed:
224 if (q->selectionModel()) {
225 oldSelection = q->selectionModel()->selection();
226 oldCurrent = q->selectionModel()->currentIndex();
230 case QScroller::Dragging:
232 if (q->selectionModel()) {
233 q->selectionModel()->select(oldSelection, QItemSelectionModel::ClearAndSelect);
235 const bool wasAutoScroll = autoScroll;
237 q->selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
238 autoScroll = wasAutoScroll;
243 oldSelection = QItemSelection();
244 oldCurrent = QModelIndex();
252void QAbstractItemViewPrivate::delegateSizeHintChanged(
const QModelIndex &index)
254 Q_Q(QAbstractItemView);
256 if (!model->checkIndex(index))
257 qCWarning(lcAIV,
"Delegate size hint changed for a model index that does not belong to this view");
259 QMetaObject::invokeMethod(q, &QAbstractItemView::doItemsLayout, Qt::QueuedConnection);
262void QAbstractItemViewPrivate::connectDelegate(QAbstractItemDelegate *delegate)
266 Q_Q(QAbstractItemView);
267 QObject::connect(delegate, &QAbstractItemDelegate::closeEditor,
268 q, &QAbstractItemView::closeEditor);
269 QObject::connect(delegate, &QAbstractItemDelegate::commitData,
270 q, &QAbstractItemView::commitData);
271 QObjectPrivate::connect(delegate, &QAbstractItemDelegate::sizeHintChanged,
272 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
275void QAbstractItemViewPrivate::disconnectDelegate(QAbstractItemDelegate *delegate)
279 Q_Q(QAbstractItemView);
280 QObject::disconnect(delegate, &QAbstractItemDelegate::closeEditor,
281 q, &QAbstractItemView::closeEditor);
282 QObject::disconnect(delegate, &QAbstractItemDelegate::commitData,
283 q, &QAbstractItemView::commitData);
284 QObjectPrivate::disconnect(delegate, &QAbstractItemDelegate::sizeHintChanged,
285 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
288void QAbstractItemViewPrivate::disconnectAll()
290 Q_Q(QAbstractItemView);
291 for (QMetaObject::Connection &connection : modelConnections)
292 QObject::disconnect(connection);
293 for (QMetaObject::Connection &connection : scrollbarConnections)
294 QObject::disconnect(connection);
295 disconnectDelegate(itemDelegate);
296 for (QAbstractItemDelegate *delegate : std::as_const(rowDelegates))
297 disconnectDelegate(delegate);
298 for (QAbstractItemDelegate *delegate : std::as_const(columnDelegates))
299 disconnectDelegate(delegate);
300 if (model && selectionModel) {
301 QObject::disconnect(model, &QAbstractItemModel::destroyed,
302 selectionModel, &QItemSelectionModel::deleteLater);
304 if (selectionModel) {
305 QObject::disconnect(selectionModel, &QItemSelectionModel::selectionChanged,
306 q, &QAbstractItemView::selectionChanged);
307 QObject::disconnect(selectionModel, &QItemSelectionModel::currentChanged,
308 q, &QAbstractItemView::currentChanged);
310 for (
const auto &info : std::as_const(indexEditorHash)) {
311 if (!info.isStatic && info.widget)
312 QObject::disconnect(info.widget, &QWidget::destroyed, q, &QAbstractItemView::editorDestroyed);
314#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
315 QObject::disconnect(scollerConnection);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
456
457
458
459
460
461
464
465
466
467
468
469
470
471
472
473
477
478
479
480
481
482
483
484
485
486
487
488
489
490
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
511
512
513
514
515
516
517
518
519
520
521
522
523
526
527
528
529
530
531
532
533
534
535
538
539
540
541
542
543
544
545
546
547
548
551
552
553
554
555
556
557
560
561
562
563
564
565
566
567
570
571
572
573
574
575
576
577
578
581
582
583
584
585
586
587
588
591
592
593
594
595
596
597
600
601
602
603
604
605
606
607
608
609
610
613
614
615
616
617
618
619
620
623
624
625
626
627
628
629
630
633
634
635
636
637
638
639
640
643
644
645
646
647
648
649
650
653
654
655
656
657
658
659
660
663
664
665
666
667
668
669
670
671
672
675
676
677
678
679
680
681
682
683
684
685
686
689
690
691
692
693
694
695
696
697
700
701
702QAbstractItemView::QAbstractItemView(QWidget *parent)
703 : QAbstractScrollArea(*(
new QAbstractItemViewPrivate), parent)
709
710
711QAbstractItemView::QAbstractItemView(QAbstractItemViewPrivate &dd, QWidget *parent)
712 : QAbstractScrollArea(dd, parent)
718
719
720QAbstractItemView::~QAbstractItemView()
722 Q_D(QAbstractItemView);
724 d->delayedReset.stop();
725 d->updateTimer.stop();
726 d->delayedEditing.stop();
727 d->delayedAutoScroll.stop();
728 d->autoScrollTimer.stop();
729 d->delayedLayout.stop();
730 d->fetchMoreTimer.stop();
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754void QAbstractItemView::setModel(QAbstractItemModel *model)
756 Q_D(QAbstractItemView);
757 if (model == d->model)
759 if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
760 for (QMetaObject::Connection &connection : d->modelConnections)
761 disconnect(connection);
763 d->model = (model ? model : QAbstractItemModelPrivate::staticEmptyModel());
765 if (d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
766 d->modelConnections = {
767 QObjectPrivate::connect(d->model, &QAbstractItemModel::destroyed,
768 d, &QAbstractItemViewPrivate::modelDestroyed),
769 QObject::connect(d->model, &QAbstractItemModel::dataChanged,
770 this, &QAbstractItemView::dataChanged),
771 QObjectPrivate::connect(d->model, &QAbstractItemModel::headerDataChanged,
772 d, &QAbstractItemViewPrivate::headerDataChanged),
773 QObject::connect(d->model, &QAbstractItemModel::rowsInserted,
774 this, &QAbstractItemView::rowsInserted),
775 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsInserted,
776 d, &QAbstractItemViewPrivate::rowsInserted),
777 QObject::connect(d->model, &QAbstractItemModel::rowsAboutToBeRemoved,
778 this, &QAbstractItemView::rowsAboutToBeRemoved),
779 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsRemoved,
780 d, &QAbstractItemViewPrivate::rowsRemoved),
781 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsMoved,
782 d, &QAbstractItemViewPrivate::rowsMoved),
783 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeRemoved,
784 d, &QAbstractItemViewPrivate::columnsAboutToBeRemoved),
785 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsRemoved,
786 d, &QAbstractItemViewPrivate::columnsRemoved),
787 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsInserted,
788 d, &QAbstractItemViewPrivate::columnsInserted),
789 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsMoved,
790 d, &QAbstractItemViewPrivate::columnsMoved),
791 QObject::connect(d->model, &QAbstractItemModel::modelReset,
792 this, &QAbstractItemView::reset),
793 QObjectPrivate::connect(d->model, &QAbstractItemModel::layoutChanged,
794 d, &QAbstractItemViewPrivate::layoutChanged),
798 QItemSelectionModel *selection_model =
new QItemSelectionModel(d->model,
this);
799 connect(d->model, &QAbstractItemModel::destroyed,
800 selection_model, &QItemSelectionModel::deleteLater);
801 setSelectionModel(selection_model);
807
808
809QAbstractItemModel *QAbstractItemView::model()
const
811 Q_D(
const QAbstractItemView);
812 return (d->model == QAbstractItemModelPrivate::staticEmptyModel() ?
nullptr : d->model);
816
817
818
819
820
821
822
823
824
825
826
827
828
829void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
832 Q_ASSERT(selectionModel);
833 Q_D(QAbstractItemView);
835 if (Q_UNLIKELY(selectionModel->model() != d->model)) {
836 qCWarning(lcAIV,
"QAbstractItemView::setSelectionModel() failed: "
837 "Trying to set a selection model, which works on "
838 "a different model than the view.");
842 QItemSelection oldSelection;
843 QModelIndex oldCurrentIndex;
845 if (d->selectionModel) {
846 if (d->selectionModel->model() == selectionModel->model()) {
847 oldSelection = d->selectionModel->selection();
848 oldCurrentIndex = d->selectionModel->currentIndex();
850 disconnect(d->selectionModel, &QItemSelectionModel::selectionChanged,
851 this, &QAbstractItemView::selectionChanged);
852 disconnect(d->selectionModel, &QItemSelectionModel::currentChanged,
853 this, &QAbstractItemView::currentChanged);
856 d->selectionModel = selectionModel;
858 if (d->selectionModel) {
859 connect(d->selectionModel, &QItemSelectionModel::selectionChanged,
860 this, &QAbstractItemView::selectionChanged);
861 connect(d->selectionModel, &QItemSelectionModel::currentChanged,
862 this, &QAbstractItemView::currentChanged);
864 selectionChanged(d->selectionModel->selection(), oldSelection);
865 currentChanged(d->selectionModel->currentIndex(), oldCurrentIndex);
870
871
872
873
874QItemSelectionModel* QAbstractItemView::selectionModel()
const
876 Q_D(
const QAbstractItemView);
877 return d->selectionModel;
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
897 Q_D(QAbstractItemView);
898 if (delegate == d->itemDelegate)
901 if (d->itemDelegate) {
902 if (d->delegateRefCount(d->itemDelegate) == 1)
903 d->disconnectDelegate(d->itemDelegate);
907 if (d->delegateRefCount(delegate) == 0)
908 d->connectDelegate(delegate);
910 d->itemDelegate = delegate;
911 viewport()->update();
912 d->doDelayedItemsLayout();
916
917
918
919
920
921QAbstractItemDelegate *QAbstractItemView::itemDelegate()
const
923 return d_func()->itemDelegate;
927
928
929QVariant QAbstractItemView::inputMethodQuery(Qt::InputMethodQuery query)
const
931 Q_D(
const QAbstractItemView);
932 const QModelIndex current = currentIndex();
934 if (current.isValid()) {
935 if (QWidget *currentEditor;
936 d->waitForIMCommit && (currentEditor = d->editorForIndex(current).widget)) {
939 result = currentEditor->inputMethodQuery(query);
940 if (result.typeId() == QMetaType::QRect) {
941 const QRect editorRect = result.value<QRect>();
942 result = QRect(currentEditor->mapTo(
this, editorRect.topLeft()), editorRect.size());
944 }
else if (query == Qt::ImCursorRectangle) {
945 const QRect visRect = visualRect(current);
946 result = QRect(d->viewport->mapTo(
this, visRect.topLeft()), visRect.size());
949 if (!result.isValid())
950 result = QAbstractScrollArea::inputMethodQuery(query);
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973void QAbstractItemView::setItemDelegateForRow(
int row, QAbstractItemDelegate *delegate)
975 Q_D(QAbstractItemView);
976 if (QAbstractItemDelegate *rowDelegate = d->rowDelegates.value(row,
nullptr)) {
977 if (d->delegateRefCount(rowDelegate) == 1)
978 d->disconnectDelegate(rowDelegate);
979 d->rowDelegates.remove(row);
982 if (d->delegateRefCount(delegate) == 0)
983 d->connectDelegate(delegate);
984 d->rowDelegates.insert(row, delegate);
986 viewport()->update();
987 d->doDelayedItemsLayout();
991
992
993
994
995
996
997QAbstractItemDelegate *QAbstractItemView::itemDelegateForRow(
int row)
const
999 Q_D(
const QAbstractItemView);
1000 return d->rowDelegates.value(row,
nullptr);
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021void QAbstractItemView::setItemDelegateForColumn(
int column, QAbstractItemDelegate *delegate)
1023 Q_D(QAbstractItemView);
1024 if (QAbstractItemDelegate *columnDelegate = d->columnDelegates.value(column,
nullptr)) {
1025 if (d->delegateRefCount(columnDelegate) == 1)
1026 d->disconnectDelegate(columnDelegate);
1027 d->columnDelegates.remove(column);
1030 if (d->delegateRefCount(delegate) == 0)
1031 d->connectDelegate(delegate);
1032 d->columnDelegates.insert(column, delegate);
1034 viewport()->update();
1035 d->doDelayedItemsLayout();
1039
1040
1041
1042
1043
1044
1045QAbstractItemDelegate *QAbstractItemView::itemDelegateForColumn(
int column)
const
1047 Q_D(
const QAbstractItemView);
1048 return d->columnDelegates.value(column,
nullptr);
1052
1053
1054
1055
1056
1059
1060
1061
1062
1063
1064
1065
1066QAbstractItemDelegate *QAbstractItemView::itemDelegateForIndex(
const QModelIndex &index)
const
1068 Q_D(
const QAbstractItemView);
1069 return d->delegateForIndex(index);
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082void QAbstractItemView::setSelectionMode(SelectionMode mode)
1084 Q_D(QAbstractItemView);
1085 d->selectionMode = mode;
1088QAbstractItemView::SelectionMode QAbstractItemView::selectionMode()
const
1090 Q_D(
const QAbstractItemView);
1091 return d->selectionMode;
1095
1096
1097
1098
1099
1100
1101
1102
1104void QAbstractItemView::setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
1106 Q_D(QAbstractItemView);
1107 d->selectionBehavior = behavior;
1110QAbstractItemView::SelectionBehavior QAbstractItemView::selectionBehavior()
const
1112 Q_D(
const QAbstractItemView);
1113 return d->selectionBehavior;
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130void QAbstractItemView::setCurrentIndex(
const QModelIndex &index)
1132 Q_D(QAbstractItemView);
1133 if (d->selectionModel && (!index.isValid() || d->isIndexEnabled(index))) {
1134 QItemSelectionModel::SelectionFlags command = selectionCommand(index,
nullptr);
1135 d->selectionModel->setCurrentIndex(index, command);
1136 d->currentIndexSet =
true;
1141
1142
1143
1144
1145QModelIndex QAbstractItemView::currentIndex()
const
1147 Q_D(
const QAbstractItemView);
1148 return d->selectionModel ? d->selectionModel->currentIndex() : QModelIndex();
1153
1154
1155
1156
1157
1158
1159
1160
1161void QAbstractItemView::reset()
1163 Q_D(QAbstractItemView);
1164 d->delayedReset.stop();
1168 const auto copy = d->indexEditorHash;
1169 for (
const auto &[index, info] : copy.asKeyValueRange()) {
1171 d->releaseEditor(info.widget.data(), d->indexForEditor(info.widget.data()));
1173 d->editorIndexHash.clear();
1174 d->indexEditorHash.clear();
1175 d->persistent.clear();
1176 d->currentIndexSet =
false;
1178 setRootIndex(QModelIndex());
1179 if (d->selectionModel)
1180 d->selectionModel->reset();
1181#if QT_CONFIG(accessibility)
1182 if (QAccessible::isActive()) {
1183 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1184 QAccessible::updateAccessibility(&accessibleEvent);
1187 d->updateGeometry();
1191
1192
1193
1194
1195void QAbstractItemView::setRootIndex(
const QModelIndex &index)
1197 Q_D(QAbstractItemView);
1198 if (Q_UNLIKELY(index.isValid() && index.model() != d->model)) {
1199 qWarning(
"QAbstractItemView::setRootIndex failed : index must be from the currently set model");
1203#if QT_CONFIG(accessibility)
1204 if (QAccessible::isActive()) {
1205 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1206 QAccessible::updateAccessibility(&accessibleEvent);
1209 d->doDelayedItemsLayout();
1210 d->updateGeometry();
1214
1215
1216
1217
1218
1219QModelIndex QAbstractItemView::rootIndex()
const
1221 return QModelIndex(d_func()->root);
1225
1226
1227
1228
1229
1230
1231void QAbstractItemView::selectAll()
1233 Q_D(QAbstractItemView);
1234 const SelectionMode mode = d->selectionMode;
1236 case MultiSelection:
1237 case ExtendedSelection:
1238 d->selectAll(QItemSelectionModel::ClearAndSelect
1239 | d->selectionBehaviorFlags());
1242 case ContiguousSelection:
1243 if (d->model->hasChildren(d->root))
1244 d->selectAll(selectionCommand(d->model->index(0, 0, d->root)));
1246 case SingleSelection:
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263void QAbstractItemView::edit(
const QModelIndex &index)
1265 Q_D(QAbstractItemView);
1266 if (Q_UNLIKELY(!d->isIndexValid(index)))
1267 qCWarning(lcAIV,
"edit: index was invalid");
1268 if (Q_UNLIKELY(!edit(index, AllEditTriggers,
nullptr)))
1269 qCWarning(lcAIV,
"edit: editing failed");
1273
1274
1275
1276
1277void QAbstractItemView::clearSelection()
1279 Q_D(QAbstractItemView);
1280 if (d->selectionModel)
1281 d->selectionModel->clearSelection();
1285
1286
1287
1288
1289
1290void QAbstractItemView::doItemsLayout()
1292 Q_D(QAbstractItemView);
1293 d->interruptDelayedItemsLayout();
1295 d->viewport->update();
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313void QAbstractItemView::setEditTriggers(EditTriggers actions)
1315 Q_D(QAbstractItemView);
1316 d->editTriggers = actions;
1319QAbstractItemView::EditTriggers QAbstractItemView::editTriggers()
const
1321 Q_D(
const QAbstractItemView);
1322 return d->editTriggers;
1326
1327
1328
1329
1330
1331
1332
1334void QAbstractItemView::setVerticalScrollMode(ScrollMode mode)
1336 Q_D(QAbstractItemView);
1337 d->verticalScrollModeSet =
true;
1338 if (mode == d->verticalScrollMode)
1340 QModelIndex topLeft = indexAt(QPoint(0, 0));
1341 d->verticalScrollMode = mode;
1342 if (mode == ScrollPerItem)
1343 verticalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1345 verticalScrollBar()->setSingleStep(-1);
1347 scrollTo(topLeft, QAbstractItemView::PositionAtTop);
1350QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode()
const
1352 Q_D(
const QAbstractItemView);
1353 return d->verticalScrollMode;
1356void QAbstractItemView::resetVerticalScrollMode()
1358 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1359 setVerticalScrollMode(sm);
1360 d_func()->verticalScrollModeSet =
false;
1364
1365
1366
1367
1368
1369
1370
1372void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode)
1374 Q_D(QAbstractItemView);
1375 d->horizontalScrollModeSet =
true;
1376 if (mode == d->horizontalScrollMode)
1378 d->horizontalScrollMode = mode;
1379 if (mode == ScrollPerItem)
1380 horizontalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1382 horizontalScrollBar()->setSingleStep(-1);
1386QAbstractItemView::ScrollMode QAbstractItemView::horizontalScrollMode()
const
1388 Q_D(
const QAbstractItemView);
1389 return d->horizontalScrollMode;
1392void QAbstractItemView::resetHorizontalScrollMode()
1394 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1395 setHorizontalScrollMode(sm);
1396 d_func()->horizontalScrollModeSet =
false;
1399#if QT_CONFIG(draganddrop)
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420void QAbstractItemView::setDragDropOverwriteMode(
bool overwrite)
1422 Q_D(QAbstractItemView);
1423 d->overwrite = overwrite;
1426bool QAbstractItemView::dragDropOverwriteMode()
const
1428 Q_D(
const QAbstractItemView);
1429 return d->overwrite;
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1447void QAbstractItemView::setAutoScroll(
bool enable)
1449 Q_D(QAbstractItemView);
1450 d->autoScroll = enable;
1453bool QAbstractItemView::hasAutoScroll()
const
1455 Q_D(
const QAbstractItemView);
1456 return d->autoScroll;
1460
1461
1462
1463
1464
1465
1466void QAbstractItemView::setAutoScrollMargin(
int margin)
1468 Q_D(QAbstractItemView);
1469 d->autoScrollMargin = margin;
1472int QAbstractItemView::autoScrollMargin()
const
1474 Q_D(
const QAbstractItemView);
1475 return d->autoScrollMargin;
1479
1480
1481
1483void QAbstractItemView::setTabKeyNavigation(
bool enable)
1485 Q_D(QAbstractItemView);
1486 d->tabKeyNavigation = enable;
1489bool QAbstractItemView::tabKeyNavigation()
const
1491 Q_D(
const QAbstractItemView);
1492 return d->tabKeyNavigation;
1496
1497
1498
1499QSize QAbstractItemView::viewportSizeHint()
const
1501 return QAbstractScrollArea::viewportSizeHint();
1504#if QT_CONFIG(draganddrop)
1506
1507
1508
1509
1510
1512void QAbstractItemView::setDropIndicatorShown(
bool enable)
1514 Q_D(QAbstractItemView);
1515 d->showDropIndicator = enable;
1518bool QAbstractItemView::showDropIndicator()
const
1520 Q_D(
const QAbstractItemView);
1521 return d->showDropIndicator;
1525
1526
1527
1528
1529
1531void QAbstractItemView::setDragEnabled(
bool enable)
1533 Q_D(QAbstractItemView);
1534 d->dragEnabled = enable;
1537bool QAbstractItemView::dragEnabled()
const
1539 Q_D(
const QAbstractItemView);
1540 return d->dragEnabled;
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1563
1564
1565
1566
1567
1568void QAbstractItemView::setDragDropMode(DragDropMode behavior)
1570 Q_D(QAbstractItemView);
1571 d->dragDropMode = behavior;
1572 setDragEnabled(behavior == DragOnly || behavior == DragDrop || behavior == InternalMove);
1573 setAcceptDrops(behavior == DropOnly || behavior == DragDrop || behavior == InternalMove);
1576QAbstractItemView::DragDropMode QAbstractItemView::dragDropMode()
const
1578 Q_D(
const QAbstractItemView);
1579 DragDropMode setBehavior = d->dragDropMode;
1580 if (!dragEnabled() && !acceptDrops())
1583 if (dragEnabled() && !acceptDrops())
1586 if (!dragEnabled() && acceptDrops())
1589 if (dragEnabled() && acceptDrops()) {
1590 if (setBehavior == InternalMove)
1600
1601
1602
1603
1604
1605
1606
1607
1608void QAbstractItemView::setDefaultDropAction(Qt::DropAction dropAction)
1610 Q_D(QAbstractItemView);
1611 d->defaultDropAction = dropAction;
1614Qt::DropAction QAbstractItemView::defaultDropAction()
const
1616 Q_D(
const QAbstractItemView);
1617 return d->defaultDropAction;
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632void QAbstractItemView::setAlternatingRowColors(
bool enable)
1634 Q_D(QAbstractItemView);
1635 d->alternatingColors = enable;
1637 d->viewport->update();
1640bool QAbstractItemView::alternatingRowColors()
const
1642 Q_D(
const QAbstractItemView);
1643 return d->alternatingColors;
1647
1648
1649
1650
1651
1652
1653void QAbstractItemView::setIconSize(
const QSize &size)
1655 Q_D(QAbstractItemView);
1656 if (size == d->iconSize)
1659 d->doDelayedItemsLayout();
1660 emit iconSizeChanged(size);
1663QSize QAbstractItemView::iconSize()
const
1665 Q_D(
const QAbstractItemView);
1670
1671
1672
1673
1674
1675
1676void QAbstractItemView::setTextElideMode(Qt::TextElideMode mode)
1678 Q_D(QAbstractItemView);
1679 d->textElideMode = mode;
1682Qt::TextElideMode QAbstractItemView::textElideMode()
const
1684 return d_func()->textElideMode;
1688
1689
1690bool QAbstractItemView::focusNextPrevChild(
bool next)
1692 Q_D(QAbstractItemView);
1693 if (d->tabKeyNavigation && isVisible() && isEnabled() && d->viewport->isEnabled()) {
1694 QKeyEvent event(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier);
1695 keyPressEvent(&event);
1696 if (event.isAccepted())
1699 return QAbstractScrollArea::focusNextPrevChild(next);
1703
1704
1705bool QAbstractItemView::event(QEvent *event)
1707 Q_D(QAbstractItemView);
1708 switch (event->type()) {
1712 d->executePostedLayout();
1715 d->executePostedLayout();
1716 if (d->shouldScrollToCurrentOnShow) {
1717 d->shouldScrollToCurrentOnShow =
false;
1718 const QModelIndex current = currentIndex();
1719 if (current.isValid() && (d->state == QAbstractItemView::EditingState || d->autoScroll))
1723 case QEvent::LocaleChange:
1724 viewport()->update();
1726 case QEvent::LayoutDirectionChange:
1727 case QEvent::ApplicationLayoutDirectionChange:
1730 case QEvent::StyleChange:
1732 if (!d->verticalScrollModeSet)
1733 resetVerticalScrollMode();
1734 if (!d->horizontalScrollModeSet)
1735 resetHorizontalScrollMode();
1737 case QEvent::FocusOut:
1738 d->checkPersistentEditorFocus();
1740 case QEvent::FontChange:
1741 d->doDelayedItemsLayout();
1746 return QAbstractScrollArea::event(event);
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760bool QAbstractItemView::viewportEvent(QEvent *event)
1762 Q_D(QAbstractItemView);
1763 switch (event->type()) {
1768 d->executePostedLayout();
1770 case QEvent::HoverMove:
1771 case QEvent::HoverEnter:
1772 d->setHoverIndex(indexAt(
static_cast<QHoverEvent*>(event)->position().toPoint()));
1774 case QEvent::HoverLeave:
1775 d->setHoverIndex(QModelIndex());
1778 d->viewportEnteredNeeded =
true;
1781 d->setHoverIndex(QModelIndex());
1782 #if QT_CONFIG(statustip)
1783 if (d->shouldClearStatusTip && d->parent) {
1785 QStatusTipEvent tip(empty);
1786 QCoreApplication::sendEvent(d->parent, &tip);
1787 d->shouldClearStatusTip =
false;
1790 d->enteredIndex = QModelIndex();
1792 case QEvent::ToolTip:
1793 case QEvent::QueryWhatsThis:
1794 case QEvent::WhatsThis: {
1795 QHelpEvent *he =
static_cast<QHelpEvent*>(event);
1796 const QModelIndex index = indexAt(he->pos());
1797 QStyleOptionViewItem option;
1798 initViewItemOption(&option);
1799 option.rect = visualRect(index);
1800 option.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
1802 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
1805 return delegate->helpEvent(he,
this, option, index);
1807 case QEvent::FontChange:
1808 d->doDelayedItemsLayout();
1810 case QEvent::WindowActivate:
1811 case QEvent::WindowDeactivate:
1812 d->viewport->update();
1814 case QEvent::ScrollPrepare:
1815 executeDelayedItemsLayout();
1816#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
1817 d->scollerConnection = QObjectPrivate::connect(
1818 QScroller::scroller(d->viewport), &QScroller::stateChanged,
1819 d, &QAbstractItemViewPrivate::scrollerStateChanged,
1820 Qt::UniqueConnection);
1827 return QAbstractScrollArea::viewportEvent(event);
1831
1832
1833
1834
1835void QAbstractItemView::mousePressEvent(QMouseEvent *event)
1837 Q_D(QAbstractItemView);
1838 d->releaseFromDoubleClick =
false;
1839 d->delayedAutoScroll.stop();
1840 QPoint pos = event->position().toPoint();
1841 QPersistentModelIndex index = indexAt(pos);
1844 d->pressClosedEditor = d->pressClosedEditorWatcher.isActive() && d->lastEditedIndex == index;
1846 if (!d->selectionModel || (d->state == EditingState && d->hasEditor(index)))
1849 d->pressedAlreadySelected = d->selectionModel->isSelected(index);
1850 d->pressedIndex = index;
1851 d->pressedModifiers = event->modifiers();
1852 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1853 d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid();
1854 QPoint offset = d->offset();
1855 d->draggedPosition = pos + offset;
1857#if QT_CONFIG(draganddrop)
1860 d->pressedPosition = d->draggedPosition;
1863 if (!(command & QItemSelectionModel::Current)) {
1864 d->pressedPosition = pos + offset;
1865 d->currentSelectionStartIndex = index;
1867 else if (!d->currentSelectionStartIndex.isValid())
1868 d->currentSelectionStartIndex = currentIndex();
1870 if (edit(index, NoEditTriggers, event))
1873 if (index.isValid() && d->isIndexEnabled(index)) {
1876 bool autoScroll = d->autoScroll;
1877 d->autoScroll =
false;
1878 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1879 d->autoScroll = autoScroll;
1880 if (command.testFlag(QItemSelectionModel::Toggle)) {
1881 command &= ~QItemSelectionModel::Toggle;
1882 d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
1883 command |= d->ctrlDragSelectionFlag;
1886 if (!(command & QItemSelectionModel::Current)) {
1887 setSelection(QRect(pos, QSize(1, 1)), command);
1889 QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
1890 setSelection(rect, command);
1894 emit pressed(index);
1895 if (d->autoScroll) {
1898 d->delayedAutoScroll.start(QApplication::doubleClickInterval()+100,
this);
1903 d->selectionModel->select(QModelIndex(), QItemSelectionModel::Select);
1908
1909
1910
1911
1912void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
1914 Q_D(QAbstractItemView);
1915 QPoint bottomRight = event->position().toPoint();
1917 d->draggedPosition = bottomRight + d->offset();
1919 if (state() == ExpandingState || state() == CollapsingState)
1922#if QT_CONFIG(draganddrop)
1923 if (state() == DraggingState) {
1924 d->maybeStartDrag(bottomRight);
1929 QPersistentModelIndex index = indexAt(bottomRight);
1930 QModelIndex buddy = d->model->buddy(d->pressedIndex);
1931 if ((state() == EditingState && d->hasEditor(buddy))
1932 || edit(index, NoEditTriggers, event))
1935 const QPoint topLeft =
1936 d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight;
1938 d->checkMouseMove(index);
1940#if QT_CONFIG(draganddrop)
1941 if (d->pressedIndex.isValid()
1943 && (state() != DragSelectingState)
1944 && (event->buttons() != Qt::NoButton)
1945 && !d->selectedDraggableIndexes().isEmpty()) {
1946 setState(DraggingState);
1947 d->maybeStartDrag(bottomRight);
1952 if ((event->buttons() & Qt::LeftButton) && d->selectionAllowed(index) && d->selectionModel) {
1953 setState(DragSelectingState);
1954 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1955 if (d->ctrlDragSelectionFlag != QItemSelectionModel::NoUpdate && command.testFlag(QItemSelectionModel::Toggle)) {
1956 command &= ~QItemSelectionModel::Toggle;
1957 command |= d->ctrlDragSelectionFlag;
1961 QRect selectionRect = QRect(topLeft, bottomRight);
1962 setSelection(selectionRect, command);
1965 if (index.isValid() && (index != d->selectionModel->currentIndex()) && d->isIndexEnabled(index))
1966 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1967 else if (d->shouldAutoScroll(event->pos()) && !d->autoScrollTimer.isActive())
1973
1974
1975
1976
1977
1978
1979void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
1981 Q_D(QAbstractItemView);
1982 const bool releaseFromDoubleClick = d->releaseFromDoubleClick;
1983 d->releaseFromDoubleClick =
false;
1985 QPoint pos = event->position().toPoint();
1986 QPersistentModelIndex index = indexAt(pos);
1988 if (state() == EditingState) {
1989 if (d->isIndexValid(index)
1990 && d->isIndexEnabled(index)
1991 && d->sendDelegateEvent(index, event))
1996 bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
1997 bool selectedClicked = click && d->pressedAlreadySelected
1998 && (event->button() == Qt::LeftButton)
1999 && (event->modifiers() == Qt::NoModifier);
2000 EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
2001 const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) :
false;
2003 d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
2005 if (d->selectionModel && d->noSelectionOnMousePress) {
2006 d->noSelectionOnMousePress =
false;
2007 if (!d->pressClosedEditor)
2008 d->selectionModel->select(index, selectionCommand(index, event));
2011 d->pressClosedEditor =
false;
2016 if (event->button() == Qt::LeftButton)
2017 emit clicked(index);
2020 QStyleOptionViewItem option;
2021 initViewItemOption(&option);
2022 if (d->pressedAlreadySelected)
2023 option.state |= QStyle::State_Selected;
2024 if ((d->model->flags(index) & Qt::ItemIsEnabled)
2025 && style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option,
this))
2026 emit activated(index);
2031
2032
2033
2034
2035void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
2037 Q_D(QAbstractItemView);
2039 QModelIndex index = indexAt(event->position().toPoint());
2040 if (!index.isValid()
2041 || !d->isIndexEnabled(index)
2042 || (d->pressedIndex != index)) {
2043 QMouseEvent me(QEvent::MouseButtonPress,
2044 event->position(), event->scenePosition(), event->globalPosition(),
2045 event->button(), event->buttons(), event->modifiers(),
2046 event->source(), event->pointingDevice());
2047 mousePressEvent(&me);
2051 QPersistentModelIndex persistent = index;
2052 emit doubleClicked(persistent);
2053 if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
2054 && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick,
nullptr,
this))
2055 emit activated(persistent);
2056 d->releaseFromDoubleClick =
true;
2059#if QT_CONFIG(draganddrop)
2062
2063
2064
2065
2066
2067
2068void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
2070 if (dragDropMode() == InternalMove
2071 && (event->source() !=
this|| !(event->possibleActions() & Qt::MoveAction)))
2074 if (d_func()->canDrop(event)) {
2076 setState(DraggingState);
2083
2084
2085
2086
2087
2088
2089
2090void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
2092 Q_D(QAbstractItemView);
2093 d->draggedPosition = event->position().toPoint() + d->offset();
2094 if (dragDropMode() == InternalMove
2095 && (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction)))
2101 QModelIndex index = indexAt(event->position().toPoint());
2103 if (!d->droppingOnItself(event, index)
2104 && d->canDrop(event)) {
2106 if (index.isValid() && d->showDropIndicator) {
2107 QRect rect = visualRect(index);
2108 d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
2109 if (d->selectionBehavior == QAbstractItemView::SelectRows
2110 && d->dropIndicatorPosition != OnViewport
2111 && (d->dropIndicatorPosition != OnItem || event->source() ==
this)) {
2112 const int maxCol = d->model->columnCount(index.parent()) - 1;
2113 const auto idx = index.column() > 0 ? index.siblingAtColumn(0) : index;
2114 rect = d->intersectedRect(viewport()->rect(), idx, idx.siblingAtColumn(maxCol));
2116 switch (d->dropIndicatorPosition) {
2118 if (d->isIndexDropEnabled(index.parent())) {
2119 d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0);
2120 event->acceptProposedAction();
2122 d->dropIndicatorRect = QRect();
2126 if (d->isIndexDropEnabled(index.parent())) {
2127 d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0);
2128 event->acceptProposedAction();
2130 d->dropIndicatorRect = QRect();
2134 if (d->isIndexDropEnabled(index)) {
2135 d->dropIndicatorRect = rect;
2136 event->acceptProposedAction();
2138 d->dropIndicatorRect = QRect();
2142 d->dropIndicatorRect = QRect();
2143 if (d->isIndexDropEnabled(rootIndex())) {
2144 event->acceptProposedAction();
2149 d->dropIndicatorRect = QRect();
2150 d->dropIndicatorPosition = OnViewport;
2151 if (d->isIndexDropEnabled(rootIndex())) {
2152 event->acceptProposedAction();
2155 d->viewport->update();
2158 if (d->shouldAutoScroll(event->position().toPoint()))
2163
2164
2165
2166
2167bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event,
const QModelIndex &index)
2169 Q_Q(QAbstractItemView);
2170 Qt::DropAction dropAction = event->dropAction();
2171 if (q->dragDropMode() == QAbstractItemView::InternalMove)
2172 dropAction = Qt::MoveAction;
2173 if (event->source() == q
2174 && event->possibleActions() & Qt::MoveAction
2175 && dropAction == Qt::MoveAction) {
2176 QModelIndexList selectedIndexes = q->selectedIndexes();
2177 QModelIndex child = index;
2178 while (child.isValid() && child != root) {
2179 if (selectedIndexes.contains(child))
2181 child = child.parent();
2188
2189
2190
2191
2192
2193void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *)
2195 Q_D(QAbstractItemView);
2198 d->hover = QModelIndex();
2199 d->viewport->update();
2203
2204
2205
2206
2207
2208
2209void QAbstractItemView::dropEvent(QDropEvent *event)
2211 Q_D(QAbstractItemView);
2212 if (dragDropMode() == InternalMove) {
2213 if (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction))
2220 if (d->dropOn(event, &row, &col, &index)) {
2221 const Qt::DropAction action = dragDropMode() == InternalMove ? Qt::MoveAction : event->dropAction();
2222 if (d->model->dropMimeData(event->mimeData(), action, row, col, index)) {
2223 if (action != event->dropAction()) {
2224 event->setDropAction(action);
2227 event->acceptProposedAction();
2233 d->viewport->update();
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247bool QAbstractItemViewPrivate::dropOn(QDropEvent *event,
int *dropRow,
int *dropCol, QModelIndex *dropIndex)
2249 Q_Q(QAbstractItemView);
2250 if (event->isAccepted())
2255 if (viewport->rect().contains(event->position().toPoint())) {
2256 index = q->indexAt(event->position().toPoint());
2257 if (!index.isValid())
2262 if (model->supportedDropActions() & event->dropAction()) {
2265 if (index != root) {
2266 dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
2267 switch (dropIndicatorPosition) {
2268 case QAbstractItemView::AboveItem:
2270 col = index.column();
2271 index = index.parent();
2273 case QAbstractItemView::BelowItem:
2274 row = index.row() + 1;
2275 col = index.column();
2276 index = index.parent();
2278 case QAbstractItemView::OnItem:
2279 case QAbstractItemView::OnViewport:
2283 dropIndicatorPosition = QAbstractItemView::OnViewport;
2288 if (!droppingOnItself(event, index))
2294QAbstractItemView::DropIndicatorPosition
2295QAbstractItemViewPrivate::position(
const QPoint &pos,
const QRect &rect,
const QModelIndex &index)
const
2297 QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport;
2299 const int margin = qBound(2, qRound(qreal(rect.height()) / 5.5), 12);
2300 if (pos.y() - rect.top() < margin) {
2301 r = QAbstractItemView::AboveItem;
2302 }
else if (rect.bottom() - pos.y() < margin) {
2303 r = QAbstractItemView::BelowItem;
2304 }
else if (rect.contains(pos,
true)) {
2305 r = QAbstractItemView::OnItem;
2308 QRect touchingRect = rect;
2309 touchingRect.adjust(-1, -1, 1, 1);
2310 if (touchingRect.contains(pos,
false)) {
2311 r = QAbstractItemView::OnItem;
2315 if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled)))
2316 r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem;
2324
2325
2326
2327
2328
2329void QAbstractItemView::focusInEvent(QFocusEvent *event)
2331 Q_D(QAbstractItemView);
2332 QAbstractScrollArea::focusInEvent(event);
2334 const QItemSelectionModel* model = selectionModel();
2335 bool currentIndexValid = currentIndex().isValid();
2338 && !d->currentIndexSet
2339 && !currentIndexValid) {
2340 bool autoScroll = d->autoScroll;
2341 d->autoScroll =
false;
2342 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2343 if (index.isValid() && d->isIndexEnabled(index) && event->reason() != Qt::MouseFocusReason) {
2344 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
2345 currentIndexValid =
true;
2347 d->autoScroll = autoScroll;
2350 if (model && currentIndexValid)
2351 setAttribute(Qt::WA_InputMethodEnabled, (currentIndex().flags() & Qt::ItemIsEditable));
2352 else if (!currentIndexValid)
2353 setAttribute(Qt::WA_InputMethodEnabled,
false);
2355 d->viewport->update();
2359
2360
2361
2362
2363
2364void QAbstractItemView::focusOutEvent(QFocusEvent *event)
2366 Q_D(QAbstractItemView);
2367 QAbstractScrollArea::focusOutEvent(event);
2368 d->viewport->update();
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382void QAbstractItemView::keyPressEvent(QKeyEvent *event)
2384 Q_D(QAbstractItemView);
2385 d->delayedAutoScroll.stop();
2387#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
2388 if (event == QKeySequence::Copy) {
2389 const QModelIndex index = currentIndex();
2390 if (index.isValid() && d->model) {
2391 const QVariant variant = d->model->data(index, Qt::DisplayRole);
2392 if (variant.canConvert<QString>())
2393 QGuiApplication::clipboard()->setText(variant.toString());
2399 QPersistentModelIndex newCurrent;
2400 d->moveCursorUpdatedView =
false;
2401 switch (event->key()) {
2403 newCurrent = moveCursor(MoveDown, event->modifiers());
2406 newCurrent = moveCursor(MoveUp, event->modifiers());
2409 newCurrent = moveCursor(MoveLeft, event->modifiers());
2412 newCurrent = moveCursor(MoveRight, event->modifiers());
2415 newCurrent = moveCursor(MoveHome, event->modifiers());
2418 newCurrent = moveCursor(MoveEnd, event->modifiers());
2420 case Qt::Key_PageUp:
2421 newCurrent = moveCursor(MovePageUp, event->modifiers());
2423 case Qt::Key_PageDown:
2424 newCurrent = moveCursor(MovePageDown, event->modifiers());
2427 if (d->tabKeyNavigation)
2428 newCurrent = moveCursor(MoveNext, event->modifiers());
2430 case Qt::Key_Backtab:
2431 if (d->tabKeyNavigation)
2432 newCurrent = moveCursor(MovePrevious, event->modifiers());
2436 QPersistentModelIndex oldCurrent = currentIndex();
2437 if (newCurrent != oldCurrent && newCurrent.isValid() && d->isIndexEnabled(newCurrent)) {
2438 if (!hasFocus() && QApplication::focusWidget() == indexWidget(oldCurrent))
2440 QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
2441 if (command != QItemSelectionModel::NoUpdate
2442 || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection,
nullptr,
this)) {
2444 if (command & QItemSelectionModel::Current) {
2445 d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
2446 if (!d->currentSelectionStartIndex.isValid())
2447 d->currentSelectionStartIndex = oldCurrent;
2448 QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center());
2449 setSelection(rect, command);
2451 d->selectionModel->setCurrentIndex(newCurrent, command);
2452 d->currentSelectionStartIndex = newCurrent;
2453 if (newCurrent.isValid()) {
2455 QRect rect(visualRect(newCurrent).center(), QSize(1, 1));
2456 setSelection(rect, command);
2464 switch (event->key()) {
2472 case Qt::Key_PageUp:
2473 case Qt::Key_PageDown:
2474 case Qt::Key_Escape:
2476 case Qt::Key_Control:
2477 case Qt::Key_Delete:
2478 case Qt::Key_Backspace:
2482 case Qt::Key_Select:
2483 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2484 if (d->selectionModel)
2485 d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
2486 if (event->key() == Qt::Key_Space) {
2487 keyboardSearch(event->text());
2494 case Qt::Key_Return:
2497 if (!edit(currentIndex(), EditKeyPressed, event) && d->editorIndexHash.isEmpty())
2502 if (!edit(currentIndex(), EditKeyPressed, event))
2506 case Qt::Key_Return:
2510 if (state() != EditingState || hasFocus()) {
2511 if (currentIndex().isValid())
2512 emit activated(currentIndex());
2518#ifndef QT_NO_SHORTCUT
2519 if (event == QKeySequence::SelectAll && selectionMode() != NoSelection) {
2525 if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
2526 emit activated(currentIndex());
2530 bool modified = (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
2531 if (!event->text().isEmpty() && !modified && !edit(currentIndex(), AnyKeyPressed, event)) {
2532 keyboardSearch(event->text());
2539 if (d->moveCursorUpdatedView)
2544
2545
2546
2547
2548
2549void QAbstractItemView::resizeEvent(QResizeEvent *event)
2551 QAbstractScrollArea::resizeEvent(event);
2556
2557
2558
2559
2560
2561void QAbstractItemView::timerEvent(QTimerEvent *event)
2563 Q_D(QAbstractItemView);
2564 if (event->timerId() == d->fetchMoreTimer.timerId())
2566 else if (event->timerId() == d->delayedReset.timerId())
2568 else if (event->timerId() == d->autoScrollTimer.timerId())
2570 else if (event->timerId() == d->updateTimer.timerId())
2571 d->updateDirtyRegion();
2572 else if (event->timerId() == d->delayedEditing.timerId()) {
2573 d->delayedEditing.stop();
2574 edit(currentIndex());
2575 }
else if (event->timerId() == d->delayedLayout.timerId()) {
2576 d->delayedLayout.stop();
2578 d->interruptDelayedItemsLayout();
2580 const QModelIndex current = currentIndex();
2581 if (current.isValid() && d->state == QAbstractItemView::EditingState)
2584 }
else if (event->timerId() == d->delayedAutoScroll.timerId()) {
2585 d->delayedAutoScroll.stop();
2588 if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
2589 scrollTo(d->pressedIndex);
2590 }
else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
2591 d->pressClosedEditorWatcher.stop();
2596
2597
2598void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
2600 Q_D(QAbstractItemView);
2607 bool forwardEventToEditor =
false;
2608 const bool commit = !event->commitString().isEmpty();
2609 const bool preediting = !event->preeditString().isEmpty();
2610 if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
2611 if (d->waitForIMCommit) {
2612 if (commit || !preediting) {
2614 d->waitForIMCommit =
false;
2615 QApplication::sendEvent(currentEditor, event);
2617 QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
2619 delegate->setEditorData(currentEditor, currentIndex());
2620 d->selectAllInEditor(currentEditor);
2622 if (currentEditor->focusPolicy() != Qt::NoFocus)
2623 currentEditor->setFocus();
2626 QApplication::sendEvent(currentEditor, event);
2630 }
else if (preediting) {
2632 d->waitForIMCommit =
true;
2634 forwardEventToEditor =
true;
2635 }
else if (!commit) {
2639 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2640 d->waitForIMCommit =
false;
2642 keyboardSearch(event->commitString());
2644 }
else if (QWidget *currentEditor; forwardEventToEditor
2645 && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
2646 QApplication::sendEvent(currentEditor, event);
2650#if QT_CONFIG(draganddrop)
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2670
2671
2672QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition()
const
2674 Q_D(
const QAbstractItemView);
2675 return d->dropIndicatorPosition;
2680
2681
2682
2683
2684
2685
2686QModelIndexList QAbstractItemView::selectedIndexes()
const
2688 Q_D(
const QAbstractItemView);
2689 QModelIndexList indexes;
2690 if (d->selectionModel) {
2691 indexes = d->selectionModel->selectedIndexes();
2692 auto isHidden = [
this](
const QModelIndex &idx) {
2693 return isIndexHidden(idx);
2695 indexes.removeIf(isHidden);
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713bool QAbstractItemView::edit(
const QModelIndex &index, EditTrigger trigger, QEvent *event)
2715 Q_D(QAbstractItemView);
2717 if (!d->isIndexValid(index))
2720 if (QWidget *w = (d->persistent.isEmpty() ?
static_cast<QWidget*>(
nullptr) : d->editorForIndex(index).widget.data())) {
2721 if (w->focusPolicy() == Qt::NoFocus)
2723 if (!d->waitForIMCommit)
2730 if (trigger == DoubleClicked) {
2731 d->delayedEditing.stop();
2732 d->delayedAutoScroll.stop();
2733 }
else if (trigger == CurrentChanged) {
2734 d->delayedEditing.stop();
2738 QPersistentModelIndex safeIndex(index);
2740 if (d->sendDelegateEvent(index, event)) {
2745 if (!safeIndex.isValid()) {
2750 EditTriggers lastTrigger = d->lastTrigger;
2751 d->lastTrigger = trigger;
2753 if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
2756 if (d->delayedEditing.isActive())
2761 if (lastTrigger == DoubleClicked && trigger == SelectedClicked)
2765 if (trigger == SelectedClicked)
2766 d->delayedEditing.start(QApplication::doubleClickInterval(),
this);
2768 d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event :
nullptr);
2774
2775
2776
2777void QAbstractItemView::updateEditorData()
2779 Q_D(QAbstractItemView);
2780 d->updateEditorData(QModelIndex(), QModelIndex());
2784
2785
2786
2787void QAbstractItemView::updateEditorGeometries()
2789 Q_D(QAbstractItemView);
2790 if (d->editorIndexHash.isEmpty())
2792 if (d->delayedPendingLayout) {
2794 d->executePostedLayout();
2797 QStyleOptionViewItem option;
2798 initViewItemOption(&option);
2799 QEditorIndexHash::iterator it = d->editorIndexHash.begin();
2800 QWidgetList editorsToRelease;
2801 QWidgetList editorsToHide;
2802 while (it != d->editorIndexHash.end()) {
2803 QModelIndex index = it.value();
2804 QWidget *editor = it.key();
2805 if (index.isValid() && editor) {
2806 option.rect = visualRect(index);
2807 if (option.rect.isValid()) {
2809 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2811 delegate->updateEditorGeometry(editor, option, index);
2813 editorsToHide << editor;
2817 d->indexEditorHash.remove(it.value());
2818 it = d->editorIndexHash.erase(it);
2819 editorsToRelease << editor;
2825 for (
int i = 0; i < editorsToHide.size(); ++i) {
2826 editorsToHide.at(i)->hide();
2828 for (
int i = 0; i < editorsToRelease.size(); ++i) {
2829 d->releaseEditor(editorsToRelease.at(i));
2834
2835
2836void QAbstractItemView::updateGeometries()
2838 Q_D(QAbstractItemView);
2839 updateEditorGeometries();
2840 d->fetchMoreTimer.start(0,
this);
2841 d->updateGeometry();
2845
2846
2847void QAbstractItemView::verticalScrollbarValueChanged(
int value)
2849 Q_D(QAbstractItemView);
2850 if (verticalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2851 d->model->fetchMore(d->root);
2852 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2853 if (viewport()->rect().contains(posInVp))
2854 d->checkMouseMove(posInVp);
2858
2859
2860void QAbstractItemView::horizontalScrollbarValueChanged(
int value)
2862 Q_D(QAbstractItemView);
2863 if (horizontalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2864 d->model->fetchMore(d->root);
2865 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2866 if (viewport()->rect().contains(posInVp))
2867 d->checkMouseMove(posInVp);
2871
2872
2873void QAbstractItemView::verticalScrollbarAction(
int)
2879
2880
2881void QAbstractItemView::horizontalScrollbarAction(
int)
2887
2888
2889
2890
2891
2892
2893
2895void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
2897 Q_D(QAbstractItemView);
2901 const bool isPersistent = d->persistent.contains(editor);
2902 const QModelIndex index = d->indexForEditor(editor);
2903 if (!index.isValid()) {
2904 if (!editor->isVisible()) {
2913 "QAbstractItemView::closeEditor called with an editor that does not "
2914 "belong to this view");
2918 const bool hadFocus = editor->hasFocus();
2922 d->pressClosedEditorWatcher.start(0,
this);
2923 d->lastEditedIndex = index;
2925 if (!isPersistent) {
2927 QModelIndex index = d->indexForEditor(editor);
2928 editor->removeEventFilter(itemDelegateForIndex(index));
2929 d->removeEditor(editor);
2932 if (focusPolicy() != Qt::NoFocus)
2935 editor->clearFocus();
2937 d->checkPersistentEditorFocus();
2940 QPointer<QWidget> ed = editor;
2941 QCoreApplication::sendPostedEvents(editor, 0);
2944 if (!isPersistent && editor)
2945 d->releaseEditor(editor, index);
2950 QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::NoUpdate;
2951 if (d->selectionMode != NoSelection)
2952 flags = QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
2954 case QAbstractItemDelegate::EditNextItem: {
2955 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2956 if (index.isValid()) {
2957 QPersistentModelIndex persistent(index);
2958 d->selectionModel->setCurrentIndex(persistent, flags);
2960 if (index.flags() & Qt::ItemIsEditable
2961 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2964 case QAbstractItemDelegate::EditPreviousItem: {
2965 QModelIndex index = moveCursor(MovePrevious, Qt::NoModifier);
2966 if (index.isValid()) {
2967 QPersistentModelIndex persistent(index);
2968 d->selectionModel->setCurrentIndex(persistent, flags);
2970 if (index.flags() & Qt::ItemIsEditable
2971 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2974 case QAbstractItemDelegate::SubmitModelCache:
2977 case QAbstractItemDelegate::RevertModelCache:
2986
2987
2988
2989
2990void QAbstractItemView::commitData(QWidget *editor)
2992 Q_D(QAbstractItemView);
2993 if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
2995 QModelIndex index = d->indexForEditor(editor);
2996 if (!index.isValid()) {
2998 "QAbstractItemView::commitData called with an editor that does not belong to "
3002 d->currentlyCommittingEditor = editor;
3003 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
3004 editor->removeEventFilter(delegate);
3005 delegate->setModelData(editor, d->model, index);
3006 editor->installEventFilter(delegate);
3007 d->currentlyCommittingEditor =
nullptr;
3011
3012
3013
3014
3015void QAbstractItemView::editorDestroyed(QObject *editor)
3017 Q_D(QAbstractItemView);
3018 QWidget *w = qobject_cast<QWidget*>(editor);
3020 d->persistent.remove(w);
3021 if (state() == EditingState)
3028
3029
3030
3031
3032
3033
3034
3035void QAbstractItemView::keyboardSearch(
const QString &search)
3037 Q_D(QAbstractItemView);
3038 if (!d->model->rowCount(d->root) || !d->model->columnCount(d->root))
3041 QModelIndex start = currentIndex().isValid() ? currentIndex()
3042 : d->model->index(0, 0, d->root);
3043 bool skipRow =
false;
3044 bool keyboardTimeWasValid = d->keyboardInputTime.isValid();
3045 qint64 keyboardInputTimeElapsed;
3046 if (keyboardTimeWasValid)
3047 keyboardInputTimeElapsed = d->keyboardInputTime.restart();
3049 d->keyboardInputTime.start();
3050 if (search.isEmpty() || !keyboardTimeWasValid
3051 || keyboardInputTimeElapsed > QApplication::keyboardInputInterval()) {
3052 d->keyboardInput = search;
3053 skipRow = currentIndex().isValid();
3055 d->keyboardInput += search;
3059 bool sameKey =
false;
3060 if (d->keyboardInput.size() > 1) {
3061 int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
3062 sameKey = (c == d->keyboardInput.size());
3069 QModelIndex parent = start.parent();
3070 int newRow = (start.row() < d->model->rowCount(parent) - 1) ? start.row() + 1 : 0;
3071 start = d->model->index(newRow, start.column(), parent);
3075 QModelIndex current = start;
3076 QModelIndexList match;
3077 QModelIndex firstMatch;
3078 QModelIndex startMatch;
3079 QModelIndexList previous;
3081 match = d->model->match(current, Qt::DisplayRole, d->keyboardInput, 1,
3082 d->keyboardSearchFlags);
3083 if (match == previous)
3085 firstMatch = match.value(0);
3087 if (firstMatch.isValid()) {
3088 if (d->isIndexEnabled(firstMatch)) {
3089 setCurrentIndex(firstMatch);
3092 int row = firstMatch.row() + 1;
3093 if (row >= d->model->rowCount(firstMatch.parent()))
3095 current = firstMatch.sibling(row, firstMatch.column());
3098 if (!startMatch.isValid())
3099 startMatch = firstMatch;
3100 else if (startMatch == firstMatch)
3103 }
while (current != start && firstMatch.isValid());
3107
3108
3109
3110
3111
3112QSize QAbstractItemView::sizeHintForIndex(
const QModelIndex &index)
const
3114 Q_D(
const QAbstractItemView);
3115 if (!d->isIndexValid(index))
3117 const auto delegate = itemDelegateForIndex(index);
3118 QStyleOptionViewItem option;
3119 initViewItemOption(&option);
3120 return delegate ? delegate->sizeHint(option, index) : QSize();
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139int QAbstractItemView::sizeHintForRow(
int row)
const
3141 Q_D(
const QAbstractItemView);
3143 if (row < 0 || row >= d->model->rowCount(d->root))
3148 QStyleOptionViewItem option;
3149 initViewItemOption(&option);
3151 int colCount = d->model->columnCount(d->root);
3152 for (
int c = 0; c < colCount; ++c) {
3153 const QModelIndex index = d->model->index(row, c, d->root);
3154 if (QWidget *editor = d->editorForIndex(index).widget.data())
3155 height = qMax(height, editor->height());
3156 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3157 height = qMax(height, delegate->sizeHint(option, index).height());
3163
3164
3165
3166
3167
3168
3169
3170int QAbstractItemView::sizeHintForColumn(
int column)
const
3172 Q_D(
const QAbstractItemView);
3174 if (column < 0 || column >= d->model->columnCount(d->root))
3179 QStyleOptionViewItem option;
3180 initViewItemOption(&option);
3182 int rows = d->model->rowCount(d->root);
3183 for (
int r = 0; r < rows; ++r) {
3184 const QModelIndex index = d->model->index(r, column, d->root);
3185 if (QWidget *editor = d->editorForIndex(index).widget.data())
3186 width = qMax(width, editor->sizeHint().width());
3187 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3188 width = qMax(width, delegate->sizeHint(option, index).width());
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210int QAbstractItemView::updateThreshold()
const
3212 Q_D(
const QAbstractItemView);
3213 return d->updateThreshold;
3216void QAbstractItemView::setUpdateThreshold(
int threshold)
3218 Q_D(QAbstractItemView);
3219 if (d->updateThreshold == threshold)
3221 d->updateThreshold = threshold;
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3236Qt::MatchFlags QAbstractItemView::keyboardSearchFlags()
const
3238 Q_D(
const QAbstractItemView);
3239 return d->keyboardSearchFlags;
3242void QAbstractItemView::setKeyboardSearchFlags(Qt::MatchFlags searchFlags)
3244 Q_D(QAbstractItemView);
3245 d->keyboardSearchFlags = searchFlags;
3249
3250
3251
3252
3253
3254void QAbstractItemView::openPersistentEditor(
const QModelIndex &index)
3256 Q_D(QAbstractItemView);
3257 QStyleOptionViewItem options;
3258 initViewItemOption(&options);
3259 options.rect = visualRect(index);
3260 options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
3262 QWidget *editor = d->editor(index, options);
3265 d->persistent.insert(editor);
3270
3271
3272
3273
3274void QAbstractItemView::closePersistentEditor(
const QModelIndex &index)
3276 Q_D(QAbstractItemView);
3277 if (QWidget *editor = d->editorForIndex(index).widget.data()) {
3278 if (index == selectionModel()->currentIndex())
3279 closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
3280 d->persistent.remove(editor);
3281 d->removeEditor(editor);
3282 d->releaseEditor(editor, index);
3287
3288
3289
3290
3291
3292
3293bool QAbstractItemView::isPersistentEditorOpen(
const QModelIndex &index)
const
3295 Q_D(
const QAbstractItemView);
3296 QWidget *editor = d->editorForIndex(index).widget;
3297 return editor && d->persistent.contains(editor);
4066void QAbstractItemView::doAutoScroll()
4069 Q_D(QAbstractItemView);
4070 QScrollBar *verticalScroll = verticalScrollBar();
4071 QScrollBar *horizontalScroll = horizontalScrollBar();
4075 QHeaderView *hv = qobject_cast<QHeaderView*>(
this);
4077 QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
4079 if (hv->orientation() == Qt::Horizontal) {
4080 if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
4081 horizontalScroll = parent->horizontalScrollBar();
4083 if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
4084 verticalScroll = parent->verticalScrollBar();
4089 const int verticalStep = verticalScroll->pageStep();
4090 const int horizontalStep = horizontalScroll->pageStep();
4091 if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
4092 ++d->autoScrollCount;
4094 const int margin = d->autoScrollMargin;
4095 const int verticalValue = verticalScroll->value();
4096 const int horizontalValue = horizontalScroll->value();
4098 const QPoint pos = d->draggedPosition - d->offset();
4099 const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
4102 if (pos.y() - area.top() < margin)
4103 verticalScroll->setValue(verticalValue - d->autoScrollCount);
4104 else if (area.bottom() - pos.y() < margin)
4105 verticalScroll->setValue(verticalValue + d->autoScrollCount);
4106 if (pos.x() - area.left() < margin)
4107 horizontalScroll->setValue(horizontalValue - d->autoScrollCount);
4108 else if (area.right() - pos.x() < margin)
4109 horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
4111 const bool verticalUnchanged = (verticalValue == verticalScroll->value());
4112 const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
4113 if (verticalUnchanged && horizontalUnchanged) {
4116#if QT_CONFIG(draganddrop)
4117 d->dropIndicatorRect = QRect();
4118 d->dropIndicatorPosition = QAbstractItemView::OnViewport;
4121 case QAbstractItemView::DragSelectingState: {
4124 const QPoint globalPos = d->viewport->mapToGlobal(pos);
4125 const QPoint windowPos = window()->mapFromGlobal(globalPos);
4126 QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
4127 Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
4128 Qt::MouseEventSynthesizedByQt);
4129 QApplication::sendEvent(viewport(), &mm);
4132 case QAbstractItemView::DraggingState: {
4138 d->draggedPosition = pos + d->offset();
4144 d->viewport->update();
4248QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
4249 const QModelIndex &index,
const QEvent *event)
const
4251 Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
4252 ?
static_cast<
const QInputEvent*>(event)->modifiers()
4253 : QGuiApplication::keyboardModifiers();
4255 switch (event->type()) {
4256 case QEvent::MouseMove: {
4258 if (modifiers & Qt::ControlModifier)
4259 return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
4262 case QEvent::MouseButtonPress: {
4263 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4264 const bool rightButtonPressed = button & Qt::RightButton;
4265 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4266 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4267 const bool indexIsSelected = selectionModel->isSelected(index);
4268 if ((shiftKeyPressed || controlKeyPressed) && rightButtonPressed)
4269 return QItemSelectionModel::NoUpdate;
4270 if (!shiftKeyPressed && !controlKeyPressed && indexIsSelected)
4271 return QItemSelectionModel::NoUpdate;
4272 if (!index.isValid() && !rightButtonPressed && !shiftKeyPressed && !controlKeyPressed)
4273 return QItemSelectionModel::Clear;
4274 if (!index.isValid())
4275 return QItemSelectionModel::NoUpdate;
4277 if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
4278#if QT_CONFIG(draganddrop)
4279 && dragEnabled && isIndexDragEnabled(index)
4282 return QItemSelectionModel::NoUpdate;
4286 case QEvent::MouseButtonRelease: {
4288 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4289 const bool rightButtonPressed = button & Qt::RightButton;
4290 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4291 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4292 if (((index == pressedIndex && selectionModel->isSelected(index))
4293 || !index.isValid()) && state != QAbstractItemView::DragSelectingState
4294 && !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
4295 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
4296 if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
4297#if QT_CONFIG(draganddrop)
4298 && dragEnabled && isIndexDragEnabled(index)
4303 return QItemSelectionModel::NoUpdate;
4305 case QEvent::KeyPress: {
4307 switch (
static_cast<
const QKeyEvent*>(event)->key()) {
4308 case Qt::Key_Backtab:
4309 modifiers = modifiers & ~Qt::ShiftModifier;
4317 case Qt::Key_PageUp:
4318 case Qt::Key_PageDown:
4320 if (modifiers & Qt::ControlModifier)
4321 return QItemSelectionModel::NoUpdate;
4323 case Qt::Key_Select:
4324 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4326 if (modifiers & Qt::ControlModifier)
4327 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4328 return QItemSelectionModel::Select|selectionBehaviorFlags();
4339 if (modifiers & Qt::ShiftModifier)
4340 return QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4341 if (modifiers & Qt::ControlModifier)
4342 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4343 if (state == QAbstractItemView::DragSelectingState) {
4345 return QItemSelectionModel::Clear|QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4348 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();