190 for (
int role : roles) {
191 if (role == Qt::AccessibleTextRole
192 || (role == Qt::DisplayRole
193 && index.data(Qt::AccessibleTextRole).toString().isEmpty())) {
194 QAccessibleEvent event(q, QAccessible::NameChanged);
195 event.setChild(childIndex);
196 QAccessible::updateAccessibility(&event);
197 }
else if (role == Qt::AccessibleDescriptionRole) {
198 QAccessibleEvent event(q, QAccessible::DescriptionChanged);
199 event.setChild(childIndex);
200 QAccessible::updateAccessibility(&event);
201 }
else if (role == Qt::CheckStateRole) {
202 QAccessible::State state;
203 state.checked =
true;
204 QAccessibleStateChangeEvent event(q, state);
205 event.setChild(childIndex);
206 QAccessible::updateAccessibility(&event);
212#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
215void QAbstractItemViewPrivate::scrollerStateChanged()
217 Q_Q(QAbstractItemView);
219 if (QScroller *scroller = QScroller::scroller(viewport)) {
220 switch (scroller->state()) {
221 case QScroller::Pressed:
223 if (q->selectionModel()) {
224 oldSelection = q->selectionModel()->selection();
225 oldCurrent = q->selectionModel()->currentIndex();
229 case QScroller::Dragging:
231 if (q->selectionModel()) {
232 q->selectionModel()->select(oldSelection, QItemSelectionModel::ClearAndSelect);
234 const bool wasAutoScroll = autoScroll;
236 q->selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
237 autoScroll = wasAutoScroll;
242 oldSelection = QItemSelection();
243 oldCurrent = QModelIndex();
251void QAbstractItemViewPrivate::delegateSizeHintChanged(
const QModelIndex &index)
253 Q_Q(QAbstractItemView);
255 if (!model->checkIndex(index))
256 qWarning(
"Delegate size hint changed for a model index that does not belong to this view");
258 QMetaObject::invokeMethod(q, &QAbstractItemView::doItemsLayout, Qt::QueuedConnection);
261void QAbstractItemViewPrivate::connectDelegate(QAbstractItemDelegate *delegate)
265 Q_Q(QAbstractItemView);
266 QObject::connect(delegate, &QAbstractItemDelegate::closeEditor,
267 q, &QAbstractItemView::closeEditor);
268 QObject::connect(delegate, &QAbstractItemDelegate::commitData,
269 q, &QAbstractItemView::commitData);
270 QObjectPrivate::connect(delegate, &QAbstractItemDelegate::sizeHintChanged,
271 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
274void QAbstractItemViewPrivate::disconnectDelegate(QAbstractItemDelegate *delegate)
278 Q_Q(QAbstractItemView);
279 QObject::disconnect(delegate, &QAbstractItemDelegate::closeEditor,
280 q, &QAbstractItemView::closeEditor);
281 QObject::disconnect(delegate, &QAbstractItemDelegate::commitData,
282 q, &QAbstractItemView::commitData);
283 QObjectPrivate::disconnect(delegate, &QAbstractItemDelegate::sizeHintChanged,
284 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
287void QAbstractItemViewPrivate::disconnectAll()
289 Q_Q(QAbstractItemView);
290 for (QMetaObject::Connection &connection : modelConnections)
291 QObject::disconnect(connection);
292 for (QMetaObject::Connection &connection : scrollbarConnections)
293 QObject::disconnect(connection);
294 disconnectDelegate(itemDelegate);
295 for (QAbstractItemDelegate *delegate : std::as_const(rowDelegates))
296 disconnectDelegate(delegate);
297 for (QAbstractItemDelegate *delegate : std::as_const(columnDelegates))
298 disconnectDelegate(delegate);
299 if (model && selectionModel) {
300 QObject::disconnect(model, &QAbstractItemModel::destroyed,
301 selectionModel, &QItemSelectionModel::deleteLater);
303 if (selectionModel) {
304 QObject::disconnect(selectionModel, &QItemSelectionModel::selectionChanged,
305 q, &QAbstractItemView::selectionChanged);
306 QObject::disconnect(selectionModel, &QItemSelectionModel::currentChanged,
307 q, &QAbstractItemView::currentChanged);
309 for (
const auto &info : std::as_const(indexEditorHash)) {
310 if (!info.isStatic && info.widget)
311 QObject::disconnect(info.widget, &QWidget::destroyed, q, &QAbstractItemView::editorDestroyed);
313#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
314 QObject::disconnect(scollerConnection);
319
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
422
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
455
456
457
458
459
460
463
464
465
466
467
468
469
470
471
472
476
477
478
479
480
481
482
483
484
485
486
487
488
489
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
510
511
512
513
514
515
516
517
518
519
520
521
522
525
526
527
528
529
530
531
532
533
534
537
538
539
540
541
542
543
544
545
546
547
550
551
552
553
554
555
556
559
560
561
562
563
564
565
566
569
570
571
572
573
574
575
576
577
580
581
582
583
584
585
586
587
590
591
592
593
594
595
596
599
600
601
602
603
604
605
606
607
608
609
612
613
614
615
616
617
618
619
622
623
624
625
626
627
628
629
632
633
634
635
636
637
638
639
642
643
644
645
646
647
648
649
652
653
654
655
656
657
658
659
662
663
664
665
666
667
668
669
670
671
674
675
676
677
678
679
680
681
682
683
684
685
688
689
690
691
692
693
694
695
696
699
700
701QAbstractItemView::QAbstractItemView(QWidget *parent)
702 : QAbstractScrollArea(*(
new QAbstractItemViewPrivate), parent)
708
709
710QAbstractItemView::QAbstractItemView(QAbstractItemViewPrivate &dd, QWidget *parent)
711 : QAbstractScrollArea(dd, parent)
717
718
719QAbstractItemView::~QAbstractItemView()
721 Q_D(QAbstractItemView);
723 d->delayedReset.stop();
724 d->updateTimer.stop();
725 d->delayedEditing.stop();
726 d->delayedAutoScroll.stop();
727 d->autoScrollTimer.stop();
728 d->delayedLayout.stop();
729 d->fetchMoreTimer.stop();
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753void QAbstractItemView::setModel(QAbstractItemModel *model)
755 Q_D(QAbstractItemView);
756 if (model == d->model)
758 if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
759 for (QMetaObject::Connection &connection : d->modelConnections)
760 disconnect(connection);
762 d->model = (model ? model : QAbstractItemModelPrivate::staticEmptyModel());
764 if (d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
765 d->modelConnections = {
766 QObjectPrivate::connect(d->model, &QAbstractItemModel::destroyed,
767 d, &QAbstractItemViewPrivate::modelDestroyed),
768 QObject::connect(d->model, &QAbstractItemModel::dataChanged,
769 this, &QAbstractItemView::dataChanged),
770 QObjectPrivate::connect(d->model, &QAbstractItemModel::headerDataChanged,
771 d, &QAbstractItemViewPrivate::headerDataChanged),
772 QObject::connect(d->model, &QAbstractItemModel::rowsInserted,
773 this, &QAbstractItemView::rowsInserted),
774 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsInserted,
775 d, &QAbstractItemViewPrivate::rowsInserted),
776 QObject::connect(d->model, &QAbstractItemModel::rowsAboutToBeRemoved,
777 this, &QAbstractItemView::rowsAboutToBeRemoved),
778 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsRemoved,
779 d, &QAbstractItemViewPrivate::rowsRemoved),
780 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsMoved,
781 d, &QAbstractItemViewPrivate::rowsMoved),
782 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeRemoved,
783 d, &QAbstractItemViewPrivate::columnsAboutToBeRemoved),
784 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsRemoved,
785 d, &QAbstractItemViewPrivate::columnsRemoved),
786 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsInserted,
787 d, &QAbstractItemViewPrivate::columnsInserted),
788 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsMoved,
789 d, &QAbstractItemViewPrivate::columnsMoved),
790 QObject::connect(d->model, &QAbstractItemModel::modelReset,
791 this, &QAbstractItemView::reset),
792 QObjectPrivate::connect(d->model, &QAbstractItemModel::layoutChanged,
793 d, &QAbstractItemViewPrivate::layoutChanged),
797 QItemSelectionModel *selection_model =
new QItemSelectionModel(d->model,
this);
798 connect(d->model, &QAbstractItemModel::destroyed,
799 selection_model, &QItemSelectionModel::deleteLater);
800 setSelectionModel(selection_model);
806
807
808QAbstractItemModel *QAbstractItemView::model()
const
810 Q_D(
const QAbstractItemView);
811 return (d->model == QAbstractItemModelPrivate::staticEmptyModel() ?
nullptr : d->model);
815
816
817
818
819
820
821
822
823
824
825
826
827
828void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
831 Q_ASSERT(selectionModel);
832 Q_D(QAbstractItemView);
834 if (Q_UNLIKELY(selectionModel->model() != d->model)) {
835 qWarning(
"QAbstractItemView::setSelectionModel() failed: "
836 "Trying to set a selection model, which works on "
837 "a different model than the view.");
841 QItemSelection oldSelection;
842 QModelIndex oldCurrentIndex;
844 if (d->selectionModel) {
845 if (d->selectionModel->model() == selectionModel->model()) {
846 oldSelection = d->selectionModel->selection();
847 oldCurrentIndex = d->selectionModel->currentIndex();
849 disconnect(d->selectionModel, &QItemSelectionModel::selectionChanged,
850 this, &QAbstractItemView::selectionChanged);
851 disconnect(d->selectionModel, &QItemSelectionModel::currentChanged,
852 this, &QAbstractItemView::currentChanged);
855 d->selectionModel = selectionModel;
857 if (d->selectionModel) {
858 connect(d->selectionModel, &QItemSelectionModel::selectionChanged,
859 this, &QAbstractItemView::selectionChanged);
860 connect(d->selectionModel, &QItemSelectionModel::currentChanged,
861 this, &QAbstractItemView::currentChanged);
863 selectionChanged(d->selectionModel->selection(), oldSelection);
864 currentChanged(d->selectionModel->currentIndex(), oldCurrentIndex);
869
870
871
872
873QItemSelectionModel* QAbstractItemView::selectionModel()
const
875 Q_D(
const QAbstractItemView);
876 return d->selectionModel;
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
896 Q_D(QAbstractItemView);
897 if (delegate == d->itemDelegate)
900 if (d->itemDelegate) {
901 if (d->delegateRefCount(d->itemDelegate) == 1)
902 d->disconnectDelegate(d->itemDelegate);
906 if (d->delegateRefCount(delegate) == 0)
907 d->connectDelegate(delegate);
909 d->itemDelegate = delegate;
910 viewport()->update();
911 d->doDelayedItemsLayout();
915
916
917
918
919
920QAbstractItemDelegate *QAbstractItemView::itemDelegate()
const
922 return d_func()->itemDelegate;
926
927
928QVariant QAbstractItemView::inputMethodQuery(Qt::InputMethodQuery query)
const
930 Q_D(
const QAbstractItemView);
931 const QModelIndex current = currentIndex();
933 if (current.isValid()) {
934 if (QWidget *currentEditor;
935 d->waitForIMCommit && (currentEditor = d->editorForIndex(current).widget)) {
938 result = currentEditor->inputMethodQuery(query);
939 if (result.typeId() == QMetaType::QRect) {
940 const QRect editorRect = result.value<QRect>();
941 result = QRect(currentEditor->mapTo(
this, editorRect.topLeft()), editorRect.size());
943 }
else if (query == Qt::ImCursorRectangle) {
944 const QRect visRect = visualRect(current);
945 result = QRect(d->viewport->mapTo(
this, visRect.topLeft()), visRect.size());
948 if (!result.isValid())
949 result = QAbstractScrollArea::inputMethodQuery(query);
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972void QAbstractItemView::setItemDelegateForRow(
int row, QAbstractItemDelegate *delegate)
974 Q_D(QAbstractItemView);
975 if (QAbstractItemDelegate *rowDelegate = d->rowDelegates.value(row,
nullptr)) {
976 if (d->delegateRefCount(rowDelegate) == 1)
977 d->disconnectDelegate(rowDelegate);
978 d->rowDelegates.remove(row);
981 if (d->delegateRefCount(delegate) == 0)
982 d->connectDelegate(delegate);
983 d->rowDelegates.insert(row, delegate);
985 viewport()->update();
986 d->doDelayedItemsLayout();
990
991
992
993
994
995
996QAbstractItemDelegate *QAbstractItemView::itemDelegateForRow(
int row)
const
998 Q_D(
const QAbstractItemView);
999 return d->rowDelegates.value(row,
nullptr);
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020void QAbstractItemView::setItemDelegateForColumn(
int column, QAbstractItemDelegate *delegate)
1022 Q_D(QAbstractItemView);
1023 if (QAbstractItemDelegate *columnDelegate = d->columnDelegates.value(column,
nullptr)) {
1024 if (d->delegateRefCount(columnDelegate) == 1)
1025 d->disconnectDelegate(columnDelegate);
1026 d->columnDelegates.remove(column);
1029 if (d->delegateRefCount(delegate) == 0)
1030 d->connectDelegate(delegate);
1031 d->columnDelegates.insert(column, delegate);
1033 viewport()->update();
1034 d->doDelayedItemsLayout();
1038
1039
1040
1041
1042
1043
1044QAbstractItemDelegate *QAbstractItemView::itemDelegateForColumn(
int column)
const
1046 Q_D(
const QAbstractItemView);
1047 return d->columnDelegates.value(column,
nullptr);
1051
1052
1053
1054
1055
1058
1059
1060
1061
1062
1063
1064
1065QAbstractItemDelegate *QAbstractItemView::itemDelegateForIndex(
const QModelIndex &index)
const
1067 Q_D(
const QAbstractItemView);
1068 return d->delegateForIndex(index);
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081void QAbstractItemView::setSelectionMode(SelectionMode mode)
1083 Q_D(QAbstractItemView);
1084 d->selectionMode = mode;
1087QAbstractItemView::SelectionMode QAbstractItemView::selectionMode()
const
1089 Q_D(
const QAbstractItemView);
1090 return d->selectionMode;
1094
1095
1096
1097
1098
1099
1100
1101
1103void QAbstractItemView::setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
1105 Q_D(QAbstractItemView);
1106 d->selectionBehavior = behavior;
1109QAbstractItemView::SelectionBehavior QAbstractItemView::selectionBehavior()
const
1111 Q_D(
const QAbstractItemView);
1112 return d->selectionBehavior;
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129void QAbstractItemView::setCurrentIndex(
const QModelIndex &index)
1131 Q_D(QAbstractItemView);
1132 if (d->selectionModel && (!index.isValid() || d->isIndexEnabled(index))) {
1133 QItemSelectionModel::SelectionFlags command = selectionCommand(index,
nullptr);
1134 d->selectionModel->setCurrentIndex(index, command);
1135 d->currentIndexSet =
true;
1140
1141
1142
1143
1144QModelIndex QAbstractItemView::currentIndex()
const
1146 Q_D(
const QAbstractItemView);
1147 return d->selectionModel ? d->selectionModel->currentIndex() : QModelIndex();
1152
1153
1154
1155
1156
1157
1158
1159
1160void QAbstractItemView::reset()
1162 Q_D(QAbstractItemView);
1163 d->delayedReset.stop();
1167 const auto copy = d->indexEditorHash;
1168 for (
const auto &[index, info] : copy.asKeyValueRange()) {
1170 d->releaseEditor(info.widget.data(), d->indexForEditor(info.widget.data()));
1172 d->editorIndexHash.clear();
1173 d->indexEditorHash.clear();
1174 d->persistent.clear();
1175 d->currentIndexSet =
false;
1177 setRootIndex(QModelIndex());
1178 if (d->selectionModel)
1179 d->selectionModel->reset();
1180#if QT_CONFIG(accessibility)
1181 if (QAccessible::isActive()) {
1182 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1183 QAccessible::updateAccessibility(&accessibleEvent);
1186 d->updateGeometry();
1190
1191
1192
1193
1194void QAbstractItemView::setRootIndex(
const QModelIndex &index)
1196 Q_D(QAbstractItemView);
1197 if (Q_UNLIKELY(index.isValid() && index.model() != d->model)) {
1198 qWarning(
"QAbstractItemView::setRootIndex failed : index must be from the currently set model");
1202#if QT_CONFIG(accessibility)
1203 if (QAccessible::isActive()) {
1204 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1205 QAccessible::updateAccessibility(&accessibleEvent);
1208 d->doDelayedItemsLayout();
1209 d->updateGeometry();
1213
1214
1215
1216
1217
1218QModelIndex QAbstractItemView::rootIndex()
const
1220 return QModelIndex(d_func()->root);
1224
1225
1226
1227
1228
1229
1230void QAbstractItemView::selectAll()
1232 Q_D(QAbstractItemView);
1233 const SelectionMode mode = d->selectionMode;
1235 case MultiSelection:
1236 case ExtendedSelection:
1237 d->selectAll(QItemSelectionModel::ClearAndSelect
1238 | d->selectionBehaviorFlags());
1241 case ContiguousSelection:
1242 if (d->model->hasChildren(d->root))
1243 d->selectAll(selectionCommand(d->model->index(0, 0, d->root)));
1245 case SingleSelection:
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262void QAbstractItemView::edit(
const QModelIndex &index)
1264 Q_D(QAbstractItemView);
1265 if (Q_UNLIKELY(!d->isIndexValid(index)))
1266 qWarning(
"edit: index was invalid");
1267 if (Q_UNLIKELY(!edit(index, AllEditTriggers,
nullptr)))
1268 qWarning(
"edit: editing failed");
1272
1273
1274
1275
1276void QAbstractItemView::clearSelection()
1278 Q_D(QAbstractItemView);
1279 if (d->selectionModel)
1280 d->selectionModel->clearSelection();
1284
1285
1286
1287
1288
1289void QAbstractItemView::doItemsLayout()
1291 Q_D(QAbstractItemView);
1292 d->interruptDelayedItemsLayout();
1294 d->viewport->update();
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312void QAbstractItemView::setEditTriggers(EditTriggers actions)
1314 Q_D(QAbstractItemView);
1315 d->editTriggers = actions;
1318QAbstractItemView::EditTriggers QAbstractItemView::editTriggers()
const
1320 Q_D(
const QAbstractItemView);
1321 return d->editTriggers;
1325
1326
1327
1328
1329
1330
1331
1333void QAbstractItemView::setVerticalScrollMode(ScrollMode mode)
1335 Q_D(QAbstractItemView);
1336 d->verticalScrollModeSet =
true;
1337 if (mode == d->verticalScrollMode)
1339 QModelIndex topLeft = indexAt(QPoint(0, 0));
1340 d->verticalScrollMode = mode;
1341 if (mode == ScrollPerItem)
1342 verticalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1344 verticalScrollBar()->setSingleStep(-1);
1346 scrollTo(topLeft, QAbstractItemView::PositionAtTop);
1349QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode()
const
1351 Q_D(
const QAbstractItemView);
1352 return d->verticalScrollMode;
1355void QAbstractItemView::resetVerticalScrollMode()
1357 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1358 setVerticalScrollMode(sm);
1359 d_func()->verticalScrollModeSet =
false;
1363
1364
1365
1366
1367
1368
1369
1371void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode)
1373 Q_D(QAbstractItemView);
1374 d->horizontalScrollModeSet =
true;
1375 if (mode == d->horizontalScrollMode)
1377 d->horizontalScrollMode = mode;
1378 if (mode == ScrollPerItem)
1379 horizontalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1381 horizontalScrollBar()->setSingleStep(-1);
1385QAbstractItemView::ScrollMode QAbstractItemView::horizontalScrollMode()
const
1387 Q_D(
const QAbstractItemView);
1388 return d->horizontalScrollMode;
1391void QAbstractItemView::resetHorizontalScrollMode()
1393 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1394 setHorizontalScrollMode(sm);
1395 d_func()->horizontalScrollModeSet =
false;
1398#if QT_CONFIG(draganddrop)
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419void QAbstractItemView::setDragDropOverwriteMode(
bool overwrite)
1421 Q_D(QAbstractItemView);
1422 d->overwrite = overwrite;
1425bool QAbstractItemView::dragDropOverwriteMode()
const
1427 Q_D(
const QAbstractItemView);
1428 return d->overwrite;
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1446void QAbstractItemView::setAutoScroll(
bool enable)
1448 Q_D(QAbstractItemView);
1449 d->autoScroll = enable;
1452bool QAbstractItemView::hasAutoScroll()
const
1454 Q_D(
const QAbstractItemView);
1455 return d->autoScroll;
1459
1460
1461
1462
1463
1464
1465void QAbstractItemView::setAutoScrollMargin(
int margin)
1467 Q_D(QAbstractItemView);
1468 d->autoScrollMargin = margin;
1471int QAbstractItemView::autoScrollMargin()
const
1473 Q_D(
const QAbstractItemView);
1474 return d->autoScrollMargin;
1478
1479
1480
1482void QAbstractItemView::setTabKeyNavigation(
bool enable)
1484 Q_D(QAbstractItemView);
1485 d->tabKeyNavigation = enable;
1488bool QAbstractItemView::tabKeyNavigation()
const
1490 Q_D(
const QAbstractItemView);
1491 return d->tabKeyNavigation;
1495
1496
1497
1498QSize QAbstractItemView::viewportSizeHint()
const
1500 return QAbstractScrollArea::viewportSizeHint();
1503#if QT_CONFIG(draganddrop)
1505
1506
1507
1508
1509
1511void QAbstractItemView::setDropIndicatorShown(
bool enable)
1513 Q_D(QAbstractItemView);
1514 d->showDropIndicator = enable;
1517bool QAbstractItemView::showDropIndicator()
const
1519 Q_D(
const QAbstractItemView);
1520 return d->showDropIndicator;
1524
1525
1526
1527
1528
1530void QAbstractItemView::setDragEnabled(
bool enable)
1532 Q_D(QAbstractItemView);
1533 d->dragEnabled = enable;
1536bool QAbstractItemView::dragEnabled()
const
1538 Q_D(
const QAbstractItemView);
1539 return d->dragEnabled;
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1562
1563
1564
1565
1566
1567void QAbstractItemView::setDragDropMode(DragDropMode behavior)
1569 Q_D(QAbstractItemView);
1570 d->dragDropMode = behavior;
1571 setDragEnabled(behavior == DragOnly || behavior == DragDrop || behavior == InternalMove);
1572 setAcceptDrops(behavior == DropOnly || behavior == DragDrop || behavior == InternalMove);
1575QAbstractItemView::DragDropMode QAbstractItemView::dragDropMode()
const
1577 Q_D(
const QAbstractItemView);
1578 DragDropMode setBehavior = d->dragDropMode;
1579 if (!dragEnabled() && !acceptDrops())
1582 if (dragEnabled() && !acceptDrops())
1585 if (!dragEnabled() && acceptDrops())
1588 if (dragEnabled() && acceptDrops()) {
1589 if (setBehavior == InternalMove)
1599
1600
1601
1602
1603
1604
1605
1606
1607void QAbstractItemView::setDefaultDropAction(Qt::DropAction dropAction)
1609 Q_D(QAbstractItemView);
1610 d->defaultDropAction = dropAction;
1613Qt::DropAction QAbstractItemView::defaultDropAction()
const
1615 Q_D(
const QAbstractItemView);
1616 return d->defaultDropAction;
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631void QAbstractItemView::setAlternatingRowColors(
bool enable)
1633 Q_D(QAbstractItemView);
1634 d->alternatingColors = enable;
1636 d->viewport->update();
1639bool QAbstractItemView::alternatingRowColors()
const
1641 Q_D(
const QAbstractItemView);
1642 return d->alternatingColors;
1646
1647
1648
1649
1650
1651
1652void QAbstractItemView::setIconSize(
const QSize &size)
1654 Q_D(QAbstractItemView);
1655 if (size == d->iconSize)
1658 d->doDelayedItemsLayout();
1659 emit iconSizeChanged(size);
1662QSize QAbstractItemView::iconSize()
const
1664 Q_D(
const QAbstractItemView);
1669
1670
1671
1672
1673
1674
1675void QAbstractItemView::setTextElideMode(Qt::TextElideMode mode)
1677 Q_D(QAbstractItemView);
1678 d->textElideMode = mode;
1681Qt::TextElideMode QAbstractItemView::textElideMode()
const
1683 return d_func()->textElideMode;
1687
1688
1689bool QAbstractItemView::focusNextPrevChild(
bool next)
1691 Q_D(QAbstractItemView);
1692 if (d->tabKeyNavigation && isVisible() && isEnabled() && d->viewport->isEnabled()) {
1693 QKeyEvent event(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier);
1694 keyPressEvent(&event);
1695 if (event.isAccepted())
1698 return QAbstractScrollArea::focusNextPrevChild(next);
1702
1703
1704bool QAbstractItemView::event(QEvent *event)
1706 Q_D(QAbstractItemView);
1707 switch (event->type()) {
1711 d->executePostedLayout();
1714 d->executePostedLayout();
1715 if (d->shouldScrollToCurrentOnShow) {
1716 d->shouldScrollToCurrentOnShow =
false;
1717 const QModelIndex current = currentIndex();
1718 if (current.isValid() && (d->state == QAbstractItemView::EditingState || d->autoScroll))
1722 case QEvent::LocaleChange:
1723 viewport()->update();
1725 case QEvent::LayoutDirectionChange:
1726 case QEvent::ApplicationLayoutDirectionChange:
1729 case QEvent::StyleChange:
1731 if (!d->verticalScrollModeSet)
1732 resetVerticalScrollMode();
1733 if (!d->horizontalScrollModeSet)
1734 resetHorizontalScrollMode();
1736 case QEvent::FocusOut:
1737 d->checkPersistentEditorFocus();
1739 case QEvent::FontChange:
1740 d->doDelayedItemsLayout();
1745 return QAbstractScrollArea::event(event);
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759bool QAbstractItemView::viewportEvent(QEvent *event)
1761 Q_D(QAbstractItemView);
1762 switch (event->type()) {
1767 d->executePostedLayout();
1769 case QEvent::HoverMove:
1770 case QEvent::HoverEnter:
1771 d->setHoverIndex(indexAt(
static_cast<QHoverEvent*>(event)->position().toPoint()));
1773 case QEvent::HoverLeave:
1774 d->setHoverIndex(QModelIndex());
1777 d->viewportEnteredNeeded =
true;
1780 d->setHoverIndex(QModelIndex());
1781 #if QT_CONFIG(statustip)
1782 if (d->shouldClearStatusTip && d->parent) {
1784 QStatusTipEvent tip(empty);
1785 QCoreApplication::sendEvent(d->parent, &tip);
1786 d->shouldClearStatusTip =
false;
1789 d->enteredIndex = QModelIndex();
1791 case QEvent::ToolTip:
1792 case QEvent::QueryWhatsThis:
1793 case QEvent::WhatsThis: {
1794 QHelpEvent *he =
static_cast<QHelpEvent*>(event);
1795 const QModelIndex index = indexAt(he->pos());
1796 QStyleOptionViewItem option;
1797 initViewItemOption(&option);
1798 option.rect = visualRect(index);
1799 option.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
1801 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
1804 return delegate->helpEvent(he,
this, option, index);
1806 case QEvent::FontChange:
1807 d->doDelayedItemsLayout();
1809 case QEvent::WindowActivate:
1810 case QEvent::WindowDeactivate:
1811 d->viewport->update();
1813 case QEvent::ScrollPrepare:
1814 executeDelayedItemsLayout();
1815#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
1816 d->scollerConnection = QObjectPrivate::connect(
1817 QScroller::scroller(d->viewport), &QScroller::stateChanged,
1818 d, &QAbstractItemViewPrivate::scrollerStateChanged,
1819 Qt::UniqueConnection);
1826 return QAbstractScrollArea::viewportEvent(event);
1830
1831
1832
1833
1834void QAbstractItemView::mousePressEvent(QMouseEvent *event)
1836 Q_D(QAbstractItemView);
1837 d->releaseFromDoubleClick =
false;
1838 d->delayedAutoScroll.stop();
1839 QPoint pos = event->position().toPoint();
1840 QPersistentModelIndex index = indexAt(pos);
1843 d->pressClosedEditor = d->pressClosedEditorWatcher.isActive() && d->lastEditedIndex == index;
1845 if (!d->selectionModel || (d->state == EditingState && d->hasEditor(index)))
1848 d->pressedAlreadySelected = d->selectionModel->isSelected(index);
1849 d->pressedIndex = index;
1850 d->pressedModifiers = event->modifiers();
1851 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1852 d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid();
1853 QPoint offset = d->offset();
1854 d->draggedPosition = pos + offset;
1856#if QT_CONFIG(draganddrop)
1859 d->pressedPosition = d->draggedPosition;
1862 if (!(command & QItemSelectionModel::Current)) {
1863 d->pressedPosition = pos + offset;
1864 d->currentSelectionStartIndex = index;
1866 else if (!d->currentSelectionStartIndex.isValid())
1867 d->currentSelectionStartIndex = currentIndex();
1869 if (edit(index, NoEditTriggers, event))
1872 if (index.isValid() && d->isIndexEnabled(index)) {
1875 bool autoScroll = d->autoScroll;
1876 d->autoScroll =
false;
1877 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1878 d->autoScroll = autoScroll;
1879 if (command.testFlag(QItemSelectionModel::Toggle)) {
1880 command &= ~QItemSelectionModel::Toggle;
1881 d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
1882 command |= d->ctrlDragSelectionFlag;
1885 if (!(command & QItemSelectionModel::Current)) {
1886 setSelection(QRect(pos, QSize(1, 1)), command);
1888 QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
1889 setSelection(rect, command);
1893 emit pressed(index);
1894 if (d->autoScroll) {
1897 d->delayedAutoScroll.start(QApplication::doubleClickInterval()+100,
this);
1902 d->selectionModel->select(QModelIndex(), QItemSelectionModel::Select);
1907
1908
1909
1910
1911void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
1913 Q_D(QAbstractItemView);
1914 QPoint bottomRight = event->position().toPoint();
1916 d->draggedPosition = bottomRight + d->offset();
1918 if (state() == ExpandingState || state() == CollapsingState)
1921#if QT_CONFIG(draganddrop)
1922 if (state() == DraggingState) {
1923 d->maybeStartDrag(bottomRight);
1928 QPersistentModelIndex index = indexAt(bottomRight);
1929 QModelIndex buddy = d->model->buddy(d->pressedIndex);
1930 if ((state() == EditingState && d->hasEditor(buddy))
1931 || edit(index, NoEditTriggers, event))
1934 const QPoint topLeft =
1935 d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight;
1937 d->checkMouseMove(index);
1939#if QT_CONFIG(draganddrop)
1940 if (d->pressedIndex.isValid()
1942 && (state() != DragSelectingState)
1943 && (event->buttons() != Qt::NoButton)
1944 && !d->selectedDraggableIndexes().isEmpty()) {
1945 setState(DraggingState);
1946 d->maybeStartDrag(bottomRight);
1951 if ((event->buttons() & Qt::LeftButton) && d->selectionAllowed(index) && d->selectionModel) {
1952 setState(DragSelectingState);
1953 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1954 if (d->ctrlDragSelectionFlag != QItemSelectionModel::NoUpdate && command.testFlag(QItemSelectionModel::Toggle)) {
1955 command &= ~QItemSelectionModel::Toggle;
1956 command |= d->ctrlDragSelectionFlag;
1960 QRect selectionRect = QRect(topLeft, bottomRight);
1961 setSelection(selectionRect, command);
1964 if (index.isValid() && (index != d->selectionModel->currentIndex()) && d->isIndexEnabled(index))
1965 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1966 else if (d->shouldAutoScroll(event->pos()) && !d->autoScrollTimer.isActive())
1972
1973
1974
1975
1976
1977
1978void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
1980 Q_D(QAbstractItemView);
1981 const bool releaseFromDoubleClick = d->releaseFromDoubleClick;
1982 d->releaseFromDoubleClick =
false;
1984 QPoint pos = event->position().toPoint();
1985 QPersistentModelIndex index = indexAt(pos);
1987 if (state() == EditingState) {
1988 if (d->isIndexValid(index)
1989 && d->isIndexEnabled(index)
1990 && d->sendDelegateEvent(index, event))
1995 bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
1996 bool selectedClicked = click && d->pressedAlreadySelected
1997 && (event->button() == Qt::LeftButton)
1998 && (event->modifiers() == Qt::NoModifier);
1999 EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
2000 const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) :
false;
2002 d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
2004 if (d->selectionModel && d->noSelectionOnMousePress) {
2005 d->noSelectionOnMousePress =
false;
2006 if (!d->pressClosedEditor)
2007 d->selectionModel->select(index, selectionCommand(index, event));
2010 d->pressClosedEditor =
false;
2014 if (event->button() == Qt::LeftButton)
2015 emit clicked(index);
2018 QStyleOptionViewItem option;
2019 initViewItemOption(&option);
2020 if (d->pressedAlreadySelected)
2021 option.state |= QStyle::State_Selected;
2022 if ((d->model->flags(index) & Qt::ItemIsEnabled)
2023 && style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option,
this))
2024 emit activated(index);
2029
2030
2031
2032
2033void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
2035 Q_D(QAbstractItemView);
2037 QModelIndex index = indexAt(event->position().toPoint());
2038 if (!index.isValid()
2039 || !d->isIndexEnabled(index)
2040 || (d->pressedIndex != index)) {
2041 QMouseEvent me(QEvent::MouseButtonPress,
2042 event->position(), event->scenePosition(), event->globalPosition(),
2043 event->button(), event->buttons(), event->modifiers(),
2044 event->source(), event->pointingDevice());
2045 mousePressEvent(&me);
2049 QPersistentModelIndex persistent = index;
2050 emit doubleClicked(persistent);
2051 if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
2052 && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick,
nullptr,
this))
2053 emit activated(persistent);
2054 d->releaseFromDoubleClick =
true;
2057#if QT_CONFIG(draganddrop)
2060
2061
2062
2063
2064
2065
2066void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
2068 if (dragDropMode() == InternalMove
2069 && (event->source() !=
this|| !(event->possibleActions() & Qt::MoveAction)))
2072 if (d_func()->canDrop(event)) {
2074 setState(DraggingState);
2081
2082
2083
2084
2085
2086
2087
2088void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
2090 Q_D(QAbstractItemView);
2091 d->draggedPosition = event->position().toPoint() + d->offset();
2092 if (dragDropMode() == InternalMove
2093 && (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction)))
2099 QModelIndex index = indexAt(event->position().toPoint());
2101 if (!d->droppingOnItself(event, index)
2102 && d->canDrop(event)) {
2104 if (index.isValid() && d->showDropIndicator) {
2105 QRect rect = visualRect(index);
2106 d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
2107 if (d->selectionBehavior == QAbstractItemView::SelectRows
2108 && d->dropIndicatorPosition != OnViewport
2109 && (d->dropIndicatorPosition != OnItem || event->source() ==
this)) {
2110 const int maxCol = d->model->columnCount(index.parent()) - 1;
2111 const auto idx = index.column() > 0 ? index.siblingAtColumn(0) : index;
2112 rect = d->intersectedRect(viewport()->rect(), idx, idx.siblingAtColumn(maxCol));
2114 switch (d->dropIndicatorPosition) {
2116 if (d->isIndexDropEnabled(index.parent())) {
2117 d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0);
2118 event->acceptProposedAction();
2120 d->dropIndicatorRect = QRect();
2124 if (d->isIndexDropEnabled(index.parent())) {
2125 d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0);
2126 event->acceptProposedAction();
2128 d->dropIndicatorRect = QRect();
2132 if (d->isIndexDropEnabled(index)) {
2133 d->dropIndicatorRect = rect;
2134 event->acceptProposedAction();
2136 d->dropIndicatorRect = QRect();
2140 d->dropIndicatorRect = QRect();
2141 if (d->isIndexDropEnabled(rootIndex())) {
2142 event->acceptProposedAction();
2147 d->dropIndicatorRect = QRect();
2148 d->dropIndicatorPosition = OnViewport;
2149 if (d->isIndexDropEnabled(rootIndex())) {
2150 event->acceptProposedAction();
2153 d->viewport->update();
2156 if (d->shouldAutoScroll(event->position().toPoint()))
2161
2162
2163
2164
2165bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event,
const QModelIndex &index)
2167 Q_Q(QAbstractItemView);
2168 Qt::DropAction dropAction = event->dropAction();
2169 if (q->dragDropMode() == QAbstractItemView::InternalMove)
2170 dropAction = Qt::MoveAction;
2171 if (event->source() == q
2172 && event->possibleActions() & Qt::MoveAction
2173 && dropAction == Qt::MoveAction) {
2174 QModelIndexList selectedIndexes = q->selectedIndexes();
2175 QModelIndex child = index;
2176 while (child.isValid() && child != root) {
2177 if (selectedIndexes.contains(child))
2179 child = child.parent();
2186
2187
2188
2189
2190
2191void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *)
2193 Q_D(QAbstractItemView);
2196 d->hover = QModelIndex();
2197 d->viewport->update();
2201
2202
2203
2204
2205
2206
2207void QAbstractItemView::dropEvent(QDropEvent *event)
2209 Q_D(QAbstractItemView);
2210 if (dragDropMode() == InternalMove) {
2211 if (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction))
2218 if (d->dropOn(event, &row, &col, &index)) {
2219 const Qt::DropAction action = dragDropMode() == InternalMove ? Qt::MoveAction : event->dropAction();
2220 if (d->model->dropMimeData(event->mimeData(), action, row, col, index)) {
2221 if (action != event->dropAction()) {
2222 event->setDropAction(action);
2225 event->acceptProposedAction();
2231 d->viewport->update();
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245bool QAbstractItemViewPrivate::dropOn(QDropEvent *event,
int *dropRow,
int *dropCol, QModelIndex *dropIndex)
2247 Q_Q(QAbstractItemView);
2248 if (event->isAccepted())
2253 if (viewport->rect().contains(event->position().toPoint())) {
2254 index = q->indexAt(event->position().toPoint());
2255 if (!index.isValid())
2260 if (model->supportedDropActions() & event->dropAction()) {
2263 if (index != root) {
2264 dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
2265 switch (dropIndicatorPosition) {
2266 case QAbstractItemView::AboveItem:
2268 col = index.column();
2269 index = index.parent();
2271 case QAbstractItemView::BelowItem:
2272 row = index.row() + 1;
2273 col = index.column();
2274 index = index.parent();
2276 case QAbstractItemView::OnItem:
2277 case QAbstractItemView::OnViewport:
2281 dropIndicatorPosition = QAbstractItemView::OnViewport;
2286 if (!droppingOnItself(event, index))
2292QAbstractItemView::DropIndicatorPosition
2293QAbstractItemViewPrivate::position(
const QPoint &pos,
const QRect &rect,
const QModelIndex &index)
const
2295 QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport;
2297 const int margin = qBound(2, qRound(qreal(rect.height()) / 5.5), 12);
2298 if (pos.y() - rect.top() < margin) {
2299 r = QAbstractItemView::AboveItem;
2300 }
else if (rect.bottom() - pos.y() < margin) {
2301 r = QAbstractItemView::BelowItem;
2302 }
else if (rect.contains(pos,
true)) {
2303 r = QAbstractItemView::OnItem;
2306 QRect touchingRect = rect;
2307 touchingRect.adjust(-1, -1, 1, 1);
2308 if (touchingRect.contains(pos,
false)) {
2309 r = QAbstractItemView::OnItem;
2313 if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled)))
2314 r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem;
2322
2323
2324
2325
2326
2327void QAbstractItemView::focusInEvent(QFocusEvent *event)
2329 Q_D(QAbstractItemView);
2330 QAbstractScrollArea::focusInEvent(event);
2332 const QItemSelectionModel* model = selectionModel();
2333 bool currentIndexValid = currentIndex().isValid();
2336 && !d->currentIndexSet
2337 && !currentIndexValid) {
2338 bool autoScroll = d->autoScroll;
2339 d->autoScroll =
false;
2340 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2341 if (index.isValid() && d->isIndexEnabled(index) && event->reason() != Qt::MouseFocusReason) {
2342 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
2343 currentIndexValid =
true;
2345 d->autoScroll = autoScroll;
2348 if (model && currentIndexValid)
2349 setAttribute(Qt::WA_InputMethodEnabled, (currentIndex().flags() & Qt::ItemIsEditable));
2350 else if (!currentIndexValid)
2351 setAttribute(Qt::WA_InputMethodEnabled,
false);
2353 d->viewport->update();
2357
2358
2359
2360
2361
2362void QAbstractItemView::focusOutEvent(QFocusEvent *event)
2364 Q_D(QAbstractItemView);
2365 QAbstractScrollArea::focusOutEvent(event);
2366 d->viewport->update();
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380void QAbstractItemView::keyPressEvent(QKeyEvent *event)
2382 Q_D(QAbstractItemView);
2383 d->delayedAutoScroll.stop();
2385#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
2386 if (event == QKeySequence::Copy) {
2387 const QModelIndex index = currentIndex();
2388 if (index.isValid() && d->model) {
2389 const QVariant variant = d->model->data(index, Qt::DisplayRole);
2390 if (variant.canConvert<QString>())
2391 QGuiApplication::clipboard()->setText(variant.toString());
2397 QPersistentModelIndex newCurrent;
2398 d->moveCursorUpdatedView =
false;
2399 switch (event->key()) {
2401 newCurrent = moveCursor(MoveDown, event->modifiers());
2404 newCurrent = moveCursor(MoveUp, event->modifiers());
2407 newCurrent = moveCursor(MoveLeft, event->modifiers());
2410 newCurrent = moveCursor(MoveRight, event->modifiers());
2413 newCurrent = moveCursor(MoveHome, event->modifiers());
2416 newCurrent = moveCursor(MoveEnd, event->modifiers());
2418 case Qt::Key_PageUp:
2419 newCurrent = moveCursor(MovePageUp, event->modifiers());
2421 case Qt::Key_PageDown:
2422 newCurrent = moveCursor(MovePageDown, event->modifiers());
2425 if (d->tabKeyNavigation)
2426 newCurrent = moveCursor(MoveNext, event->modifiers());
2428 case Qt::Key_Backtab:
2429 if (d->tabKeyNavigation)
2430 newCurrent = moveCursor(MovePrevious, event->modifiers());
2434 QPersistentModelIndex oldCurrent = currentIndex();
2435 if (newCurrent != oldCurrent && newCurrent.isValid() && d->isIndexEnabled(newCurrent)) {
2436 if (!hasFocus() && QApplication::focusWidget() == indexWidget(oldCurrent))
2438 QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
2439 if (command != QItemSelectionModel::NoUpdate
2440 || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection,
nullptr,
this)) {
2442 if (command & QItemSelectionModel::Current) {
2443 d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
2444 if (!d->currentSelectionStartIndex.isValid())
2445 d->currentSelectionStartIndex = oldCurrent;
2446 QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center());
2447 setSelection(rect, command);
2449 d->selectionModel->setCurrentIndex(newCurrent, command);
2450 d->currentSelectionStartIndex = newCurrent;
2451 if (newCurrent.isValid()) {
2453 QRect rect(visualRect(newCurrent).center(), QSize(1, 1));
2454 setSelection(rect, command);
2462 switch (event->key()) {
2470 case Qt::Key_PageUp:
2471 case Qt::Key_PageDown:
2472 case Qt::Key_Escape:
2474 case Qt::Key_Control:
2475 case Qt::Key_Delete:
2476 case Qt::Key_Backspace:
2480 case Qt::Key_Select:
2481 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2482 if (d->selectionModel)
2483 d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
2484 if (event->key() == Qt::Key_Space) {
2485 keyboardSearch(event->text());
2492 case Qt::Key_Return:
2495 if (!edit(currentIndex(), EditKeyPressed, event) && d->editorIndexHash.isEmpty())
2500 if (!edit(currentIndex(), EditKeyPressed, event))
2504 case Qt::Key_Return:
2508 if (state() != EditingState || hasFocus()) {
2509 if (currentIndex().isValid())
2510 emit activated(currentIndex());
2516#ifndef QT_NO_SHORTCUT
2517 if (event == QKeySequence::SelectAll && selectionMode() != NoSelection) {
2523 if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
2524 emit activated(currentIndex());
2528 bool modified = (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
2529 if (!event->text().isEmpty() && !modified && !edit(currentIndex(), AnyKeyPressed, event)) {
2530 keyboardSearch(event->text());
2537 if (d->moveCursorUpdatedView)
2542
2543
2544
2545
2546
2547void QAbstractItemView::resizeEvent(QResizeEvent *event)
2549 QAbstractScrollArea::resizeEvent(event);
2554
2555
2556
2557
2558
2559void QAbstractItemView::timerEvent(QTimerEvent *event)
2561 Q_D(QAbstractItemView);
2562 if (event->timerId() == d->fetchMoreTimer.timerId())
2564 else if (event->timerId() == d->delayedReset.timerId())
2566 else if (event->timerId() == d->autoScrollTimer.timerId())
2568 else if (event->timerId() == d->updateTimer.timerId())
2569 d->updateDirtyRegion();
2570 else if (event->timerId() == d->delayedEditing.timerId()) {
2571 d->delayedEditing.stop();
2572 edit(currentIndex());
2573 }
else if (event->timerId() == d->delayedLayout.timerId()) {
2574 d->delayedLayout.stop();
2576 d->interruptDelayedItemsLayout();
2578 const QModelIndex current = currentIndex();
2579 if (current.isValid() && d->state == QAbstractItemView::EditingState)
2582 }
else if (event->timerId() == d->delayedAutoScroll.timerId()) {
2583 d->delayedAutoScroll.stop();
2586 if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
2587 scrollTo(d->pressedIndex);
2588 }
else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
2589 d->pressClosedEditorWatcher.stop();
2594
2595
2596void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
2598 Q_D(QAbstractItemView);
2605 bool forwardEventToEditor =
false;
2606 const bool commit = !event->commitString().isEmpty();
2607 const bool preediting = !event->preeditString().isEmpty();
2608 if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
2609 if (d->waitForIMCommit) {
2610 if (commit || !preediting) {
2612 d->waitForIMCommit =
false;
2613 QApplication::sendEvent(currentEditor, event);
2615 QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
2617 delegate->setEditorData(currentEditor, currentIndex());
2618 d->selectAllInEditor(currentEditor);
2620 if (currentEditor->focusPolicy() != Qt::NoFocus)
2621 currentEditor->setFocus();
2624 QApplication::sendEvent(currentEditor, event);
2628 }
else if (preediting) {
2630 d->waitForIMCommit =
true;
2632 forwardEventToEditor =
true;
2633 }
else if (!commit) {
2637 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2638 d->waitForIMCommit =
false;
2640 keyboardSearch(event->commitString());
2642 }
else if (QWidget *currentEditor; forwardEventToEditor
2643 && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
2644 QApplication::sendEvent(currentEditor, event);
2648#if QT_CONFIG(draganddrop)
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2668
2669
2670QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition()
const
2672 Q_D(
const QAbstractItemView);
2673 return d->dropIndicatorPosition;
2678
2679
2680
2681
2682
2683
2684QModelIndexList QAbstractItemView::selectedIndexes()
const
2686 Q_D(
const QAbstractItemView);
2687 QModelIndexList indexes;
2688 if (d->selectionModel) {
2689 indexes = d->selectionModel->selectedIndexes();
2690 auto isHidden = [
this](
const QModelIndex &idx) {
2691 return isIndexHidden(idx);
2693 indexes.removeIf(isHidden);
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711bool QAbstractItemView::edit(
const QModelIndex &index, EditTrigger trigger, QEvent *event)
2713 Q_D(QAbstractItemView);
2715 if (!d->isIndexValid(index))
2718 if (QWidget *w = (d->persistent.isEmpty() ?
static_cast<QWidget*>(
nullptr) : d->editorForIndex(index).widget.data())) {
2719 if (w->focusPolicy() == Qt::NoFocus)
2721 if (!d->waitForIMCommit)
2728 if (trigger == DoubleClicked) {
2729 d->delayedEditing.stop();
2730 d->delayedAutoScroll.stop();
2731 }
else if (trigger == CurrentChanged) {
2732 d->delayedEditing.stop();
2736 QPersistentModelIndex safeIndex(index);
2738 if (d->sendDelegateEvent(index, event)) {
2743 if (!safeIndex.isValid()) {
2748 EditTriggers lastTrigger = d->lastTrigger;
2749 d->lastTrigger = trigger;
2751 if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
2754 if (d->delayedEditing.isActive())
2759 if (lastTrigger == DoubleClicked && trigger == SelectedClicked)
2763 if (trigger == SelectedClicked)
2764 d->delayedEditing.start(QApplication::doubleClickInterval(),
this);
2766 d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event :
nullptr);
2772
2773
2774
2775void QAbstractItemView::updateEditorData()
2777 Q_D(QAbstractItemView);
2778 d->updateEditorData(QModelIndex(), QModelIndex());
2782
2783
2784
2785void QAbstractItemView::updateEditorGeometries()
2787 Q_D(QAbstractItemView);
2788 if (d->editorIndexHash.isEmpty())
2790 if (d->delayedPendingLayout) {
2792 d->executePostedLayout();
2795 QStyleOptionViewItem option;
2796 initViewItemOption(&option);
2797 QEditorIndexHash::iterator it = d->editorIndexHash.begin();
2798 QWidgetList editorsToRelease;
2799 QWidgetList editorsToHide;
2800 while (it != d->editorIndexHash.end()) {
2801 QModelIndex index = it.value();
2802 QWidget *editor = it.key();
2803 if (index.isValid() && editor) {
2804 option.rect = visualRect(index);
2805 if (option.rect.isValid()) {
2807 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2809 delegate->updateEditorGeometry(editor, option, index);
2811 editorsToHide << editor;
2815 d->indexEditorHash.remove(it.value());
2816 it = d->editorIndexHash.erase(it);
2817 editorsToRelease << editor;
2823 for (
int i = 0; i < editorsToHide.size(); ++i) {
2824 editorsToHide.at(i)->hide();
2826 for (
int i = 0; i < editorsToRelease.size(); ++i) {
2827 d->releaseEditor(editorsToRelease.at(i));
2832
2833
2834void QAbstractItemView::updateGeometries()
2836 Q_D(QAbstractItemView);
2837 updateEditorGeometries();
2838 d->fetchMoreTimer.start(0,
this);
2839 d->updateGeometry();
2843
2844
2845void QAbstractItemView::verticalScrollbarValueChanged(
int value)
2847 Q_D(QAbstractItemView);
2848 if (verticalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2849 d->model->fetchMore(d->root);
2850 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2851 if (viewport()->rect().contains(posInVp))
2852 d->checkMouseMove(posInVp);
2856
2857
2858void QAbstractItemView::horizontalScrollbarValueChanged(
int value)
2860 Q_D(QAbstractItemView);
2861 if (horizontalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2862 d->model->fetchMore(d->root);
2863 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2864 if (viewport()->rect().contains(posInVp))
2865 d->checkMouseMove(posInVp);
2869
2870
2871void QAbstractItemView::verticalScrollbarAction(
int)
2877
2878
2879void QAbstractItemView::horizontalScrollbarAction(
int)
2885
2886
2887
2888
2889
2890
2891
2893void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
2895 Q_D(QAbstractItemView);
2899 const bool isPersistent = d->persistent.contains(editor);
2900 const QModelIndex index = d->indexForEditor(editor);
2901 if (!index.isValid()) {
2902 if (!editor->isVisible()) {
2910 qWarning(
"QAbstractItemView::closeEditor called with an editor that does not belong to this view");
2914 const bool hadFocus = editor->hasFocus();
2918 d->pressClosedEditorWatcher.start(0,
this);
2919 d->lastEditedIndex = index;
2921 if (!isPersistent) {
2923 QModelIndex index = d->indexForEditor(editor);
2924 editor->removeEventFilter(itemDelegateForIndex(index));
2925 d->removeEditor(editor);
2928 if (focusPolicy() != Qt::NoFocus)
2931 editor->clearFocus();
2933 d->checkPersistentEditorFocus();
2936 QPointer<QWidget> ed = editor;
2937 QCoreApplication::sendPostedEvents(editor, 0);
2940 if (!isPersistent && editor)
2941 d->releaseEditor(editor, index);
2946 QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::NoUpdate;
2947 if (d->selectionMode != NoSelection)
2948 flags = QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
2950 case QAbstractItemDelegate::EditNextItem: {
2951 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2952 if (index.isValid()) {
2953 QPersistentModelIndex persistent(index);
2954 d->selectionModel->setCurrentIndex(persistent, flags);
2956 if (index.flags() & Qt::ItemIsEditable
2957 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2960 case QAbstractItemDelegate::EditPreviousItem: {
2961 QModelIndex index = moveCursor(MovePrevious, Qt::NoModifier);
2962 if (index.isValid()) {
2963 QPersistentModelIndex persistent(index);
2964 d->selectionModel->setCurrentIndex(persistent, flags);
2966 if (index.flags() & Qt::ItemIsEditable
2967 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2970 case QAbstractItemDelegate::SubmitModelCache:
2973 case QAbstractItemDelegate::RevertModelCache:
2982
2983
2984
2985
2986void QAbstractItemView::commitData(QWidget *editor)
2988 Q_D(QAbstractItemView);
2989 if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
2991 QModelIndex index = d->indexForEditor(editor);
2992 if (!index.isValid()) {
2993 qWarning(
"QAbstractItemView::commitData called with an editor that does not belong to this view");
2996 d->currentlyCommittingEditor = editor;
2997 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2998 editor->removeEventFilter(delegate);
2999 delegate->setModelData(editor, d->model, index);
3000 editor->installEventFilter(delegate);
3001 d->currentlyCommittingEditor =
nullptr;
3005
3006
3007
3008
3009void QAbstractItemView::editorDestroyed(QObject *editor)
3011 Q_D(QAbstractItemView);
3012 QWidget *w = qobject_cast<QWidget*>(editor);
3014 d->persistent.remove(w);
3015 if (state() == EditingState)
3022
3023
3024
3025
3026
3027
3028
3029void QAbstractItemView::keyboardSearch(
const QString &search)
3031 Q_D(QAbstractItemView);
3032 if (!d->model->rowCount(d->root) || !d->model->columnCount(d->root))
3035 QModelIndex start = currentIndex().isValid() ? currentIndex()
3036 : d->model->index(0, 0, d->root);
3037 bool skipRow =
false;
3038 bool keyboardTimeWasValid = d->keyboardInputTime.isValid();
3039 qint64 keyboardInputTimeElapsed;
3040 if (keyboardTimeWasValid)
3041 keyboardInputTimeElapsed = d->keyboardInputTime.restart();
3043 d->keyboardInputTime.start();
3044 if (search.isEmpty() || !keyboardTimeWasValid
3045 || keyboardInputTimeElapsed > QApplication::keyboardInputInterval()) {
3046 d->keyboardInput = search;
3047 skipRow = currentIndex().isValid();
3049 d->keyboardInput += search;
3053 bool sameKey =
false;
3054 if (d->keyboardInput.size() > 1) {
3055 int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
3056 sameKey = (c == d->keyboardInput.size());
3063 QModelIndex parent = start.parent();
3064 int newRow = (start.row() < d->model->rowCount(parent) - 1) ? start.row() + 1 : 0;
3065 start = d->model->index(newRow, start.column(), parent);
3069 QModelIndex current = start;
3070 QModelIndexList match;
3071 QModelIndex firstMatch;
3072 QModelIndex startMatch;
3073 QModelIndexList previous;
3075 match = d->model->match(current, Qt::DisplayRole, d->keyboardInput, 1,
3076 d->keyboardSearchFlags);
3077 if (match == previous)
3079 firstMatch = match.value(0);
3081 if (firstMatch.isValid()) {
3082 if (d->isIndexEnabled(firstMatch)) {
3083 setCurrentIndex(firstMatch);
3086 int row = firstMatch.row() + 1;
3087 if (row >= d->model->rowCount(firstMatch.parent()))
3089 current = firstMatch.sibling(row, firstMatch.column());
3092 if (!startMatch.isValid())
3093 startMatch = firstMatch;
3094 else if (startMatch == firstMatch)
3097 }
while (current != start && firstMatch.isValid());
3101
3102
3103
3104
3105
3106QSize QAbstractItemView::sizeHintForIndex(
const QModelIndex &index)
const
3108 Q_D(
const QAbstractItemView);
3109 if (!d->isIndexValid(index))
3111 const auto delegate = itemDelegateForIndex(index);
3112 QStyleOptionViewItem option;
3113 initViewItemOption(&option);
3114 return delegate ? delegate->sizeHint(option, index) : QSize();
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133int QAbstractItemView::sizeHintForRow(
int row)
const
3135 Q_D(
const QAbstractItemView);
3137 if (row < 0 || row >= d->model->rowCount(d->root))
3142 QStyleOptionViewItem option;
3143 initViewItemOption(&option);
3145 int colCount = d->model->columnCount(d->root);
3146 for (
int c = 0; c < colCount; ++c) {
3147 const QModelIndex index = d->model->index(row, c, d->root);
3148 if (QWidget *editor = d->editorForIndex(index).widget.data())
3149 height = qMax(height, editor->height());
3150 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3151 height = qMax(height, delegate->sizeHint(option, index).height());
3157
3158
3159
3160
3161
3162
3163
3164int QAbstractItemView::sizeHintForColumn(
int column)
const
3166 Q_D(
const QAbstractItemView);
3168 if (column < 0 || column >= d->model->columnCount(d->root))
3173 QStyleOptionViewItem option;
3174 initViewItemOption(&option);
3176 int rows = d->model->rowCount(d->root);
3177 for (
int r = 0; r < rows; ++r) {
3178 const QModelIndex index = d->model->index(r, column, d->root);
3179 if (QWidget *editor = d->editorForIndex(index).widget.data())
3180 width = qMax(width, editor->sizeHint().width());
3181 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3182 width = qMax(width, delegate->sizeHint(option, index).width());
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204int QAbstractItemView::updateThreshold()
const
3206 Q_D(
const QAbstractItemView);
3207 return d->updateThreshold;
3210void QAbstractItemView::setUpdateThreshold(
int threshold)
3212 Q_D(QAbstractItemView);
3213 if (d->updateThreshold == threshold)
3215 d->updateThreshold = threshold;
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3230Qt::MatchFlags QAbstractItemView::keyboardSearchFlags()
const
3232 Q_D(
const QAbstractItemView);
3233 return d->keyboardSearchFlags;
3236void QAbstractItemView::setKeyboardSearchFlags(Qt::MatchFlags searchFlags)
3238 Q_D(QAbstractItemView);
3239 d->keyboardSearchFlags = searchFlags;
3243
3244
3245
3246
3247
3248void QAbstractItemView::openPersistentEditor(
const QModelIndex &index)
3250 Q_D(QAbstractItemView);
3251 QStyleOptionViewItem options;
3252 initViewItemOption(&options);
3253 options.rect = visualRect(index);
3254 options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
3256 QWidget *editor = d->editor(index, options);
3259 d->persistent.insert(editor);
3264
3265
3266
3267
3268void QAbstractItemView::closePersistentEditor(
const QModelIndex &index)
3270 Q_D(QAbstractItemView);
3271 if (QWidget *editor = d->editorForIndex(index).widget.data()) {
3272 if (index == selectionModel()->currentIndex())
3273 closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
3274 d->persistent.remove(editor);
3275 d->removeEditor(editor);
3276 d->releaseEditor(editor, index);
3281
3282
3283
3284
3285
3286
3287bool QAbstractItemView::isPersistentEditorOpen(
const QModelIndex &index)
const
3289 Q_D(
const QAbstractItemView);
3290 QWidget *editor = d->editorForIndex(index).widget;
3291 return editor && d->persistent.contains(editor);
4059void QAbstractItemView::doAutoScroll()
4062 Q_D(QAbstractItemView);
4063 QScrollBar *verticalScroll = verticalScrollBar();
4064 QScrollBar *horizontalScroll = horizontalScrollBar();
4068 QHeaderView *hv = qobject_cast<QHeaderView*>(
this);
4070 QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
4072 if (hv->orientation() == Qt::Horizontal) {
4073 if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
4074 horizontalScroll = parent->horizontalScrollBar();
4076 if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
4077 verticalScroll = parent->verticalScrollBar();
4082 const int verticalStep = verticalScroll->pageStep();
4083 const int horizontalStep = horizontalScroll->pageStep();
4084 if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
4085 ++d->autoScrollCount;
4087 const int margin = d->autoScrollMargin;
4088 const int verticalValue = verticalScroll->value();
4089 const int horizontalValue = horizontalScroll->value();
4091 const QPoint pos = d->draggedPosition - d->offset();
4092 const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
4095 if (pos.y() - area.top() < margin)
4096 verticalScroll->setValue(verticalValue - d->autoScrollCount);
4097 else if (area.bottom() - pos.y() < margin)
4098 verticalScroll->setValue(verticalValue + d->autoScrollCount);
4099 if (pos.x() - area.left() < margin)
4100 horizontalScroll->setValue(horizontalValue - d->autoScrollCount);
4101 else if (area.right() - pos.x() < margin)
4102 horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
4104 const bool verticalUnchanged = (verticalValue == verticalScroll->value());
4105 const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
4106 if (verticalUnchanged && horizontalUnchanged) {
4109#if QT_CONFIG(draganddrop)
4110 d->dropIndicatorRect = QRect();
4111 d->dropIndicatorPosition = QAbstractItemView::OnViewport;
4114 case QAbstractItemView::DragSelectingState: {
4117 const QPoint globalPos = d->viewport->mapToGlobal(pos);
4118 const QPoint windowPos = window()->mapFromGlobal(globalPos);
4119 QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
4120 Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
4121 Qt::MouseEventSynthesizedByQt);
4122 QApplication::sendEvent(viewport(), &mm);
4125 case QAbstractItemView::DraggingState: {
4131 d->draggedPosition = pos + d->offset();
4137 d->viewport->update();
4241QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
4242 const QModelIndex &index,
const QEvent *event)
const
4244 Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
4245 ?
static_cast<
const QInputEvent*>(event)->modifiers()
4246 : QGuiApplication::keyboardModifiers();
4248 switch (event->type()) {
4249 case QEvent::MouseMove: {
4251 if (modifiers & Qt::ControlModifier)
4252 return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
4255 case QEvent::MouseButtonPress: {
4256 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4257 const bool rightButtonPressed = button & Qt::RightButton;
4258 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4259 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4260 const bool indexIsSelected = selectionModel->isSelected(index);
4261 if ((shiftKeyPressed || controlKeyPressed) && rightButtonPressed)
4262 return QItemSelectionModel::NoUpdate;
4263 if (!shiftKeyPressed && !controlKeyPressed && indexIsSelected)
4264 return QItemSelectionModel::NoUpdate;
4265 if (!index.isValid() && !rightButtonPressed && !shiftKeyPressed && !controlKeyPressed)
4266 return QItemSelectionModel::Clear;
4267 if (!index.isValid())
4268 return QItemSelectionModel::NoUpdate;
4270 if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
4271#if QT_CONFIG(draganddrop)
4272 && dragEnabled && isIndexDragEnabled(index)
4275 return QItemSelectionModel::NoUpdate;
4279 case QEvent::MouseButtonRelease: {
4281 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4282 const bool rightButtonPressed = button & Qt::RightButton;
4283 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4284 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4285 if (((index == pressedIndex && selectionModel->isSelected(index))
4286 || !index.isValid()) && state != QAbstractItemView::DragSelectingState
4287 && !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
4288 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
4289 if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
4290#if QT_CONFIG(draganddrop)
4291 && dragEnabled && isIndexDragEnabled(index)
4296 return QItemSelectionModel::NoUpdate;
4298 case QEvent::KeyPress: {
4300 switch (
static_cast<
const QKeyEvent*>(event)->key()) {
4301 case Qt::Key_Backtab:
4302 modifiers = modifiers & ~Qt::ShiftModifier;
4310 case Qt::Key_PageUp:
4311 case Qt::Key_PageDown:
4313 if (modifiers & Qt::ControlModifier)
4314 return QItemSelectionModel::NoUpdate;
4316 case Qt::Key_Select:
4317 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4319 if (modifiers & Qt::ControlModifier)
4320 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4321 return QItemSelectionModel::Select|selectionBehaviorFlags();
4332 if (modifiers & Qt::ShiftModifier)
4333 return QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4334 if (modifiers & Qt::ControlModifier)
4335 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4336 if (state == QAbstractItemView::DragSelectingState) {
4338 return QItemSelectionModel::Clear|QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4341 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();