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
874
875
876
877
878
879
880QItemSelectionModel* QAbstractItemView::selectionModel()
const
882 Q_D(
const QAbstractItemView);
883 return d->selectionModel;
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
903 Q_D(QAbstractItemView);
904 if (delegate == d->itemDelegate)
907 if (d->itemDelegate) {
908 if (d->delegateRefCount(d->itemDelegate) == 1)
909 d->disconnectDelegate(d->itemDelegate);
913 if (d->delegateRefCount(delegate) == 0)
914 d->connectDelegate(delegate);
916 d->itemDelegate = delegate;
917 viewport()->update();
918 d->doDelayedItemsLayout();
922
923
924
925
926
927QAbstractItemDelegate *QAbstractItemView::itemDelegate()
const
929 return d_func()->itemDelegate;
933
934
935QVariant QAbstractItemView::inputMethodQuery(Qt::InputMethodQuery query)
const
937 Q_D(
const QAbstractItemView);
938 const QModelIndex current = currentIndex();
940 if (current.isValid()) {
941 if (QWidget *currentEditor;
942 d->waitForIMCommit && (currentEditor = d->editorForIndex(current).widget)) {
945 result = currentEditor->inputMethodQuery(query);
946 if (result.typeId() == QMetaType::QRect) {
947 const QRect editorRect = result.value<QRect>();
948 result = QRect(currentEditor->mapTo(
this, editorRect.topLeft()), editorRect.size());
950 }
else if (query == Qt::ImCursorRectangle) {
951 const QRect visRect = visualRect(current);
952 result = QRect(d->viewport->mapTo(
this, visRect.topLeft()), visRect.size());
955 if (!result.isValid())
956 result = QAbstractScrollArea::inputMethodQuery(query);
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979void QAbstractItemView::setItemDelegateForRow(
int row, QAbstractItemDelegate *delegate)
981 Q_D(QAbstractItemView);
982 if (QAbstractItemDelegate *rowDelegate = d->rowDelegates.value(row,
nullptr)) {
983 if (d->delegateRefCount(rowDelegate) == 1)
984 d->disconnectDelegate(rowDelegate);
985 d->rowDelegates.remove(row);
988 if (d->delegateRefCount(delegate) == 0)
989 d->connectDelegate(delegate);
990 d->rowDelegates.insert(row, delegate);
992 viewport()->update();
993 d->doDelayedItemsLayout();
997
998
999
1000
1001
1002
1003QAbstractItemDelegate *QAbstractItemView::itemDelegateForRow(
int row)
const
1005 Q_D(
const QAbstractItemView);
1006 return d->rowDelegates.value(row,
nullptr);
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027void QAbstractItemView::setItemDelegateForColumn(
int column, QAbstractItemDelegate *delegate)
1029 Q_D(QAbstractItemView);
1030 if (QAbstractItemDelegate *columnDelegate = d->columnDelegates.value(column,
nullptr)) {
1031 if (d->delegateRefCount(columnDelegate) == 1)
1032 d->disconnectDelegate(columnDelegate);
1033 d->columnDelegates.remove(column);
1036 if (d->delegateRefCount(delegate) == 0)
1037 d->connectDelegate(delegate);
1038 d->columnDelegates.insert(column, delegate);
1040 viewport()->update();
1041 d->doDelayedItemsLayout();
1045
1046
1047
1048
1049
1050
1051QAbstractItemDelegate *QAbstractItemView::itemDelegateForColumn(
int column)
const
1053 Q_D(
const QAbstractItemView);
1054 return d->columnDelegates.value(column,
nullptr);
1058
1059
1060
1061
1062
1065
1066
1067
1068
1069
1070
1071
1072QAbstractItemDelegate *QAbstractItemView::itemDelegateForIndex(
const QModelIndex &index)
const
1074 Q_D(
const QAbstractItemView);
1075 return d->delegateForIndex(index);
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088void QAbstractItemView::setSelectionMode(SelectionMode mode)
1090 Q_D(QAbstractItemView);
1091 d->selectionMode = mode;
1094QAbstractItemView::SelectionMode QAbstractItemView::selectionMode()
const
1096 Q_D(
const QAbstractItemView);
1097 return d->selectionMode;
1101
1102
1103
1104
1105
1106
1107
1108
1110void QAbstractItemView::setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
1112 Q_D(QAbstractItemView);
1113 d->selectionBehavior = behavior;
1116QAbstractItemView::SelectionBehavior QAbstractItemView::selectionBehavior()
const
1118 Q_D(
const QAbstractItemView);
1119 return d->selectionBehavior;
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136void QAbstractItemView::setCurrentIndex(
const QModelIndex &index)
1138 Q_D(QAbstractItemView);
1139 if (d->selectionModel && (!index.isValid() || d->isIndexEnabled(index))) {
1140 QItemSelectionModel::SelectionFlags command = selectionCommand(index,
nullptr);
1141 d->selectionModel->setCurrentIndex(index, command);
1142 d->currentIndexSet =
true;
1147
1148
1149
1150
1151QModelIndex QAbstractItemView::currentIndex()
const
1153 Q_D(
const QAbstractItemView);
1154 return d->selectionModel ? d->selectionModel->currentIndex() : QModelIndex();
1159
1160
1161
1162
1163
1164
1165
1166
1167void QAbstractItemView::reset()
1169 Q_D(QAbstractItemView);
1170 d->delayedReset.stop();
1174 const auto copy = d->indexEditorHash;
1175 for (
const auto &[index, info] : copy.asKeyValueRange()) {
1177 d->releaseEditor(info.widget.data(), d->indexForEditor(info.widget.data()));
1179 d->editorIndexHash.clear();
1180 d->indexEditorHash.clear();
1181 d->persistent.clear();
1182 d->currentIndexSet =
false;
1184 setRootIndex(QModelIndex());
1185 if (d->selectionModel)
1186 d->selectionModel->reset();
1187#if QT_CONFIG(accessibility)
1188 if (QAccessible::isActive()) {
1189 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1190 QAccessible::updateAccessibility(&accessibleEvent);
1193 d->updateGeometry();
1197
1198
1199
1200
1201void QAbstractItemView::setRootIndex(
const QModelIndex &index)
1203 Q_D(QAbstractItemView);
1204 if (Q_UNLIKELY(index.isValid() && index.model() != d->model)) {
1205 qWarning(
"QAbstractItemView::setRootIndex failed : index must be from the currently set model");
1209#if QT_CONFIG(accessibility)
1210 if (QAccessible::isActive()) {
1211 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1212 QAccessible::updateAccessibility(&accessibleEvent);
1215 d->doDelayedItemsLayout();
1216 d->updateGeometry();
1220
1221
1222
1223
1224
1225QModelIndex QAbstractItemView::rootIndex()
const
1227 return QModelIndex(d_func()->root);
1231
1232
1233
1234
1235
1236
1237void QAbstractItemView::selectAll()
1239 Q_D(QAbstractItemView);
1240 const SelectionMode mode = d->selectionMode;
1242 case MultiSelection:
1243 case ExtendedSelection:
1244 d->selectAll(QItemSelectionModel::ClearAndSelect
1245 | d->selectionBehaviorFlags());
1248 case ContiguousSelection:
1249 if (d->model->hasChildren(d->root))
1250 d->selectAll(selectionCommand(d->model->index(0, 0, d->root)));
1252 case SingleSelection:
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269void QAbstractItemView::edit(
const QModelIndex &index)
1271 Q_D(QAbstractItemView);
1272 if (Q_UNLIKELY(!d->isIndexValid(index)))
1273 qCWarning(lcAIV,
"edit: index was invalid");
1274 if (Q_UNLIKELY(!edit(index, AllEditTriggers,
nullptr)))
1275 qCWarning(lcAIV,
"edit: editing failed");
1279
1280
1281
1282
1283void QAbstractItemView::clearSelection()
1285 Q_D(QAbstractItemView);
1286 if (d->selectionModel)
1287 d->selectionModel->clearSelection();
1291
1292
1293
1294
1295
1296void QAbstractItemView::doItemsLayout()
1298 Q_D(QAbstractItemView);
1299 d->interruptDelayedItemsLayout();
1301 d->viewport->update();
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319void QAbstractItemView::setEditTriggers(EditTriggers actions)
1321 Q_D(QAbstractItemView);
1322 d->editTriggers = actions;
1325QAbstractItemView::EditTriggers QAbstractItemView::editTriggers()
const
1327 Q_D(
const QAbstractItemView);
1328 return d->editTriggers;
1332
1333
1334
1335
1336
1337
1338
1340void QAbstractItemView::setVerticalScrollMode(ScrollMode mode)
1342 Q_D(QAbstractItemView);
1343 d->verticalScrollModeSet =
true;
1344 if (mode == d->verticalScrollMode)
1346 QModelIndex topLeft = indexAt(QPoint(0, 0));
1347 d->verticalScrollMode = mode;
1348 if (mode == ScrollPerItem)
1349 verticalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1351 verticalScrollBar()->setSingleStep(-1);
1353 scrollTo(topLeft, QAbstractItemView::PositionAtTop);
1356QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode()
const
1358 Q_D(
const QAbstractItemView);
1359 return d->verticalScrollMode;
1362void QAbstractItemView::resetVerticalScrollMode()
1364 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1365 setVerticalScrollMode(sm);
1366 d_func()->verticalScrollModeSet =
false;
1370
1371
1372
1373
1374
1375
1376
1378void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode)
1380 Q_D(QAbstractItemView);
1381 d->horizontalScrollModeSet =
true;
1382 if (mode == d->horizontalScrollMode)
1384 d->horizontalScrollMode = mode;
1385 if (mode == ScrollPerItem)
1386 horizontalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1388 horizontalScrollBar()->setSingleStep(-1);
1392QAbstractItemView::ScrollMode QAbstractItemView::horizontalScrollMode()
const
1394 Q_D(
const QAbstractItemView);
1395 return d->horizontalScrollMode;
1398void QAbstractItemView::resetHorizontalScrollMode()
1400 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1401 setHorizontalScrollMode(sm);
1402 d_func()->horizontalScrollModeSet =
false;
1405#if QT_CONFIG(draganddrop)
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426void QAbstractItemView::setDragDropOverwriteMode(
bool overwrite)
1428 Q_D(QAbstractItemView);
1429 d->overwrite = overwrite;
1432bool QAbstractItemView::dragDropOverwriteMode()
const
1434 Q_D(
const QAbstractItemView);
1435 return d->overwrite;
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1453void QAbstractItemView::setAutoScroll(
bool enable)
1455 Q_D(QAbstractItemView);
1456 d->autoScroll = enable;
1459bool QAbstractItemView::hasAutoScroll()
const
1461 Q_D(
const QAbstractItemView);
1462 return d->autoScroll;
1466
1467
1468
1469
1470
1471
1472void QAbstractItemView::setAutoScrollMargin(
int margin)
1474 Q_D(QAbstractItemView);
1475 d->autoScrollMargin = margin;
1478int QAbstractItemView::autoScrollMargin()
const
1480 Q_D(
const QAbstractItemView);
1481 return d->autoScrollMargin;
1485
1486
1487
1489void QAbstractItemView::setTabKeyNavigation(
bool enable)
1491 Q_D(QAbstractItemView);
1492 d->tabKeyNavigation = enable;
1495bool QAbstractItemView::tabKeyNavigation()
const
1497 Q_D(
const QAbstractItemView);
1498 return d->tabKeyNavigation;
1502
1503
1504
1505QSize QAbstractItemView::viewportSizeHint()
const
1507 return QAbstractScrollArea::viewportSizeHint();
1510#if QT_CONFIG(draganddrop)
1512
1513
1514
1515
1516
1518void QAbstractItemView::setDropIndicatorShown(
bool enable)
1520 Q_D(QAbstractItemView);
1521 d->showDropIndicator = enable;
1524bool QAbstractItemView::showDropIndicator()
const
1526 Q_D(
const QAbstractItemView);
1527 return d->showDropIndicator;
1531
1532
1533
1534
1535
1537void QAbstractItemView::setDragEnabled(
bool enable)
1539 Q_D(QAbstractItemView);
1540 d->dragEnabled = enable;
1543bool QAbstractItemView::dragEnabled()
const
1545 Q_D(
const QAbstractItemView);
1546 return d->dragEnabled;
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1569
1570
1571
1572
1573
1574void QAbstractItemView::setDragDropMode(DragDropMode behavior)
1576 Q_D(QAbstractItemView);
1577 d->dragDropMode = behavior;
1578 setDragEnabled(behavior == DragOnly || behavior == DragDrop || behavior == InternalMove);
1579 setAcceptDrops(behavior == DropOnly || behavior == DragDrop || behavior == InternalMove);
1582QAbstractItemView::DragDropMode QAbstractItemView::dragDropMode()
const
1584 Q_D(
const QAbstractItemView);
1585 DragDropMode setBehavior = d->dragDropMode;
1586 if (!dragEnabled() && !acceptDrops())
1589 if (dragEnabled() && !acceptDrops())
1592 if (!dragEnabled() && acceptDrops())
1595 if (dragEnabled() && acceptDrops()) {
1596 if (setBehavior == InternalMove)
1606
1607
1608
1609
1610
1611
1612
1613
1614void QAbstractItemView::setDefaultDropAction(Qt::DropAction dropAction)
1616 Q_D(QAbstractItemView);
1617 d->defaultDropAction = dropAction;
1620Qt::DropAction QAbstractItemView::defaultDropAction()
const
1622 Q_D(
const QAbstractItemView);
1623 return d->defaultDropAction;
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638void QAbstractItemView::setAlternatingRowColors(
bool enable)
1640 Q_D(QAbstractItemView);
1641 d->alternatingColors = enable;
1643 d->viewport->update();
1646bool QAbstractItemView::alternatingRowColors()
const
1648 Q_D(
const QAbstractItemView);
1649 return d->alternatingColors;
1653
1654
1655
1656
1657
1658
1659void QAbstractItemView::setIconSize(
const QSize &size)
1661 Q_D(QAbstractItemView);
1662 if (size == d->iconSize)
1665 d->doDelayedItemsLayout();
1666 emit iconSizeChanged(size);
1669QSize QAbstractItemView::iconSize()
const
1671 Q_D(
const QAbstractItemView);
1676
1677
1678
1679
1680
1681
1682void QAbstractItemView::setTextElideMode(Qt::TextElideMode mode)
1684 Q_D(QAbstractItemView);
1685 d->textElideMode = mode;
1688Qt::TextElideMode QAbstractItemView::textElideMode()
const
1690 return d_func()->textElideMode;
1694
1695
1696bool QAbstractItemView::focusNextPrevChild(
bool next)
1698 Q_D(QAbstractItemView);
1699 if (d->tabKeyNavigation && isVisible() && isEnabled() && d->viewport->isEnabled()) {
1700 QKeyEvent event(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier);
1701 keyPressEvent(&event);
1702 if (event.isAccepted())
1705 return QAbstractScrollArea::focusNextPrevChild(next);
1709
1710
1711bool QAbstractItemView::event(QEvent *event)
1713 Q_D(QAbstractItemView);
1714 switch (event->type()) {
1718 d->executePostedLayout();
1721 d->executePostedLayout();
1722 if (d->shouldScrollToCurrentOnShow) {
1723 d->shouldScrollToCurrentOnShow =
false;
1724 const QModelIndex current = currentIndex();
1725 if (current.isValid() && (d->state == QAbstractItemView::EditingState || d->autoScroll))
1729 case QEvent::LocaleChange:
1730 viewport()->update();
1732 case QEvent::LayoutDirectionChange:
1733 case QEvent::ApplicationLayoutDirectionChange:
1736 case QEvent::StyleChange:
1738 if (!d->verticalScrollModeSet)
1739 resetVerticalScrollMode();
1740 if (!d->horizontalScrollModeSet)
1741 resetHorizontalScrollMode();
1743 case QEvent::FocusOut:
1744 d->checkPersistentEditorFocus();
1746 case QEvent::FontChange:
1747 d->doDelayedItemsLayout();
1752 return QAbstractScrollArea::event(event);
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766bool QAbstractItemView::viewportEvent(QEvent *event)
1768 Q_D(QAbstractItemView);
1769 switch (event->type()) {
1774 d->executePostedLayout();
1776 case QEvent::HoverMove:
1777 case QEvent::HoverEnter:
1778 d->setHoverIndex(indexAt(
static_cast<QHoverEvent*>(event)->position().toPoint()));
1780 case QEvent::HoverLeave:
1781 d->setHoverIndex(QModelIndex());
1784 d->viewportEnteredNeeded =
true;
1787 d->setHoverIndex(QModelIndex());
1788 #if QT_CONFIG(statustip)
1789 if (d->shouldClearStatusTip && d->parent) {
1791 QStatusTipEvent tip(empty);
1792 QCoreApplication::sendEvent(d->parent, &tip);
1793 d->shouldClearStatusTip =
false;
1796 d->enteredIndex = QModelIndex();
1798 case QEvent::ToolTip:
1799 case QEvent::QueryWhatsThis:
1800 case QEvent::WhatsThis: {
1801 QHelpEvent *he =
static_cast<QHelpEvent*>(event);
1802 const QModelIndex index = indexAt(he->pos());
1803 QStyleOptionViewItem option;
1804 initViewItemOption(&option);
1805 option.rect = visualRect(index);
1806 option.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
1808 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
1811 return delegate->helpEvent(he,
this, option, index);
1813 case QEvent::FontChange:
1814 d->doDelayedItemsLayout();
1816 case QEvent::WindowActivate:
1817 case QEvent::WindowDeactivate:
1818 d->viewport->update();
1820 case QEvent::ScrollPrepare:
1821 executeDelayedItemsLayout();
1822#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
1823 d->scollerConnection = QObjectPrivate::connect(
1824 QScroller::scroller(d->viewport), &QScroller::stateChanged,
1825 d, &QAbstractItemViewPrivate::scrollerStateChanged,
1826 Qt::UniqueConnection);
1833 return QAbstractScrollArea::viewportEvent(event);
1837
1838
1839
1840
1841void QAbstractItemView::mousePressEvent(QMouseEvent *event)
1843 Q_D(QAbstractItemView);
1844 d->releaseFromDoubleClick =
false;
1845 d->delayedAutoScroll.stop();
1846 QPoint pos = event->position().toPoint();
1847 QPersistentModelIndex index = indexAt(pos);
1850 d->pressClosedEditor = d->pressClosedEditorWatcher.isActive() && d->lastEditedIndex == index;
1852 if (!d->selectionModel || (d->state == EditingState && d->hasEditor(index)))
1855 d->pressedAlreadySelected = d->selectionModel->isSelected(index);
1856 d->pressedIndex = index;
1857 d->pressedModifiers = event->modifiers();
1858 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1859 d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid();
1860 QPoint offset = d->offset();
1861 d->draggedPosition = pos + offset;
1863#if QT_CONFIG(draganddrop)
1866 d->pressedPosition = d->draggedPosition;
1869 if (!(command & QItemSelectionModel::Current)) {
1870 d->pressedPosition = pos + offset;
1871 d->currentSelectionStartIndex = index;
1873 else if (!d->currentSelectionStartIndex.isValid())
1874 d->currentSelectionStartIndex = currentIndex();
1876 if (edit(index, NoEditTriggers, event))
1879 if (index.isValid() && d->isIndexEnabled(index)) {
1882 bool autoScroll = d->autoScroll;
1883 d->autoScroll =
false;
1884 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1885 d->autoScroll = autoScroll;
1886 if (command.testFlag(QItemSelectionModel::Toggle)) {
1887 command &= ~QItemSelectionModel::Toggle;
1888 d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
1889 command |= d->ctrlDragSelectionFlag;
1892 if (!(command & QItemSelectionModel::Current)) {
1893 setSelection(QRect(pos, QSize(1, 1)), command);
1895 QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
1896 setSelection(rect, command);
1900 emit pressed(index);
1901 if (d->autoScroll) {
1904 d->delayedAutoScroll.start(QApplication::doubleClickInterval()+100,
this);
1909 d->selectionModel->select(QModelIndex(), QItemSelectionModel::Select);
1914
1915
1916
1917
1918void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
1920 Q_D(QAbstractItemView);
1921 QPoint bottomRight = event->position().toPoint();
1923 d->draggedPosition = bottomRight + d->offset();
1925 if (state() == ExpandingState || state() == CollapsingState)
1928#if QT_CONFIG(draganddrop)
1929 if (state() == DraggingState) {
1930 d->maybeStartDrag(bottomRight);
1935 QPersistentModelIndex index = indexAt(bottomRight);
1936 QModelIndex buddy = d->model->buddy(d->pressedIndex);
1937 if ((state() == EditingState && d->hasEditor(buddy))
1938 || edit(index, NoEditTriggers, event))
1941 const QPoint topLeft =
1942 d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight;
1944 d->checkMouseMove(index);
1946#if QT_CONFIG(draganddrop)
1947 if (d->pressedIndex.isValid()
1949 && (state() != DragSelectingState)
1950 && (event->buttons() != Qt::NoButton)
1951 && !d->selectedDraggableIndexes().isEmpty()) {
1952 setState(DraggingState);
1953 d->maybeStartDrag(bottomRight);
1958 if ((event->buttons() & Qt::LeftButton) && d->selectionAllowed(index) && d->selectionModel) {
1959 setState(DragSelectingState);
1960 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1961 if (d->ctrlDragSelectionFlag != QItemSelectionModel::NoUpdate && command.testFlag(QItemSelectionModel::Toggle)) {
1962 command &= ~QItemSelectionModel::Toggle;
1963 command |= d->ctrlDragSelectionFlag;
1967 QRect selectionRect = QRect(topLeft, bottomRight);
1968 setSelection(selectionRect, command);
1971 if (index.isValid() && (index != d->selectionModel->currentIndex()) && d->isIndexEnabled(index))
1972 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1973 else if (d->shouldAutoScroll(event->pos()) && !d->autoScrollTimer.isActive())
1979
1980
1981
1982
1983
1984
1985void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
1987 Q_D(QAbstractItemView);
1988 const bool releaseFromDoubleClick = d->releaseFromDoubleClick;
1989 d->releaseFromDoubleClick =
false;
1991 QPoint pos = event->position().toPoint();
1992 QPersistentModelIndex index = indexAt(pos);
1994 if (state() == EditingState) {
1995 if (d->isIndexValid(index)
1996 && d->isIndexEnabled(index)
1997 && d->sendDelegateEvent(index, event))
2002 bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
2003 bool selectedClicked = click && d->pressedAlreadySelected
2004 && (event->button() == Qt::LeftButton)
2005 && (event->modifiers() == Qt::NoModifier);
2006 EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
2007 const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) :
false;
2009 d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
2011 if (d->selectionModel && d->noSelectionOnMousePress) {
2012 d->noSelectionOnMousePress =
false;
2013 if (!d->pressClosedEditor)
2014 d->selectionModel->select(index, selectionCommand(index, event));
2017 d->pressClosedEditor =
false;
2022 if (event->button() == Qt::LeftButton)
2023 emit clicked(index);
2026 QStyleOptionViewItem option;
2027 initViewItemOption(&option);
2028 if (d->pressedAlreadySelected)
2029 option.state |= QStyle::State_Selected;
2030 if ((d->model->flags(index) & Qt::ItemIsEnabled)
2031 && style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option,
this))
2032 emit activated(index);
2037
2038
2039
2040
2041void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
2043 Q_D(QAbstractItemView);
2045 QModelIndex index = indexAt(event->position().toPoint());
2046 if (!index.isValid()
2047 || !d->isIndexEnabled(index)
2048 || (d->pressedIndex != index)) {
2049 QMouseEvent me(QEvent::MouseButtonPress,
2050 event->position(), event->scenePosition(), event->globalPosition(),
2051 event->button(), event->buttons(), event->modifiers(),
2052 event->source(), event->pointingDevice());
2053 mousePressEvent(&me);
2057 QPersistentModelIndex persistent = index;
2058 emit doubleClicked(persistent);
2059 if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
2060 && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick,
nullptr,
this))
2061 emit activated(persistent);
2062 d->releaseFromDoubleClick =
true;
2065#if QT_CONFIG(draganddrop)
2068
2069
2070
2071
2072
2073
2074void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
2076 if (dragDropMode() == InternalMove
2077 && (event->source() !=
this|| !(event->possibleActions() & Qt::MoveAction)))
2080 if (d_func()->canDrop(event)) {
2082 setState(DraggingState);
2089
2090
2091
2092
2093
2094
2095
2096void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
2098 Q_D(QAbstractItemView);
2099 d->draggedPosition = event->position().toPoint() + d->offset();
2100 if (dragDropMode() == InternalMove
2101 && (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction)))
2107 QModelIndex index = indexAt(event->position().toPoint());
2109 if (!d->droppingOnItself(event, index)
2110 && d->canDrop(event)) {
2112 if (index.isValid() && d->showDropIndicator) {
2113 QRect rect = visualRect(index);
2114 d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
2115 if (d->selectionBehavior == QAbstractItemView::SelectRows
2116 && d->dropIndicatorPosition != OnViewport
2117 && (d->dropIndicatorPosition != OnItem || event->source() ==
this)) {
2118 const int maxCol = d->model->columnCount(index.parent()) - 1;
2119 const auto idx = index.column() > 0 ? index.siblingAtColumn(0) : index;
2120 rect = d->intersectedRect(viewport()->rect(), idx, idx.siblingAtColumn(maxCol));
2122 switch (d->dropIndicatorPosition) {
2124 if (d->isIndexDropEnabled(index.parent())) {
2125 d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0);
2126 event->acceptProposedAction();
2128 d->dropIndicatorRect = QRect();
2132 if (d->isIndexDropEnabled(index.parent())) {
2133 d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0);
2134 event->acceptProposedAction();
2136 d->dropIndicatorRect = QRect();
2140 if (d->isIndexDropEnabled(index)) {
2141 d->dropIndicatorRect = rect;
2142 event->acceptProposedAction();
2144 d->dropIndicatorRect = QRect();
2148 d->dropIndicatorRect = QRect();
2149 if (d->isIndexDropEnabled(rootIndex())) {
2150 event->acceptProposedAction();
2155 d->dropIndicatorRect = QRect();
2156 d->dropIndicatorPosition = OnViewport;
2157 if (d->isIndexDropEnabled(rootIndex())) {
2158 event->acceptProposedAction();
2161 d->viewport->update();
2164 if (d->shouldAutoScroll(event->position().toPoint()))
2169
2170
2171
2172
2173bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event,
const QModelIndex &index)
2175 Q_Q(QAbstractItemView);
2176 Qt::DropAction dropAction = event->dropAction();
2177 if (q->dragDropMode() == QAbstractItemView::InternalMove)
2178 dropAction = Qt::MoveAction;
2179 if (event->source() == q
2180 && event->possibleActions() & Qt::MoveAction
2181 && dropAction == Qt::MoveAction) {
2182 QModelIndexList selectedIndexes = q->selectedIndexes();
2183 QModelIndex child = index;
2184 while (child.isValid() && child != root) {
2185 if (selectedIndexes.contains(child))
2187 child = child.parent();
2194
2195
2196
2197
2198
2199void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *)
2201 Q_D(QAbstractItemView);
2204 d->hover = QModelIndex();
2205 d->viewport->update();
2209
2210
2211
2212
2213
2214
2215void QAbstractItemView::dropEvent(QDropEvent *event)
2217 Q_D(QAbstractItemView);
2218 if (dragDropMode() == InternalMove) {
2219 if (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction))
2226 if (d->dropOn(event, &row, &col, &index)) {
2227 const Qt::DropAction action = dragDropMode() == InternalMove ? Qt::MoveAction : event->dropAction();
2228 if (d->model->dropMimeData(event->mimeData(), action, row, col, index)) {
2229 if (action != event->dropAction()) {
2230 event->setDropAction(action);
2233 event->acceptProposedAction();
2239 d->viewport->update();
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253bool QAbstractItemViewPrivate::dropOn(QDropEvent *event,
int *dropRow,
int *dropCol, QModelIndex *dropIndex)
2255 Q_Q(QAbstractItemView);
2256 if (event->isAccepted())
2261 if (viewport->rect().contains(event->position().toPoint())) {
2262 index = q->indexAt(event->position().toPoint());
2263 if (!index.isValid())
2268 if (model->supportedDropActions() & event->dropAction()) {
2271 if (index != root) {
2272 dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
2273 switch (dropIndicatorPosition) {
2274 case QAbstractItemView::AboveItem:
2276 col = index.column();
2277 index = index.parent();
2279 case QAbstractItemView::BelowItem:
2280 row = index.row() + 1;
2281 col = index.column();
2282 index = index.parent();
2284 case QAbstractItemView::OnItem:
2285 case QAbstractItemView::OnViewport:
2289 dropIndicatorPosition = QAbstractItemView::OnViewport;
2294 if (!droppingOnItself(event, index))
2300QAbstractItemView::DropIndicatorPosition
2301QAbstractItemViewPrivate::position(
const QPoint &pos,
const QRect &rect,
const QModelIndex &index)
const
2303 QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport;
2305 const int margin = qBound(2, qRound(qreal(rect.height()) / 5.5), 12);
2306 if (pos.y() - rect.top() < margin) {
2307 r = QAbstractItemView::AboveItem;
2308 }
else if (rect.bottom() - pos.y() < margin) {
2309 r = QAbstractItemView::BelowItem;
2310 }
else if (rect.contains(pos,
true)) {
2311 r = QAbstractItemView::OnItem;
2314 QRect touchingRect = rect;
2315 touchingRect.adjust(-1, -1, 1, 1);
2316 if (touchingRect.contains(pos,
false)) {
2317 r = QAbstractItemView::OnItem;
2321 if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled)))
2322 r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem;
2330
2331
2332
2333
2334
2335void QAbstractItemView::focusInEvent(QFocusEvent *event)
2337 Q_D(QAbstractItemView);
2338 QAbstractScrollArea::focusInEvent(event);
2340 const QItemSelectionModel* model = selectionModel();
2341 bool currentIndexValid = currentIndex().isValid();
2344 && !d->currentIndexSet
2345 && !currentIndexValid) {
2346 bool autoScroll = d->autoScroll;
2347 d->autoScroll =
false;
2348 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2349 if (index.isValid() && d->isIndexEnabled(index) && event->reason() != Qt::MouseFocusReason) {
2350 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
2351 currentIndexValid =
true;
2353 d->autoScroll = autoScroll;
2356 if (model && currentIndexValid)
2357 setAttribute(Qt::WA_InputMethodEnabled, (currentIndex().flags() & Qt::ItemIsEditable));
2358 else if (!currentIndexValid)
2359 setAttribute(Qt::WA_InputMethodEnabled,
false);
2361 d->viewport->update();
2365
2366
2367
2368
2369
2370void QAbstractItemView::focusOutEvent(QFocusEvent *event)
2372 Q_D(QAbstractItemView);
2373 QAbstractScrollArea::focusOutEvent(event);
2374 d->viewport->update();
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388void QAbstractItemView::keyPressEvent(QKeyEvent *event)
2390 Q_D(QAbstractItemView);
2391 d->delayedAutoScroll.stop();
2393#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
2394 if (event == QKeySequence::Copy) {
2395 const QModelIndex index = currentIndex();
2396 if (index.isValid() && d->model) {
2397 const QVariant variant = d->model->data(index, Qt::DisplayRole);
2398 if (variant.canConvert<QString>())
2399 QGuiApplication::clipboard()->setText(variant.toString());
2405 QPersistentModelIndex newCurrent;
2406 d->moveCursorUpdatedView =
false;
2407 switch (event->key()) {
2409 newCurrent = moveCursor(MoveDown, event->modifiers());
2412 newCurrent = moveCursor(MoveUp, event->modifiers());
2415 newCurrent = moveCursor(MoveLeft, event->modifiers());
2418 newCurrent = moveCursor(MoveRight, event->modifiers());
2421 newCurrent = moveCursor(MoveHome, event->modifiers());
2424 newCurrent = moveCursor(MoveEnd, event->modifiers());
2426 case Qt::Key_PageUp:
2427 newCurrent = moveCursor(MovePageUp, event->modifiers());
2429 case Qt::Key_PageDown:
2430 newCurrent = moveCursor(MovePageDown, event->modifiers());
2433 if (d->tabKeyNavigation)
2434 newCurrent = moveCursor(MoveNext, event->modifiers());
2436 case Qt::Key_Backtab:
2437 if (d->tabKeyNavigation)
2438 newCurrent = moveCursor(MovePrevious, event->modifiers());
2442 QPersistentModelIndex oldCurrent = currentIndex();
2443 if (newCurrent != oldCurrent && newCurrent.isValid() && d->isIndexEnabled(newCurrent)) {
2444 if (!hasFocus() && QApplication::focusWidget() == indexWidget(oldCurrent))
2446 QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
2447 if (command != QItemSelectionModel::NoUpdate
2448 || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection,
nullptr,
this)) {
2450 if (command & QItemSelectionModel::Current) {
2451 d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
2452 if (!d->currentSelectionStartIndex.isValid())
2453 d->currentSelectionStartIndex = oldCurrent;
2454 QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center());
2455 setSelection(rect, command);
2457 d->selectionModel->setCurrentIndex(newCurrent, command);
2458 d->currentSelectionStartIndex = newCurrent;
2459 if (newCurrent.isValid()) {
2461 QRect rect(visualRect(newCurrent).center(), QSize(1, 1));
2462 setSelection(rect, command);
2470 switch (event->key()) {
2478 case Qt::Key_PageUp:
2479 case Qt::Key_PageDown:
2480 case Qt::Key_Escape:
2482 case Qt::Key_Control:
2483 case Qt::Key_Delete:
2484 case Qt::Key_Backspace:
2488 case Qt::Key_Select:
2489 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2490 if (d->selectionModel)
2491 d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
2492 if (event->key() == Qt::Key_Space) {
2493 keyboardSearch(event->text());
2500 case Qt::Key_Return:
2503 if (!edit(currentIndex(), EditKeyPressed, event) && d->editorIndexHash.isEmpty())
2508 if (!edit(currentIndex(), EditKeyPressed, event))
2512 case Qt::Key_Return:
2516 if (state() != EditingState || hasFocus()) {
2517 if (currentIndex().isValid())
2518 emit activated(currentIndex());
2524#ifndef QT_NO_SHORTCUT
2525 if (event == QKeySequence::SelectAll && selectionMode() != NoSelection) {
2531 if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
2532 emit activated(currentIndex());
2536 bool modified = (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
2537 if (!event->text().isEmpty() && !modified && !edit(currentIndex(), AnyKeyPressed, event)) {
2538 keyboardSearch(event->text());
2545 if (d->moveCursorUpdatedView)
2550
2551
2552
2553
2554
2555void QAbstractItemView::resizeEvent(QResizeEvent *event)
2557 QAbstractScrollArea::resizeEvent(event);
2562
2563
2564
2565
2566
2567void QAbstractItemView::timerEvent(QTimerEvent *event)
2569 Q_D(QAbstractItemView);
2570 if (event->timerId() == d->fetchMoreTimer.timerId())
2572 else if (event->timerId() == d->delayedReset.timerId())
2574 else if (event->timerId() == d->autoScrollTimer.timerId())
2576 else if (event->timerId() == d->updateTimer.timerId())
2577 d->updateDirtyRegion();
2578 else if (event->timerId() == d->delayedEditing.timerId()) {
2579 d->delayedEditing.stop();
2580 edit(currentIndex());
2581 }
else if (event->timerId() == d->delayedLayout.timerId()) {
2582 d->delayedLayout.stop();
2584 d->interruptDelayedItemsLayout();
2586 const QModelIndex current = currentIndex();
2587 if (current.isValid() && d->state == QAbstractItemView::EditingState)
2590 }
else if (event->timerId() == d->delayedAutoScroll.timerId()) {
2591 d->delayedAutoScroll.stop();
2594 if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
2595 scrollTo(d->pressedIndex);
2596 }
else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
2597 d->pressClosedEditorWatcher.stop();
2602
2603
2604void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
2606 Q_D(QAbstractItemView);
2613 bool forwardEventToEditor =
false;
2614 const bool commit = !event->commitString().isEmpty();
2615 const bool preediting = !event->preeditString().isEmpty();
2616 if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
2617 if (d->waitForIMCommit) {
2618 if (commit || !preediting) {
2620 d->waitForIMCommit =
false;
2621 QApplication::sendEvent(currentEditor, event);
2623 QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
2625 delegate->setEditorData(currentEditor, currentIndex());
2626 d->selectAllInEditor(currentEditor);
2628 if (currentEditor->focusPolicy() != Qt::NoFocus)
2629 currentEditor->setFocus();
2632 QApplication::sendEvent(currentEditor, event);
2636 }
else if (preediting) {
2638 d->waitForIMCommit =
true;
2640 forwardEventToEditor =
true;
2641 }
else if (!commit) {
2645 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2646 d->waitForIMCommit =
false;
2648 keyboardSearch(event->commitString());
2650 }
else if (QWidget *currentEditor; forwardEventToEditor
2651 && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
2652 QApplication::sendEvent(currentEditor, event);
2656#if QT_CONFIG(draganddrop)
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2676
2677
2678QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition()
const
2680 Q_D(
const QAbstractItemView);
2681 return d->dropIndicatorPosition;
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698QModelIndexList QAbstractItemView::selectedIndexes()
const
2700 Q_D(
const QAbstractItemView);
2701 QModelIndexList indexes;
2702 if (d->selectionModel) {
2703 indexes = d->selectionModel->selectedIndexes();
2704 auto isHidden = [
this](
const QModelIndex &idx) {
2705 return isIndexHidden(idx);
2707 indexes.removeIf(isHidden);
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725bool QAbstractItemView::edit(
const QModelIndex &index, EditTrigger trigger, QEvent *event)
2727 Q_D(QAbstractItemView);
2729 if (!d->isIndexValid(index))
2732 if (QWidget *w = (d->persistent.isEmpty() ?
static_cast<QWidget*>(
nullptr) : d->editorForIndex(index).widget.data())) {
2733 if (w->focusPolicy() == Qt::NoFocus)
2735 if (!d->waitForIMCommit)
2742 if (trigger == DoubleClicked) {
2743 d->delayedEditing.stop();
2744 d->delayedAutoScroll.stop();
2745 }
else if (trigger == CurrentChanged) {
2746 d->delayedEditing.stop();
2750 QPersistentModelIndex safeIndex(index);
2752 if (d->sendDelegateEvent(index, event)) {
2757 if (!safeIndex.isValid()) {
2762 EditTriggers lastTrigger = d->lastTrigger;
2763 d->lastTrigger = trigger;
2765 if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
2768 if (d->delayedEditing.isActive())
2773 if (lastTrigger == DoubleClicked && trigger == SelectedClicked)
2777 if (trigger == SelectedClicked)
2778 d->delayedEditing.start(QApplication::doubleClickInterval(),
this);
2780 d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event :
nullptr);
2786
2787
2788
2789void QAbstractItemView::updateEditorData()
2791 Q_D(QAbstractItemView);
2792 d->updateEditorData(QModelIndex(), QModelIndex());
2796
2797
2798
2799void QAbstractItemView::updateEditorGeometries()
2801 Q_D(QAbstractItemView);
2802 if (d->editorIndexHash.isEmpty())
2804 if (d->delayedPendingLayout) {
2806 d->executePostedLayout();
2809 QStyleOptionViewItem option;
2810 initViewItemOption(&option);
2811 QEditorIndexHash::iterator it = d->editorIndexHash.begin();
2812 QWidgetList editorsToRelease;
2813 QWidgetList editorsToHide;
2814 while (it != d->editorIndexHash.end()) {
2815 QModelIndex index = it.value();
2816 QWidget *editor = it.key();
2817 if (index.isValid() && editor) {
2818 option.rect = visualRect(index);
2819 if (option.rect.isValid()) {
2821 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2823 delegate->updateEditorGeometry(editor, option, index);
2825 editorsToHide << editor;
2829 d->indexEditorHash.remove(it.value());
2830 it = d->editorIndexHash.erase(it);
2831 editorsToRelease << editor;
2837 for (
int i = 0; i < editorsToHide.size(); ++i) {
2838 editorsToHide.at(i)->hide();
2840 for (
int i = 0; i < editorsToRelease.size(); ++i) {
2841 d->releaseEditor(editorsToRelease.at(i));
2846
2847
2848void QAbstractItemView::updateGeometries()
2850 Q_D(QAbstractItemView);
2851 updateEditorGeometries();
2852 d->fetchMoreTimer.start(0,
this);
2853 d->updateGeometry();
2857
2858
2859void QAbstractItemView::verticalScrollbarValueChanged(
int value)
2861 Q_D(QAbstractItemView);
2862 if (verticalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2863 d->model->fetchMore(d->root);
2864 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2865 if (viewport()->rect().contains(posInVp))
2866 d->checkMouseMove(posInVp);
2870
2871
2872void QAbstractItemView::horizontalScrollbarValueChanged(
int value)
2874 Q_D(QAbstractItemView);
2875 if (horizontalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2876 d->model->fetchMore(d->root);
2877 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2878 if (viewport()->rect().contains(posInVp))
2879 d->checkMouseMove(posInVp);
2883
2884
2885void QAbstractItemView::verticalScrollbarAction(
int)
2891
2892
2893void QAbstractItemView::horizontalScrollbarAction(
int)
2899
2900
2901
2902
2903
2904
2905
2907void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
2909 Q_D(QAbstractItemView);
2913 const bool isPersistent = d->persistent.contains(editor);
2914 const QModelIndex index = d->indexForEditor(editor);
2915 if (!index.isValid()) {
2916 if (!editor->isVisible()) {
2925 "QAbstractItemView::closeEditor called with an editor that does not "
2926 "belong to this view");
2930 const bool hadFocus = editor->hasFocus();
2934 d->pressClosedEditorWatcher.start(0,
this);
2935 d->lastEditedIndex = index;
2937 if (!isPersistent) {
2939 QModelIndex index = d->indexForEditor(editor);
2940 editor->removeEventFilter(itemDelegateForIndex(index));
2941 d->removeEditor(editor);
2944 if (focusPolicy() != Qt::NoFocus)
2947 editor->clearFocus();
2949 d->checkPersistentEditorFocus();
2952 QPointer<QWidget> ed = editor;
2953 QCoreApplication::sendPostedEvents(editor, 0);
2956 if (!isPersistent && editor)
2957 d->releaseEditor(editor, index);
2962 QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::NoUpdate;
2963 if (d->selectionMode != NoSelection)
2964 flags = QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
2966 case QAbstractItemDelegate::EditNextItem: {
2967 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2968 if (index.isValid()) {
2969 QPersistentModelIndex persistent(index);
2970 d->selectionModel->setCurrentIndex(persistent, flags);
2972 if (index.flags() & Qt::ItemIsEditable
2973 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2976 case QAbstractItemDelegate::EditPreviousItem: {
2977 QModelIndex index = moveCursor(MovePrevious, Qt::NoModifier);
2978 if (index.isValid()) {
2979 QPersistentModelIndex persistent(index);
2980 d->selectionModel->setCurrentIndex(persistent, flags);
2982 if (index.flags() & Qt::ItemIsEditable
2983 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2986 case QAbstractItemDelegate::SubmitModelCache:
2989 case QAbstractItemDelegate::RevertModelCache:
2998
2999
3000
3001
3002void QAbstractItemView::commitData(QWidget *editor)
3004 Q_D(QAbstractItemView);
3005 if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
3007 QModelIndex index = d->indexForEditor(editor);
3008 if (!index.isValid()) {
3010 "QAbstractItemView::commitData called with an editor that does not belong to "
3014 d->currentlyCommittingEditor = editor;
3015 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
3016 editor->removeEventFilter(delegate);
3017 delegate->setModelData(editor, d->model, index);
3018 editor->installEventFilter(delegate);
3019 d->currentlyCommittingEditor =
nullptr;
3023
3024
3025
3026
3027void QAbstractItemView::editorDestroyed(QObject *editor)
3029 Q_D(QAbstractItemView);
3030 QWidget *w = qobject_cast<QWidget*>(editor);
3032 d->persistent.remove(w);
3033 if (state() == EditingState)
3040
3041
3042
3043
3044
3045
3046
3047void QAbstractItemView::keyboardSearch(
const QString &search)
3049 Q_D(QAbstractItemView);
3050 if (!d->model->rowCount(d->root) || !d->model->columnCount(d->root))
3053 QModelIndex start = currentIndex().isValid() ? currentIndex()
3054 : d->model->index(0, 0, d->root);
3055 bool skipRow =
false;
3056 bool keyboardTimeWasValid = d->keyboardInputTime.isValid();
3057 qint64 keyboardInputTimeElapsed;
3058 if (keyboardTimeWasValid)
3059 keyboardInputTimeElapsed = d->keyboardInputTime.restart();
3061 d->keyboardInputTime.start();
3062 if (search.isEmpty() || !keyboardTimeWasValid
3063 || keyboardInputTimeElapsed > QApplication::keyboardInputInterval()) {
3064 d->keyboardInput = search;
3065 skipRow = currentIndex().isValid();
3067 d->keyboardInput += search;
3071 bool sameKey =
false;
3072 if (d->keyboardInput.size() > 1) {
3073 int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
3074 sameKey = (c == d->keyboardInput.size());
3081 QModelIndex parent = start.parent();
3082 int newRow = (start.row() < d->model->rowCount(parent) - 1) ? start.row() + 1 : 0;
3083 start = d->model->index(newRow, start.column(), parent);
3087 QModelIndex current = start;
3088 QModelIndexList match;
3089 QModelIndex firstMatch;
3090 QModelIndex startMatch;
3091 QModelIndexList previous;
3093 match = d->model->match(current, Qt::DisplayRole, d->keyboardInput, 1,
3094 d->keyboardSearchFlags);
3095 if (match == previous)
3097 firstMatch = match.value(0);
3099 if (firstMatch.isValid()) {
3100 if (d->isIndexEnabled(firstMatch)) {
3101 setCurrentIndex(firstMatch);
3104 int row = firstMatch.row() + 1;
3105 if (row >= d->model->rowCount(firstMatch.parent()))
3107 current = firstMatch.sibling(row, firstMatch.column());
3110 if (!startMatch.isValid())
3111 startMatch = firstMatch;
3112 else if (startMatch == firstMatch)
3115 }
while (current != start && firstMatch.isValid());
3119
3120
3121
3122
3123
3124QSize QAbstractItemView::sizeHintForIndex(
const QModelIndex &index)
const
3126 Q_D(
const QAbstractItemView);
3127 if (!d->isIndexValid(index))
3129 const auto delegate = itemDelegateForIndex(index);
3130 QStyleOptionViewItem option;
3131 initViewItemOption(&option);
3132 return delegate ? delegate->sizeHint(option, index) : QSize();
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151int QAbstractItemView::sizeHintForRow(
int row)
const
3153 Q_D(
const QAbstractItemView);
3155 if (row < 0 || row >= d->model->rowCount(d->root))
3160 QStyleOptionViewItem option;
3161 initViewItemOption(&option);
3163 int colCount = d->model->columnCount(d->root);
3164 for (
int c = 0; c < colCount; ++c) {
3165 const QModelIndex index = d->model->index(row, c, d->root);
3166 if (QWidget *editor = d->editorForIndex(index).widget.data())
3167 height = qMax(height, editor->height());
3168 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3169 height = qMax(height, delegate->sizeHint(option, index).height());
3175
3176
3177
3178
3179
3180
3181
3182int QAbstractItemView::sizeHintForColumn(
int column)
const
3184 Q_D(
const QAbstractItemView);
3186 if (column < 0 || column >= d->model->columnCount(d->root))
3191 QStyleOptionViewItem option;
3192 initViewItemOption(&option);
3194 int rows = d->model->rowCount(d->root);
3195 for (
int r = 0; r < rows; ++r) {
3196 const QModelIndex index = d->model->index(r, column, d->root);
3197 if (QWidget *editor = d->editorForIndex(index).widget.data())
3198 width = qMax(width, editor->sizeHint().width());
3199 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3200 width = qMax(width, delegate->sizeHint(option, index).width());
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222int QAbstractItemView::updateThreshold()
const
3224 Q_D(
const QAbstractItemView);
3225 return d->updateThreshold;
3228void QAbstractItemView::setUpdateThreshold(
int threshold)
3230 Q_D(QAbstractItemView);
3231 if (d->updateThreshold == threshold)
3233 d->updateThreshold = threshold;
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3248Qt::MatchFlags QAbstractItemView::keyboardSearchFlags()
const
3250 Q_D(
const QAbstractItemView);
3251 return d->keyboardSearchFlags;
3254void QAbstractItemView::setKeyboardSearchFlags(Qt::MatchFlags searchFlags)
3256 Q_D(QAbstractItemView);
3257 d->keyboardSearchFlags = searchFlags;
3261
3262
3263
3264
3265
3266void QAbstractItemView::openPersistentEditor(
const QModelIndex &index)
3268 Q_D(QAbstractItemView);
3269 QStyleOptionViewItem options;
3270 initViewItemOption(&options);
3271 options.rect = visualRect(index);
3272 options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
3274 QWidget *editor = d->editor(index, options);
3277 d->persistent.insert(editor);
3282
3283
3284
3285
3286void QAbstractItemView::closePersistentEditor(
const QModelIndex &index)
3288 Q_D(QAbstractItemView);
3289 if (QWidget *editor = d->editorForIndex(index).widget.data()) {
3290 if (index == selectionModel()->currentIndex())
3291 closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
3292 d->persistent.remove(editor);
3293 d->removeEditor(editor);
3294 d->releaseEditor(editor, index);
3299
3300
3301
3302
3303
3304
3305bool QAbstractItemView::isPersistentEditorOpen(
const QModelIndex &index)
const
3307 Q_D(
const QAbstractItemView);
3308 QWidget *editor = d->editorForIndex(index).widget;
3309 return editor && d->persistent.contains(editor);
4078void QAbstractItemView::doAutoScroll()
4081 Q_D(QAbstractItemView);
4082 QScrollBar *verticalScroll = verticalScrollBar();
4083 QScrollBar *horizontalScroll = horizontalScrollBar();
4087 QHeaderView *hv = qobject_cast<QHeaderView*>(
this);
4089 QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
4091 if (hv->orientation() == Qt::Horizontal) {
4092 if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
4093 horizontalScroll = parent->horizontalScrollBar();
4095 if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
4096 verticalScroll = parent->verticalScrollBar();
4101 const int verticalStep = verticalScroll->pageStep();
4102 const int horizontalStep = horizontalScroll->pageStep();
4103 if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
4104 ++d->autoScrollCount;
4106 const int margin = d->autoScrollMargin;
4107 const int verticalValue = verticalScroll->value();
4108 const int horizontalValue = horizontalScroll->value();
4110 const QPoint pos = d->draggedPosition - d->offset();
4111 const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
4114 if (pos.y() - area.top() < margin)
4115 verticalScroll->setValue(verticalValue - d->autoScrollCount);
4116 else if (area.bottom() - pos.y() < margin)
4117 verticalScroll->setValue(verticalValue + d->autoScrollCount);
4118 if (pos.x() - area.left() < margin)
4119 horizontalScroll->setValue(horizontalValue - d->autoScrollCount);
4120 else if (area.right() - pos.x() < margin)
4121 horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
4123 const bool verticalUnchanged = (verticalValue == verticalScroll->value());
4124 const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
4125 if (verticalUnchanged && horizontalUnchanged) {
4128#if QT_CONFIG(draganddrop)
4129 d->dropIndicatorRect = QRect();
4130 d->dropIndicatorPosition = QAbstractItemView::OnViewport;
4133 case QAbstractItemView::DragSelectingState: {
4136 const QPoint globalPos = d->viewport->mapToGlobal(pos);
4137 const QPoint windowPos = window()->mapFromGlobal(globalPos);
4138 QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
4139 Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
4140 Qt::MouseEventSynthesizedByQt);
4141 QApplication::sendEvent(viewport(), &mm);
4144 case QAbstractItemView::DraggingState: {
4150 d->draggedPosition = pos + d->offset();
4156 d->viewport->update();
4260QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
4261 const QModelIndex &index,
const QEvent *event)
const
4263 Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
4264 ?
static_cast<
const QInputEvent*>(event)->modifiers()
4265 : QGuiApplication::keyboardModifiers();
4267 switch (event->type()) {
4268 case QEvent::MouseMove: {
4270 if (modifiers & Qt::ControlModifier)
4271 return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
4274 case QEvent::MouseButtonPress: {
4275 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4276 const bool rightButtonPressed = button & Qt::RightButton;
4277 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4278 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4279 const bool indexIsSelected = selectionModel->isSelected(index);
4280 if ((shiftKeyPressed || controlKeyPressed) && rightButtonPressed)
4281 return QItemSelectionModel::NoUpdate;
4282 if (!shiftKeyPressed && !controlKeyPressed && indexIsSelected)
4283 return QItemSelectionModel::NoUpdate;
4284 if (!index.isValid() && !rightButtonPressed && !shiftKeyPressed && !controlKeyPressed)
4285 return QItemSelectionModel::Clear;
4286 if (!index.isValid())
4287 return QItemSelectionModel::NoUpdate;
4289 if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
4290#if QT_CONFIG(draganddrop)
4291 && dragEnabled && isIndexDragEnabled(index)
4294 return QItemSelectionModel::NoUpdate;
4298 case QEvent::MouseButtonRelease: {
4300 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4301 const bool rightButtonPressed = button & Qt::RightButton;
4302 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4303 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4304 if (((index == pressedIndex && selectionModel->isSelected(index))
4305 || !index.isValid()) && state != QAbstractItemView::DragSelectingState
4306 && !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
4307 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
4308 if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
4309#if QT_CONFIG(draganddrop)
4310 && dragEnabled && isIndexDragEnabled(index)
4315 return QItemSelectionModel::NoUpdate;
4317 case QEvent::KeyPress: {
4319 switch (
static_cast<
const QKeyEvent*>(event)->key()) {
4320 case Qt::Key_Backtab:
4321 modifiers = modifiers & ~Qt::ShiftModifier;
4329 case Qt::Key_PageUp:
4330 case Qt::Key_PageDown:
4332 if (modifiers & Qt::ControlModifier)
4333 return QItemSelectionModel::NoUpdate;
4335 case Qt::Key_Select:
4336 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4338 if (modifiers & Qt::ControlModifier)
4339 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4340 return QItemSelectionModel::Select|selectionBehaviorFlags();
4351 if (modifiers & Qt::ShiftModifier)
4352 return QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4353 if (modifiers & Qt::ControlModifier)
4354 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4355 if (state == QAbstractItemView::DragSelectingState) {
4357 return QItemSelectionModel::Clear|QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4360 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();