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;
2015 if (event->button() == Qt::LeftButton)
2016 emit clicked(index);
2019 QStyleOptionViewItem option;
2020 initViewItemOption(&option);
2021 if (d->pressedAlreadySelected)
2022 option.state |= QStyle::State_Selected;
2023 if ((d->model->flags(index) & Qt::ItemIsEnabled)
2024 && style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option,
this))
2025 emit activated(index);
2030
2031
2032
2033
2034void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
2036 Q_D(QAbstractItemView);
2038 QModelIndex index = indexAt(event->position().toPoint());
2039 if (!index.isValid()
2040 || !d->isIndexEnabled(index)
2041 || (d->pressedIndex != index)) {
2042 QMouseEvent me(QEvent::MouseButtonPress,
2043 event->position(), event->scenePosition(), event->globalPosition(),
2044 event->button(), event->buttons(), event->modifiers(),
2045 event->source(), event->pointingDevice());
2046 mousePressEvent(&me);
2050 QPersistentModelIndex persistent = index;
2051 emit doubleClicked(persistent);
2052 if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
2053 && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick,
nullptr,
this))
2054 emit activated(persistent);
2055 d->releaseFromDoubleClick =
true;
2058#if QT_CONFIG(draganddrop)
2061
2062
2063
2064
2065
2066
2067void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
2069 if (dragDropMode() == InternalMove
2070 && (event->source() !=
this|| !(event->possibleActions() & Qt::MoveAction)))
2073 if (d_func()->canDrop(event)) {
2075 setState(DraggingState);
2082
2083
2084
2085
2086
2087
2088
2089void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
2091 Q_D(QAbstractItemView);
2092 d->draggedPosition = event->position().toPoint() + d->offset();
2093 if (dragDropMode() == InternalMove
2094 && (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction)))
2100 QModelIndex index = indexAt(event->position().toPoint());
2102 if (!d->droppingOnItself(event, index)
2103 && d->canDrop(event)) {
2105 if (index.isValid() && d->showDropIndicator) {
2106 QRect rect = visualRect(index);
2107 d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
2108 if (d->selectionBehavior == QAbstractItemView::SelectRows
2109 && d->dropIndicatorPosition != OnViewport
2110 && (d->dropIndicatorPosition != OnItem || event->source() ==
this)) {
2111 const int maxCol = d->model->columnCount(index.parent()) - 1;
2112 const auto idx = index.column() > 0 ? index.siblingAtColumn(0) : index;
2113 rect = d->intersectedRect(viewport()->rect(), idx, idx.siblingAtColumn(maxCol));
2115 switch (d->dropIndicatorPosition) {
2117 if (d->isIndexDropEnabled(index.parent())) {
2118 d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0);
2119 event->acceptProposedAction();
2121 d->dropIndicatorRect = QRect();
2125 if (d->isIndexDropEnabled(index.parent())) {
2126 d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0);
2127 event->acceptProposedAction();
2129 d->dropIndicatorRect = QRect();
2133 if (d->isIndexDropEnabled(index)) {
2134 d->dropIndicatorRect = rect;
2135 event->acceptProposedAction();
2137 d->dropIndicatorRect = QRect();
2141 d->dropIndicatorRect = QRect();
2142 if (d->isIndexDropEnabled(rootIndex())) {
2143 event->acceptProposedAction();
2148 d->dropIndicatorRect = QRect();
2149 d->dropIndicatorPosition = OnViewport;
2150 if (d->isIndexDropEnabled(rootIndex())) {
2151 event->acceptProposedAction();
2154 d->viewport->update();
2157 if (d->shouldAutoScroll(event->position().toPoint()))
2162
2163
2164
2165
2166bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event,
const QModelIndex &index)
2168 Q_Q(QAbstractItemView);
2169 Qt::DropAction dropAction = event->dropAction();
2170 if (q->dragDropMode() == QAbstractItemView::InternalMove)
2171 dropAction = Qt::MoveAction;
2172 if (event->source() == q
2173 && event->possibleActions() & Qt::MoveAction
2174 && dropAction == Qt::MoveAction) {
2175 QModelIndexList selectedIndexes = q->selectedIndexes();
2176 QModelIndex child = index;
2177 while (child.isValid() && child != root) {
2178 if (selectedIndexes.contains(child))
2180 child = child.parent();
2187
2188
2189
2190
2191
2192void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *)
2194 Q_D(QAbstractItemView);
2197 d->hover = QModelIndex();
2198 d->viewport->update();
2202
2203
2204
2205
2206
2207
2208void QAbstractItemView::dropEvent(QDropEvent *event)
2210 Q_D(QAbstractItemView);
2211 if (dragDropMode() == InternalMove) {
2212 if (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction))
2219 if (d->dropOn(event, &row, &col, &index)) {
2220 const Qt::DropAction action = dragDropMode() == InternalMove ? Qt::MoveAction : event->dropAction();
2221 if (d->model->dropMimeData(event->mimeData(), action, row, col, index)) {
2222 if (action != event->dropAction()) {
2223 event->setDropAction(action);
2226 event->acceptProposedAction();
2232 d->viewport->update();
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246bool QAbstractItemViewPrivate::dropOn(QDropEvent *event,
int *dropRow,
int *dropCol, QModelIndex *dropIndex)
2248 Q_Q(QAbstractItemView);
2249 if (event->isAccepted())
2254 if (viewport->rect().contains(event->position().toPoint())) {
2255 index = q->indexAt(event->position().toPoint());
2256 if (!index.isValid())
2261 if (model->supportedDropActions() & event->dropAction()) {
2264 if (index != root) {
2265 dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
2266 switch (dropIndicatorPosition) {
2267 case QAbstractItemView::AboveItem:
2269 col = index.column();
2270 index = index.parent();
2272 case QAbstractItemView::BelowItem:
2273 row = index.row() + 1;
2274 col = index.column();
2275 index = index.parent();
2277 case QAbstractItemView::OnItem:
2278 case QAbstractItemView::OnViewport:
2282 dropIndicatorPosition = QAbstractItemView::OnViewport;
2287 if (!droppingOnItself(event, index))
2293QAbstractItemView::DropIndicatorPosition
2294QAbstractItemViewPrivate::position(
const QPoint &pos,
const QRect &rect,
const QModelIndex &index)
const
2296 QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport;
2298 const int margin = qBound(2, qRound(qreal(rect.height()) / 5.5), 12);
2299 if (pos.y() - rect.top() < margin) {
2300 r = QAbstractItemView::AboveItem;
2301 }
else if (rect.bottom() - pos.y() < margin) {
2302 r = QAbstractItemView::BelowItem;
2303 }
else if (rect.contains(pos,
true)) {
2304 r = QAbstractItemView::OnItem;
2307 QRect touchingRect = rect;
2308 touchingRect.adjust(-1, -1, 1, 1);
2309 if (touchingRect.contains(pos,
false)) {
2310 r = QAbstractItemView::OnItem;
2314 if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled)))
2315 r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem;
2323
2324
2325
2326
2327
2328void QAbstractItemView::focusInEvent(QFocusEvent *event)
2330 Q_D(QAbstractItemView);
2331 QAbstractScrollArea::focusInEvent(event);
2333 const QItemSelectionModel* model = selectionModel();
2334 bool currentIndexValid = currentIndex().isValid();
2337 && !d->currentIndexSet
2338 && !currentIndexValid) {
2339 bool autoScroll = d->autoScroll;
2340 d->autoScroll =
false;
2341 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2342 if (index.isValid() && d->isIndexEnabled(index) && event->reason() != Qt::MouseFocusReason) {
2343 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
2344 currentIndexValid =
true;
2346 d->autoScroll = autoScroll;
2349 if (model && currentIndexValid)
2350 setAttribute(Qt::WA_InputMethodEnabled, (currentIndex().flags() & Qt::ItemIsEditable));
2351 else if (!currentIndexValid)
2352 setAttribute(Qt::WA_InputMethodEnabled,
false);
2354 d->viewport->update();
2358
2359
2360
2361
2362
2363void QAbstractItemView::focusOutEvent(QFocusEvent *event)
2365 Q_D(QAbstractItemView);
2366 QAbstractScrollArea::focusOutEvent(event);
2367 d->viewport->update();
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381void QAbstractItemView::keyPressEvent(QKeyEvent *event)
2383 Q_D(QAbstractItemView);
2384 d->delayedAutoScroll.stop();
2386#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
2387 if (event == QKeySequence::Copy) {
2388 const QModelIndex index = currentIndex();
2389 if (index.isValid() && d->model) {
2390 const QVariant variant = d->model->data(index, Qt::DisplayRole);
2391 if (variant.canConvert<QString>())
2392 QGuiApplication::clipboard()->setText(variant.toString());
2398 QPersistentModelIndex newCurrent;
2399 d->moveCursorUpdatedView =
false;
2400 switch (event->key()) {
2402 newCurrent = moveCursor(MoveDown, event->modifiers());
2405 newCurrent = moveCursor(MoveUp, event->modifiers());
2408 newCurrent = moveCursor(MoveLeft, event->modifiers());
2411 newCurrent = moveCursor(MoveRight, event->modifiers());
2414 newCurrent = moveCursor(MoveHome, event->modifiers());
2417 newCurrent = moveCursor(MoveEnd, event->modifiers());
2419 case Qt::Key_PageUp:
2420 newCurrent = moveCursor(MovePageUp, event->modifiers());
2422 case Qt::Key_PageDown:
2423 newCurrent = moveCursor(MovePageDown, event->modifiers());
2426 if (d->tabKeyNavigation)
2427 newCurrent = moveCursor(MoveNext, event->modifiers());
2429 case Qt::Key_Backtab:
2430 if (d->tabKeyNavigation)
2431 newCurrent = moveCursor(MovePrevious, event->modifiers());
2435 QPersistentModelIndex oldCurrent = currentIndex();
2436 if (newCurrent != oldCurrent && newCurrent.isValid() && d->isIndexEnabled(newCurrent)) {
2437 if (!hasFocus() && QApplication::focusWidget() == indexWidget(oldCurrent))
2439 QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
2440 if (command != QItemSelectionModel::NoUpdate
2441 || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection,
nullptr,
this)) {
2443 if (command & QItemSelectionModel::Current) {
2444 d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
2445 if (!d->currentSelectionStartIndex.isValid())
2446 d->currentSelectionStartIndex = oldCurrent;
2447 QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center());
2448 setSelection(rect, command);
2450 d->selectionModel->setCurrentIndex(newCurrent, command);
2451 d->currentSelectionStartIndex = newCurrent;
2452 if (newCurrent.isValid()) {
2454 QRect rect(visualRect(newCurrent).center(), QSize(1, 1));
2455 setSelection(rect, command);
2463 switch (event->key()) {
2471 case Qt::Key_PageUp:
2472 case Qt::Key_PageDown:
2473 case Qt::Key_Escape:
2475 case Qt::Key_Control:
2476 case Qt::Key_Delete:
2477 case Qt::Key_Backspace:
2481 case Qt::Key_Select:
2482 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2483 if (d->selectionModel)
2484 d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
2485 if (event->key() == Qt::Key_Space) {
2486 keyboardSearch(event->text());
2493 case Qt::Key_Return:
2496 if (!edit(currentIndex(), EditKeyPressed, event) && d->editorIndexHash.isEmpty())
2501 if (!edit(currentIndex(), EditKeyPressed, event))
2505 case Qt::Key_Return:
2509 if (state() != EditingState || hasFocus()) {
2510 if (currentIndex().isValid())
2511 emit activated(currentIndex());
2517#ifndef QT_NO_SHORTCUT
2518 if (event == QKeySequence::SelectAll && selectionMode() != NoSelection) {
2524 if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
2525 emit activated(currentIndex());
2529 bool modified = (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
2530 if (!event->text().isEmpty() && !modified && !edit(currentIndex(), AnyKeyPressed, event)) {
2531 keyboardSearch(event->text());
2538 if (d->moveCursorUpdatedView)
2543
2544
2545
2546
2547
2548void QAbstractItemView::resizeEvent(QResizeEvent *event)
2550 QAbstractScrollArea::resizeEvent(event);
2555
2556
2557
2558
2559
2560void QAbstractItemView::timerEvent(QTimerEvent *event)
2562 Q_D(QAbstractItemView);
2563 if (event->timerId() == d->fetchMoreTimer.timerId())
2565 else if (event->timerId() == d->delayedReset.timerId())
2567 else if (event->timerId() == d->autoScrollTimer.timerId())
2569 else if (event->timerId() == d->updateTimer.timerId())
2570 d->updateDirtyRegion();
2571 else if (event->timerId() == d->delayedEditing.timerId()) {
2572 d->delayedEditing.stop();
2573 edit(currentIndex());
2574 }
else if (event->timerId() == d->delayedLayout.timerId()) {
2575 d->delayedLayout.stop();
2577 d->interruptDelayedItemsLayout();
2579 const QModelIndex current = currentIndex();
2580 if (current.isValid() && d->state == QAbstractItemView::EditingState)
2583 }
else if (event->timerId() == d->delayedAutoScroll.timerId()) {
2584 d->delayedAutoScroll.stop();
2587 if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
2588 scrollTo(d->pressedIndex);
2589 }
else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
2590 d->pressClosedEditorWatcher.stop();
2595
2596
2597void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
2599 Q_D(QAbstractItemView);
2606 bool forwardEventToEditor =
false;
2607 const bool commit = !event->commitString().isEmpty();
2608 const bool preediting = !event->preeditString().isEmpty();
2609 if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
2610 if (d->waitForIMCommit) {
2611 if (commit || !preediting) {
2613 d->waitForIMCommit =
false;
2614 QApplication::sendEvent(currentEditor, event);
2616 QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
2618 delegate->setEditorData(currentEditor, currentIndex());
2619 d->selectAllInEditor(currentEditor);
2621 if (currentEditor->focusPolicy() != Qt::NoFocus)
2622 currentEditor->setFocus();
2625 QApplication::sendEvent(currentEditor, event);
2629 }
else if (preediting) {
2631 d->waitForIMCommit =
true;
2633 forwardEventToEditor =
true;
2634 }
else if (!commit) {
2638 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2639 d->waitForIMCommit =
false;
2641 keyboardSearch(event->commitString());
2643 }
else if (QWidget *currentEditor; forwardEventToEditor
2644 && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
2645 QApplication::sendEvent(currentEditor, event);
2649#if QT_CONFIG(draganddrop)
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2669
2670
2671QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition()
const
2673 Q_D(
const QAbstractItemView);
2674 return d->dropIndicatorPosition;
2679
2680
2681
2682
2683
2684
2685QModelIndexList QAbstractItemView::selectedIndexes()
const
2687 Q_D(
const QAbstractItemView);
2688 QModelIndexList indexes;
2689 if (d->selectionModel) {
2690 indexes = d->selectionModel->selectedIndexes();
2691 auto isHidden = [
this](
const QModelIndex &idx) {
2692 return isIndexHidden(idx);
2694 indexes.removeIf(isHidden);
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712bool QAbstractItemView::edit(
const QModelIndex &index, EditTrigger trigger, QEvent *event)
2714 Q_D(QAbstractItemView);
2716 if (!d->isIndexValid(index))
2719 if (QWidget *w = (d->persistent.isEmpty() ?
static_cast<QWidget*>(
nullptr) : d->editorForIndex(index).widget.data())) {
2720 if (w->focusPolicy() == Qt::NoFocus)
2722 if (!d->waitForIMCommit)
2729 if (trigger == DoubleClicked) {
2730 d->delayedEditing.stop();
2731 d->delayedAutoScroll.stop();
2732 }
else if (trigger == CurrentChanged) {
2733 d->delayedEditing.stop();
2737 QPersistentModelIndex safeIndex(index);
2739 if (d->sendDelegateEvent(index, event)) {
2744 if (!safeIndex.isValid()) {
2749 EditTriggers lastTrigger = d->lastTrigger;
2750 d->lastTrigger = trigger;
2752 if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
2755 if (d->delayedEditing.isActive())
2760 if (lastTrigger == DoubleClicked && trigger == SelectedClicked)
2764 if (trigger == SelectedClicked)
2765 d->delayedEditing.start(QApplication::doubleClickInterval(),
this);
2767 d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event :
nullptr);
2773
2774
2775
2776void QAbstractItemView::updateEditorData()
2778 Q_D(QAbstractItemView);
2779 d->updateEditorData(QModelIndex(), QModelIndex());
2783
2784
2785
2786void QAbstractItemView::updateEditorGeometries()
2788 Q_D(QAbstractItemView);
2789 if (d->editorIndexHash.isEmpty())
2791 if (d->delayedPendingLayout) {
2793 d->executePostedLayout();
2796 QStyleOptionViewItem option;
2797 initViewItemOption(&option);
2798 QEditorIndexHash::iterator it = d->editorIndexHash.begin();
2799 QWidgetList editorsToRelease;
2800 QWidgetList editorsToHide;
2801 while (it != d->editorIndexHash.end()) {
2802 QModelIndex index = it.value();
2803 QWidget *editor = it.key();
2804 if (index.isValid() && editor) {
2805 option.rect = visualRect(index);
2806 if (option.rect.isValid()) {
2808 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2810 delegate->updateEditorGeometry(editor, option, index);
2812 editorsToHide << editor;
2816 d->indexEditorHash.remove(it.value());
2817 it = d->editorIndexHash.erase(it);
2818 editorsToRelease << editor;
2824 for (
int i = 0; i < editorsToHide.size(); ++i) {
2825 editorsToHide.at(i)->hide();
2827 for (
int i = 0; i < editorsToRelease.size(); ++i) {
2828 d->releaseEditor(editorsToRelease.at(i));
2833
2834
2835void QAbstractItemView::updateGeometries()
2837 Q_D(QAbstractItemView);
2838 updateEditorGeometries();
2839 d->fetchMoreTimer.start(0,
this);
2840 d->updateGeometry();
2844
2845
2846void QAbstractItemView::verticalScrollbarValueChanged(
int value)
2848 Q_D(QAbstractItemView);
2849 if (verticalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2850 d->model->fetchMore(d->root);
2851 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2852 if (viewport()->rect().contains(posInVp))
2853 d->checkMouseMove(posInVp);
2857
2858
2859void QAbstractItemView::horizontalScrollbarValueChanged(
int value)
2861 Q_D(QAbstractItemView);
2862 if (horizontalScrollBar()->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::verticalScrollbarAction(
int)
2878
2879
2880void QAbstractItemView::horizontalScrollbarAction(
int)
2886
2887
2888
2889
2890
2891
2892
2894void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
2896 Q_D(QAbstractItemView);
2900 const bool isPersistent = d->persistent.contains(editor);
2901 const QModelIndex index = d->indexForEditor(editor);
2902 if (!index.isValid()) {
2903 if (!editor->isVisible()) {
2911 qWarning(
"QAbstractItemView::closeEditor called with an editor that does not belong to this view");
2915 const bool hadFocus = editor->hasFocus();
2919 d->pressClosedEditorWatcher.start(0,
this);
2920 d->lastEditedIndex = index;
2922 if (!isPersistent) {
2924 QModelIndex index = d->indexForEditor(editor);
2925 editor->removeEventFilter(itemDelegateForIndex(index));
2926 d->removeEditor(editor);
2929 if (focusPolicy() != Qt::NoFocus)
2932 editor->clearFocus();
2934 d->checkPersistentEditorFocus();
2937 QPointer<QWidget> ed = editor;
2938 QCoreApplication::sendPostedEvents(editor, 0);
2941 if (!isPersistent && editor)
2942 d->releaseEditor(editor, index);
2947 QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::NoUpdate;
2948 if (d->selectionMode != NoSelection)
2949 flags = QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
2951 case QAbstractItemDelegate::EditNextItem: {
2952 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2953 if (index.isValid()) {
2954 QPersistentModelIndex persistent(index);
2955 d->selectionModel->setCurrentIndex(persistent, flags);
2957 if (index.flags() & Qt::ItemIsEditable
2958 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2961 case QAbstractItemDelegate::EditPreviousItem: {
2962 QModelIndex index = moveCursor(MovePrevious, Qt::NoModifier);
2963 if (index.isValid()) {
2964 QPersistentModelIndex persistent(index);
2965 d->selectionModel->setCurrentIndex(persistent, flags);
2967 if (index.flags() & Qt::ItemIsEditable
2968 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2971 case QAbstractItemDelegate::SubmitModelCache:
2974 case QAbstractItemDelegate::RevertModelCache:
2983
2984
2985
2986
2987void QAbstractItemView::commitData(QWidget *editor)
2989 Q_D(QAbstractItemView);
2990 if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
2992 QModelIndex index = d->indexForEditor(editor);
2993 if (!index.isValid()) {
2994 qWarning(
"QAbstractItemView::commitData called with an editor that does not belong to this view");
2997 d->currentlyCommittingEditor = editor;
2998 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2999 editor->removeEventFilter(delegate);
3000 delegate->setModelData(editor, d->model, index);
3001 editor->installEventFilter(delegate);
3002 d->currentlyCommittingEditor =
nullptr;
3006
3007
3008
3009
3010void QAbstractItemView::editorDestroyed(QObject *editor)
3012 Q_D(QAbstractItemView);
3013 QWidget *w = qobject_cast<QWidget*>(editor);
3015 d->persistent.remove(w);
3016 if (state() == EditingState)
3023
3024
3025
3026
3027
3028
3029
3030void QAbstractItemView::keyboardSearch(
const QString &search)
3032 Q_D(QAbstractItemView);
3033 if (!d->model->rowCount(d->root) || !d->model->columnCount(d->root))
3036 QModelIndex start = currentIndex().isValid() ? currentIndex()
3037 : d->model->index(0, 0, d->root);
3038 bool skipRow =
false;
3039 bool keyboardTimeWasValid = d->keyboardInputTime.isValid();
3040 qint64 keyboardInputTimeElapsed;
3041 if (keyboardTimeWasValid)
3042 keyboardInputTimeElapsed = d->keyboardInputTime.restart();
3044 d->keyboardInputTime.start();
3045 if (search.isEmpty() || !keyboardTimeWasValid
3046 || keyboardInputTimeElapsed > QApplication::keyboardInputInterval()) {
3047 d->keyboardInput = search;
3048 skipRow = currentIndex().isValid();
3050 d->keyboardInput += search;
3054 bool sameKey =
false;
3055 if (d->keyboardInput.size() > 1) {
3056 int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
3057 sameKey = (c == d->keyboardInput.size());
3064 QModelIndex parent = start.parent();
3065 int newRow = (start.row() < d->model->rowCount(parent) - 1) ? start.row() + 1 : 0;
3066 start = d->model->index(newRow, start.column(), parent);
3070 QModelIndex current = start;
3071 QModelIndexList match;
3072 QModelIndex firstMatch;
3073 QModelIndex startMatch;
3074 QModelIndexList previous;
3076 match = d->model->match(current, Qt::DisplayRole, d->keyboardInput, 1,
3077 d->keyboardSearchFlags);
3078 if (match == previous)
3080 firstMatch = match.value(0);
3082 if (firstMatch.isValid()) {
3083 if (d->isIndexEnabled(firstMatch)) {
3084 setCurrentIndex(firstMatch);
3087 int row = firstMatch.row() + 1;
3088 if (row >= d->model->rowCount(firstMatch.parent()))
3090 current = firstMatch.sibling(row, firstMatch.column());
3093 if (!startMatch.isValid())
3094 startMatch = firstMatch;
3095 else if (startMatch == firstMatch)
3098 }
while (current != start && firstMatch.isValid());
3102
3103
3104
3105
3106
3107QSize QAbstractItemView::sizeHintForIndex(
const QModelIndex &index)
const
3109 Q_D(
const QAbstractItemView);
3110 if (!d->isIndexValid(index))
3112 const auto delegate = itemDelegateForIndex(index);
3113 QStyleOptionViewItem option;
3114 initViewItemOption(&option);
3115 return delegate ? delegate->sizeHint(option, index) : QSize();
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134int QAbstractItemView::sizeHintForRow(
int row)
const
3136 Q_D(
const QAbstractItemView);
3138 if (row < 0 || row >= d->model->rowCount(d->root))
3143 QStyleOptionViewItem option;
3144 initViewItemOption(&option);
3146 int colCount = d->model->columnCount(d->root);
3147 for (
int c = 0; c < colCount; ++c) {
3148 const QModelIndex index = d->model->index(row, c, d->root);
3149 if (QWidget *editor = d->editorForIndex(index).widget.data())
3150 height = qMax(height, editor->height());
3151 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3152 height = qMax(height, delegate->sizeHint(option, index).height());
3158
3159
3160
3161
3162
3163
3164
3165int QAbstractItemView::sizeHintForColumn(
int column)
const
3167 Q_D(
const QAbstractItemView);
3169 if (column < 0 || column >= d->model->columnCount(d->root))
3174 QStyleOptionViewItem option;
3175 initViewItemOption(&option);
3177 int rows = d->model->rowCount(d->root);
3178 for (
int r = 0; r < rows; ++r) {
3179 const QModelIndex index = d->model->index(r, column, d->root);
3180 if (QWidget *editor = d->editorForIndex(index).widget.data())
3181 width = qMax(width, editor->sizeHint().width());
3182 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3183 width = qMax(width, delegate->sizeHint(option, index).width());
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205int QAbstractItemView::updateThreshold()
const
3207 Q_D(
const QAbstractItemView);
3208 return d->updateThreshold;
3211void QAbstractItemView::setUpdateThreshold(
int threshold)
3213 Q_D(QAbstractItemView);
3214 if (d->updateThreshold == threshold)
3216 d->updateThreshold = threshold;
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3231Qt::MatchFlags QAbstractItemView::keyboardSearchFlags()
const
3233 Q_D(
const QAbstractItemView);
3234 return d->keyboardSearchFlags;
3237void QAbstractItemView::setKeyboardSearchFlags(Qt::MatchFlags searchFlags)
3239 Q_D(QAbstractItemView);
3240 d->keyboardSearchFlags = searchFlags;
3244
3245
3246
3247
3248
3249void QAbstractItemView::openPersistentEditor(
const QModelIndex &index)
3251 Q_D(QAbstractItemView);
3252 QStyleOptionViewItem options;
3253 initViewItemOption(&options);
3254 options.rect = visualRect(index);
3255 options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
3257 QWidget *editor = d->editor(index, options);
3260 d->persistent.insert(editor);
3265
3266
3267
3268
3269void QAbstractItemView::closePersistentEditor(
const QModelIndex &index)
3271 Q_D(QAbstractItemView);
3272 if (QWidget *editor = d->editorForIndex(index).widget.data()) {
3273 if (index == selectionModel()->currentIndex())
3274 closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
3275 d->persistent.remove(editor);
3276 d->removeEditor(editor);
3277 d->releaseEditor(editor, index);
3282
3283
3284
3285
3286
3287
3288bool QAbstractItemView::isPersistentEditorOpen(
const QModelIndex &index)
const
3290 Q_D(
const QAbstractItemView);
3291 QWidget *editor = d->editorForIndex(index).widget;
3292 return editor && d->persistent.contains(editor);
4060void QAbstractItemView::doAutoScroll()
4063 Q_D(QAbstractItemView);
4064 QScrollBar *verticalScroll = verticalScrollBar();
4065 QScrollBar *horizontalScroll = horizontalScrollBar();
4069 QHeaderView *hv = qobject_cast<QHeaderView*>(
this);
4071 QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
4073 if (hv->orientation() == Qt::Horizontal) {
4074 if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
4075 horizontalScroll = parent->horizontalScrollBar();
4077 if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
4078 verticalScroll = parent->verticalScrollBar();
4083 const int verticalStep = verticalScroll->pageStep();
4084 const int horizontalStep = horizontalScroll->pageStep();
4085 if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
4086 ++d->autoScrollCount;
4088 const int margin = d->autoScrollMargin;
4089 const int verticalValue = verticalScroll->value();
4090 const int horizontalValue = horizontalScroll->value();
4092 const QPoint pos = d->draggedPosition - d->offset();
4093 const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
4096 if (pos.y() - area.top() < margin)
4097 verticalScroll->setValue(verticalValue - d->autoScrollCount);
4098 else if (area.bottom() - pos.y() < margin)
4099 verticalScroll->setValue(verticalValue + d->autoScrollCount);
4100 if (pos.x() - area.left() < margin)
4101 horizontalScroll->setValue(horizontalValue - d->autoScrollCount);
4102 else if (area.right() - pos.x() < margin)
4103 horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
4105 const bool verticalUnchanged = (verticalValue == verticalScroll->value());
4106 const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
4107 if (verticalUnchanged && horizontalUnchanged) {
4110#if QT_CONFIG(draganddrop)
4111 d->dropIndicatorRect = QRect();
4112 d->dropIndicatorPosition = QAbstractItemView::OnViewport;
4115 case QAbstractItemView::DragSelectingState: {
4118 const QPoint globalPos = d->viewport->mapToGlobal(pos);
4119 const QPoint windowPos = window()->mapFromGlobal(globalPos);
4120 QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
4121 Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
4122 Qt::MouseEventSynthesizedByQt);
4123 QApplication::sendEvent(viewport(), &mm);
4126 case QAbstractItemView::DraggingState: {
4132 d->draggedPosition = pos + d->offset();
4138 d->viewport->update();
4242QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
4243 const QModelIndex &index,
const QEvent *event)
const
4245 Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
4246 ?
static_cast<
const QInputEvent*>(event)->modifiers()
4247 : QGuiApplication::keyboardModifiers();
4249 switch (event->type()) {
4250 case QEvent::MouseMove: {
4252 if (modifiers & Qt::ControlModifier)
4253 return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
4256 case QEvent::MouseButtonPress: {
4257 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4258 const bool rightButtonPressed = button & Qt::RightButton;
4259 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4260 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4261 const bool indexIsSelected = selectionModel->isSelected(index);
4262 if ((shiftKeyPressed || controlKeyPressed) && rightButtonPressed)
4263 return QItemSelectionModel::NoUpdate;
4264 if (!shiftKeyPressed && !controlKeyPressed && indexIsSelected)
4265 return QItemSelectionModel::NoUpdate;
4266 if (!index.isValid() && !rightButtonPressed && !shiftKeyPressed && !controlKeyPressed)
4267 return QItemSelectionModel::Clear;
4268 if (!index.isValid())
4269 return QItemSelectionModel::NoUpdate;
4271 if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
4272#if QT_CONFIG(draganddrop)
4273 && dragEnabled && isIndexDragEnabled(index)
4276 return QItemSelectionModel::NoUpdate;
4280 case QEvent::MouseButtonRelease: {
4282 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4283 const bool rightButtonPressed = button & Qt::RightButton;
4284 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4285 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4286 if (((index == pressedIndex && selectionModel->isSelected(index))
4287 || !index.isValid()) && state != QAbstractItemView::DragSelectingState
4288 && !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
4289 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
4290 if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
4291#if QT_CONFIG(draganddrop)
4292 && dragEnabled && isIndexDragEnabled(index)
4297 return QItemSelectionModel::NoUpdate;
4299 case QEvent::KeyPress: {
4301 switch (
static_cast<
const QKeyEvent*>(event)->key()) {
4302 case Qt::Key_Backtab:
4303 modifiers = modifiers & ~Qt::ShiftModifier;
4311 case Qt::Key_PageUp:
4312 case Qt::Key_PageDown:
4314 if (modifiers & Qt::ControlModifier)
4315 return QItemSelectionModel::NoUpdate;
4317 case Qt::Key_Select:
4318 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4320 if (modifiers & Qt::ControlModifier)
4321 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4322 return QItemSelectionModel::Select|selectionBehaviorFlags();
4333 if (modifiers & Qt::ShiftModifier)
4334 return QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4335 if (modifiers & Qt::ControlModifier)
4336 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4337 if (state == QAbstractItemView::DragSelectingState) {
4339 return QItemSelectionModel::Clear|QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4342 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();