191 for (
int role : roles) {
192 if (role == Qt::AccessibleTextRole
193 || (role == Qt::DisplayRole
194 && index.data(Qt::AccessibleTextRole).toString().isEmpty())) {
195 QAccessibleEvent event(q, QAccessible::NameChanged);
196 event.setChild(childIndex);
197 QAccessible::updateAccessibility(&event);
198 }
else if (role == Qt::AccessibleDescriptionRole) {
199 QAccessibleEvent event(q, QAccessible::DescriptionChanged);
200 event.setChild(childIndex);
201 QAccessible::updateAccessibility(&event);
202 }
else if (role == Qt::CheckStateRole) {
203 QAccessible::State state;
204 state.checked =
true;
205 QAccessibleStateChangeEvent event(q, state);
206 event.setChild(childIndex);
207 QAccessible::updateAccessibility(&event);
213#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
216void QAbstractItemViewPrivate::scrollerStateChanged()
218 Q_Q(QAbstractItemView);
220 if (QScroller *scroller = QScroller::scroller(viewport)) {
221 switch (scroller->state()) {
222 case QScroller::Pressed:
224 if (q->selectionModel()) {
225 oldSelection = q->selectionModel()->selection();
226 oldCurrent = q->selectionModel()->currentIndex();
230 case QScroller::Dragging:
232 if (q->selectionModel()) {
233 q->selectionModel()->select(oldSelection, QItemSelectionModel::ClearAndSelect);
235 const bool wasAutoScroll = autoScroll;
237 q->selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
238 autoScroll = wasAutoScroll;
243 oldSelection = QItemSelection();
244 oldCurrent = QModelIndex();
252void QAbstractItemViewPrivate::delegateSizeHintChanged(
const QModelIndex &index)
254 Q_Q(QAbstractItemView);
256 if (!model->checkIndex(index))
257 qCWarning(lcAIV,
"Delegate size hint changed for a model index that does not belong to this view");
259 QMetaObject::invokeMethod(q, &QAbstractItemView::doItemsLayout, Qt::QueuedConnection);
262void QAbstractItemViewPrivate::connectDelegate(QAbstractItemDelegate *delegate)
266 Q_Q(QAbstractItemView);
267 QObject::connect(delegate, &QAbstractItemDelegate::closeEditor,
268 q, &QAbstractItemView::closeEditor);
269 QObject::connect(delegate, &QAbstractItemDelegate::commitData,
270 q, &QAbstractItemView::commitData);
271 QObjectPrivate::connect(delegate, &QAbstractItemDelegate::sizeHintChanged,
272 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
275void QAbstractItemViewPrivate::disconnectDelegate(QAbstractItemDelegate *delegate)
279 Q_Q(QAbstractItemView);
280 QObject::disconnect(delegate, &QAbstractItemDelegate::closeEditor,
281 q, &QAbstractItemView::closeEditor);
282 QObject::disconnect(delegate, &QAbstractItemDelegate::commitData,
283 q, &QAbstractItemView::commitData);
284 QObjectPrivate::disconnect(delegate, &QAbstractItemDelegate::sizeHintChanged,
285 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
288void QAbstractItemViewPrivate::disconnectAll()
290 Q_Q(QAbstractItemView);
291 for (QMetaObject::Connection &connection : modelConnections)
292 QObject::disconnect(connection);
293 for (QMetaObject::Connection &connection : scrollbarConnections)
294 QObject::disconnect(connection);
295 disconnectDelegate(itemDelegate);
296 for (QAbstractItemDelegate *delegate : std::as_const(rowDelegates))
297 disconnectDelegate(delegate);
298 for (QAbstractItemDelegate *delegate : std::as_const(columnDelegates))
299 disconnectDelegate(delegate);
300 if (model && selectionModel) {
301 QObject::disconnect(model, &QAbstractItemModel::destroyed,
302 selectionModel, &QItemSelectionModel::deleteLater);
304 if (selectionModel) {
305 QObject::disconnect(selectionModel, &QItemSelectionModel::selectionChanged,
306 q, &QAbstractItemView::selectionChanged);
307 QObject::disconnect(selectionModel, &QItemSelectionModel::currentChanged,
308 q, &QAbstractItemView::currentChanged);
310 for (
const auto &info : std::as_const(indexEditorHash)) {
311 if (!info.isStatic && info.widget)
312 QObject::disconnect(info.widget, &QWidget::destroyed, q, &QAbstractItemView::editorDestroyed);
314#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
315 QObject::disconnect(scollerConnection);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
456
457
458
459
460
461
464
465
466
467
468
469
470
471
472
473
477
478
479
480
481
482
483
484
485
486
487
488
489
490
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
511
512
513
514
515
516
517
518
519
520
521
522
523
526
527
528
529
530
531
532
533
534
535
538
539
540
541
542
543
544
545
546
547
548
551
552
553
554
555
556
557
560
561
562
563
564
565
566
567
570
571
572
573
574
575
576
577
578
581
582
583
584
585
586
587
588
591
592
593
594
595
596
597
600
601
602
603
604
605
606
607
608
609
610
613
614
615
616
617
618
619
620
623
624
625
626
627
628
629
630
633
634
635
636
637
638
639
640
643
644
645
646
647
648
649
650
653
654
655
656
657
658
659
660
663
664
665
666
667
668
669
670
671
672
675
676
677
678
679
680
681
682
683
684
685
686
689
690
691
692
693
694
695
696
697
700
701
702QAbstractItemView::QAbstractItemView(QWidget *parent)
703 : QAbstractScrollArea(*(
new QAbstractItemViewPrivate), parent)
709
710
711QAbstractItemView::QAbstractItemView(QAbstractItemViewPrivate &dd, QWidget *parent)
712 : QAbstractScrollArea(dd, parent)
718
719
720QAbstractItemView::~QAbstractItemView()
722 Q_D(QAbstractItemView);
724 d->delayedReset.stop();
725 d->updateTimer.stop();
726 d->delayedEditing.stop();
727 d->delayedAutoScroll.stop();
728 d->autoScrollTimer.stop();
729 d->delayedLayout.stop();
730 d->fetchMoreTimer.stop();
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754void QAbstractItemView::setModel(QAbstractItemModel *model)
756 Q_D(QAbstractItemView);
757 if (model == d->model)
759 if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
760 for (QMetaObject::Connection &connection : d->modelConnections)
761 disconnect(connection);
763 d->model = (model ? model : QAbstractItemModelPrivate::staticEmptyModel());
765 if (d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
766 d->modelConnections = {
767 QObjectPrivate::connect(d->model, &QAbstractItemModel::destroyed,
768 d, &QAbstractItemViewPrivate::modelDestroyed),
769 QObject::connect(d->model, &QAbstractItemModel::dataChanged,
770 this, &QAbstractItemView::dataChanged),
771 QObjectPrivate::connect(d->model, &QAbstractItemModel::headerDataChanged,
772 d, &QAbstractItemViewPrivate::headerDataChanged),
773 QObject::connect(d->model, &QAbstractItemModel::rowsInserted,
774 this, &QAbstractItemView::rowsInserted),
775 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsInserted,
776 d, &QAbstractItemViewPrivate::rowsInserted),
777 QObject::connect(d->model, &QAbstractItemModel::rowsAboutToBeRemoved,
778 this, &QAbstractItemView::rowsAboutToBeRemoved),
779 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsRemoved,
780 d, &QAbstractItemViewPrivate::rowsRemoved),
781 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsMoved,
782 d, &QAbstractItemViewPrivate::rowsMoved),
783 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeRemoved,
784 d, &QAbstractItemViewPrivate::columnsAboutToBeRemoved),
785 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsRemoved,
786 d, &QAbstractItemViewPrivate::columnsRemoved),
787 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsInserted,
788 d, &QAbstractItemViewPrivate::columnsInserted),
789 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsMoved,
790 d, &QAbstractItemViewPrivate::columnsMoved),
791 QObject::connect(d->model, &QAbstractItemModel::modelReset,
792 this, &QAbstractItemView::reset),
793 QObjectPrivate::connect(d->model, &QAbstractItemModel::layoutChanged,
794 d, &QAbstractItemViewPrivate::layoutChanged),
798 QItemSelectionModel *selection_model =
new QItemSelectionModel(d->model,
this);
799 connect(d->model, &QAbstractItemModel::destroyed,
800 selection_model, &QItemSelectionModel::deleteLater);
801 setSelectionModel(selection_model);
807
808
809QAbstractItemModel *QAbstractItemView::model()
const
811 Q_D(
const QAbstractItemView);
812 return (d->model == QAbstractItemModelPrivate::staticEmptyModel() ?
nullptr : d->model);
816
817
818
819
820
821
822
823
824
825
826
827
828
829void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
832 Q_ASSERT(selectionModel);
833 Q_D(QAbstractItemView);
835 if (Q_UNLIKELY(selectionModel->model() != d->model)) {
836 qCWarning(lcAIV,
"QAbstractItemView::setSelectionModel() failed: "
837 "Trying to set a selection model, which works on "
838 "a different model than the view.");
842 QItemSelection oldSelection;
843 QModelIndex oldCurrentIndex;
845 if (d->selectionModel) {
846 if (d->selectionModel->model() == selectionModel->model()) {
847 oldSelection = d->selectionModel->selection();
848 oldCurrentIndex = d->selectionModel->currentIndex();
850 disconnect(d->selectionModel, &QItemSelectionModel::selectionChanged,
851 this, &QAbstractItemView::selectionChanged);
852 disconnect(d->selectionModel, &QItemSelectionModel::currentChanged,
853 this, &QAbstractItemView::currentChanged);
856 d->selectionModel = selectionModel;
858 if (d->selectionModel) {
859 connect(d->selectionModel, &QItemSelectionModel::selectionChanged,
860 this, &QAbstractItemView::selectionChanged);
861 connect(d->selectionModel, &QItemSelectionModel::currentChanged,
862 this, &QAbstractItemView::currentChanged);
864 selectionChanged(d->selectionModel->selection(), oldSelection);
865 currentChanged(d->selectionModel->currentIndex(), oldCurrentIndex);
870
871
872
873
874
875
876
877
878
879
880QItemSelectionModel* QAbstractItemView::selectionModel()
const
882 Q_D(
const QAbstractItemView);
883 return d->selectionModel;
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
903 Q_D(QAbstractItemView);
904 if (delegate == d->itemDelegate)
907 if (d->itemDelegate) {
908 if (d->delegateRefCount(d->itemDelegate) == 1)
909 d->disconnectDelegate(d->itemDelegate);
913 if (d->delegateRefCount(delegate) == 0)
914 d->connectDelegate(delegate);
916 d->itemDelegate = delegate;
917 viewport()->update();
918 d->doDelayedItemsLayout();
922
923
924
925
926
927QAbstractItemDelegate *QAbstractItemView::itemDelegate()
const
929 return d_func()->itemDelegate;
933
934
935QVariant QAbstractItemView::inputMethodQuery(Qt::InputMethodQuery query)
const
937 Q_D(
const QAbstractItemView);
938 const QModelIndex current = currentIndex();
940 if (current.isValid()) {
941 if (QWidget *currentEditor;
942 d->waitForIMCommit && (currentEditor = d->editorForIndex(current).widget)) {
945 result = currentEditor->inputMethodQuery(query);
946 if (result.typeId() == QMetaType::QRect) {
947 const QRect editorRect = result.value<QRect>();
948 result = QRect(currentEditor->mapTo(
this, editorRect.topLeft()), editorRect.size());
950 }
else if (query == Qt::ImCursorRectangle) {
951 const QRect visRect = visualRect(current);
952 result = QRect(d->viewport->mapTo(
this, visRect.topLeft()), visRect.size());
955 if (!result.isValid())
956 result = QAbstractScrollArea::inputMethodQuery(query);
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979void QAbstractItemView::setItemDelegateForRow(
int row, QAbstractItemDelegate *delegate)
981 Q_D(QAbstractItemView);
982 if (QAbstractItemDelegate *rowDelegate = d->rowDelegates.value(row,
nullptr)) {
983 if (d->delegateRefCount(rowDelegate) == 1)
984 d->disconnectDelegate(rowDelegate);
985 d->rowDelegates.remove(row);
988 if (d->delegateRefCount(delegate) == 0)
989 d->connectDelegate(delegate);
990 d->rowDelegates.insert(row, delegate);
992 viewport()->update();
993 d->doDelayedItemsLayout();
997
998
999
1000
1001
1002
1003QAbstractItemDelegate *QAbstractItemView::itemDelegateForRow(
int row)
const
1005 Q_D(
const QAbstractItemView);
1006 return d->rowDelegates.value(row,
nullptr);
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027void QAbstractItemView::setItemDelegateForColumn(
int column, QAbstractItemDelegate *delegate)
1029 Q_D(QAbstractItemView);
1030 if (QAbstractItemDelegate *columnDelegate = d->columnDelegates.value(column,
nullptr)) {
1031 if (d->delegateRefCount(columnDelegate) == 1)
1032 d->disconnectDelegate(columnDelegate);
1033 d->columnDelegates.remove(column);
1036 if (d->delegateRefCount(delegate) == 0)
1037 d->connectDelegate(delegate);
1038 d->columnDelegates.insert(column, delegate);
1040 viewport()->update();
1041 d->doDelayedItemsLayout();
1045
1046
1047
1048
1049
1050
1051QAbstractItemDelegate *QAbstractItemView::itemDelegateForColumn(
int column)
const
1053 Q_D(
const QAbstractItemView);
1054 return d->columnDelegates.value(column,
nullptr);
1058
1059
1060
1061
1062
1063
1066
1067
1068
1069
1070
1071
1072
1073QAbstractItemDelegate *QAbstractItemView::itemDelegateForIndex(
const QModelIndex &index)
const
1075 Q_D(
const QAbstractItemView);
1076 return d->delegateForIndex(index);
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089void QAbstractItemView::setSelectionMode(SelectionMode mode)
1091 Q_D(QAbstractItemView);
1092 d->selectionMode = mode;
1095QAbstractItemView::SelectionMode QAbstractItemView::selectionMode()
const
1097 Q_D(
const QAbstractItemView);
1098 return d->selectionMode;
1102
1103
1104
1105
1106
1107
1108
1109
1111void QAbstractItemView::setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
1113 Q_D(QAbstractItemView);
1114 d->selectionBehavior = behavior;
1117QAbstractItemView::SelectionBehavior QAbstractItemView::selectionBehavior()
const
1119 Q_D(
const QAbstractItemView);
1120 return d->selectionBehavior;
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137void QAbstractItemView::setCurrentIndex(
const QModelIndex &index)
1139 Q_D(QAbstractItemView);
1140 if (d->selectionModel && (!index.isValid() || d->isIndexEnabled(index))) {
1141 QItemSelectionModel::SelectionFlags command = selectionCommand(index,
nullptr);
1142 d->selectionModel->setCurrentIndex(index, command);
1143 d->currentIndexSet =
true;
1148
1149
1150
1151
1152QModelIndex QAbstractItemView::currentIndex()
const
1154 Q_D(
const QAbstractItemView);
1155 return d->selectionModel ? d->selectionModel->currentIndex() : QModelIndex();
1160
1161
1162
1163
1164
1165
1166
1167
1168void QAbstractItemView::reset()
1170 Q_D(QAbstractItemView);
1171 d->delayedReset.stop();
1175 const auto copy = d->indexEditorHash;
1176 for (
const auto &[index, info] : copy.asKeyValueRange()) {
1178 d->releaseEditor(info.widget.data(), d->indexForEditor(info.widget.data()));
1180 d->editorIndexHash.clear();
1181 d->indexEditorHash.clear();
1182 d->persistent.clear();
1183 d->currentIndexSet =
false;
1185 setRootIndex(QModelIndex());
1186 if (d->selectionModel)
1187 d->selectionModel->reset();
1188#if QT_CONFIG(accessibility)
1189 if (QAccessible::isActive()) {
1190 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1191 QAccessible::updateAccessibility(&accessibleEvent);
1194 d->updateGeometry();
1198
1199
1200
1201
1202void QAbstractItemView::setRootIndex(
const QModelIndex &index)
1204 Q_D(QAbstractItemView);
1205 if (Q_UNLIKELY(index.isValid() && index.model() != d->model)) {
1206 qWarning(
"QAbstractItemView::setRootIndex failed : index must be from the currently set model");
1210#if QT_CONFIG(accessibility)
1211 if (QAccessible::isActive()) {
1212 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1213 QAccessible::updateAccessibility(&accessibleEvent);
1216 d->doDelayedItemsLayout();
1217 d->updateGeometry();
1221
1222
1223
1224
1225
1226QModelIndex QAbstractItemView::rootIndex()
const
1228 return QModelIndex(d_func()->root);
1232
1233
1234
1235
1236
1237
1238void QAbstractItemView::selectAll()
1240 Q_D(QAbstractItemView);
1241 const SelectionMode mode = d->selectionMode;
1243 case MultiSelection:
1244 case ExtendedSelection:
1245 d->selectAll(QItemSelectionModel::ClearAndSelect
1246 | d->selectionBehaviorFlags());
1249 case ContiguousSelection:
1250 if (d->model->hasChildren(d->root))
1251 d->selectAll(selectionCommand(d->model->index(0, 0, d->root)));
1253 case SingleSelection:
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270void QAbstractItemView::edit(
const QModelIndex &index)
1272 Q_D(QAbstractItemView);
1273 if (Q_UNLIKELY(!d->isIndexValid(index)))
1274 qCWarning(lcAIV,
"edit: index was invalid");
1275 if (Q_UNLIKELY(!edit(index, AllEditTriggers,
nullptr)))
1276 qCWarning(lcAIV,
"edit: editing failed");
1280
1281
1282
1283
1284void QAbstractItemView::clearSelection()
1286 Q_D(QAbstractItemView);
1287 if (d->selectionModel)
1288 d->selectionModel->clearSelection();
1292
1293
1294
1295
1296
1297void QAbstractItemView::doItemsLayout()
1299 Q_D(QAbstractItemView);
1300 d->interruptDelayedItemsLayout();
1302 d->viewport->update();
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320void QAbstractItemView::setEditTriggers(EditTriggers actions)
1322 Q_D(QAbstractItemView);
1323 d->editTriggers = actions;
1326QAbstractItemView::EditTriggers QAbstractItemView::editTriggers()
const
1328 Q_D(
const QAbstractItemView);
1329 return d->editTriggers;
1333
1334
1335
1336
1337
1338
1339
1341void QAbstractItemView::setVerticalScrollMode(ScrollMode mode)
1343 Q_D(QAbstractItemView);
1344 d->verticalScrollModeSet =
true;
1345 if (mode == d->verticalScrollMode)
1347 QModelIndex topLeft = indexAt(QPoint(0, 0));
1348 d->verticalScrollMode = mode;
1349 if (mode == ScrollPerItem)
1350 verticalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1352 verticalScrollBar()->setSingleStep(-1);
1354 scrollTo(topLeft, QAbstractItemView::PositionAtTop);
1357QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode()
const
1359 Q_D(
const QAbstractItemView);
1360 return d->verticalScrollMode;
1363void QAbstractItemView::resetVerticalScrollMode()
1365 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1366 setVerticalScrollMode(sm);
1367 d_func()->verticalScrollModeSet =
false;
1371
1372
1373
1374
1375
1376
1377
1379void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode)
1381 Q_D(QAbstractItemView);
1382 d->horizontalScrollModeSet =
true;
1383 if (mode == d->horizontalScrollMode)
1385 d->horizontalScrollMode = mode;
1386 if (mode == ScrollPerItem)
1387 horizontalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1389 horizontalScrollBar()->setSingleStep(-1);
1393QAbstractItemView::ScrollMode QAbstractItemView::horizontalScrollMode()
const
1395 Q_D(
const QAbstractItemView);
1396 return d->horizontalScrollMode;
1399void QAbstractItemView::resetHorizontalScrollMode()
1401 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1402 setHorizontalScrollMode(sm);
1403 d_func()->horizontalScrollModeSet =
false;
1406#if QT_CONFIG(draganddrop)
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427void QAbstractItemView::setDragDropOverwriteMode(
bool overwrite)
1429 Q_D(QAbstractItemView);
1430 d->overwrite = overwrite;
1433bool QAbstractItemView::dragDropOverwriteMode()
const
1435 Q_D(
const QAbstractItemView);
1436 return d->overwrite;
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1454void QAbstractItemView::setAutoScroll(
bool enable)
1456 Q_D(QAbstractItemView);
1457 d->autoScroll = enable;
1460bool QAbstractItemView::hasAutoScroll()
const
1462 Q_D(
const QAbstractItemView);
1463 return d->autoScroll;
1467
1468
1469
1470
1471
1472
1473void QAbstractItemView::setAutoScrollMargin(
int margin)
1475 Q_D(QAbstractItemView);
1476 d->autoScrollMargin = margin;
1479int QAbstractItemView::autoScrollMargin()
const
1481 Q_D(
const QAbstractItemView);
1482 return d->autoScrollMargin;
1486
1487
1488
1490void QAbstractItemView::setTabKeyNavigation(
bool enable)
1492 Q_D(QAbstractItemView);
1493 d->tabKeyNavigation = enable;
1496bool QAbstractItemView::tabKeyNavigation()
const
1498 Q_D(
const QAbstractItemView);
1499 return d->tabKeyNavigation;
1503
1504
1505
1506QSize QAbstractItemView::viewportSizeHint()
const
1508 return QAbstractScrollArea::viewportSizeHint();
1511#if QT_CONFIG(draganddrop)
1513
1514
1515
1516
1517
1519void QAbstractItemView::setDropIndicatorShown(
bool enable)
1521 Q_D(QAbstractItemView);
1522 d->showDropIndicator = enable;
1525bool QAbstractItemView::showDropIndicator()
const
1527 Q_D(
const QAbstractItemView);
1528 return d->showDropIndicator;
1532
1533
1534
1535
1536
1538void QAbstractItemView::setDragEnabled(
bool enable)
1540 Q_D(QAbstractItemView);
1541 d->dragEnabled = enable;
1544bool QAbstractItemView::dragEnabled()
const
1546 Q_D(
const QAbstractItemView);
1547 return d->dragEnabled;
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1570
1571
1572
1573
1574
1575void QAbstractItemView::setDragDropMode(DragDropMode behavior)
1577 Q_D(QAbstractItemView);
1578 d->dragDropMode = behavior;
1579 setDragEnabled(behavior == DragOnly || behavior == DragDrop || behavior == InternalMove);
1580 setAcceptDrops(behavior == DropOnly || behavior == DragDrop || behavior == InternalMove);
1583QAbstractItemView::DragDropMode QAbstractItemView::dragDropMode()
const
1585 Q_D(
const QAbstractItemView);
1586 DragDropMode setBehavior = d->dragDropMode;
1587 if (!dragEnabled() && !acceptDrops())
1590 if (dragEnabled() && !acceptDrops())
1593 if (!dragEnabled() && acceptDrops())
1596 if (dragEnabled() && acceptDrops()) {
1597 if (setBehavior == InternalMove)
1607
1608
1609
1610
1611
1612
1613
1614
1615void QAbstractItemView::setDefaultDropAction(Qt::DropAction dropAction)
1617 Q_D(QAbstractItemView);
1618 d->defaultDropAction = dropAction;
1621Qt::DropAction QAbstractItemView::defaultDropAction()
const
1623 Q_D(
const QAbstractItemView);
1624 return d->defaultDropAction;
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639void QAbstractItemView::setAlternatingRowColors(
bool enable)
1641 Q_D(QAbstractItemView);
1642 d->alternatingColors = enable;
1644 d->viewport->update();
1647bool QAbstractItemView::alternatingRowColors()
const
1649 Q_D(
const QAbstractItemView);
1650 return d->alternatingColors;
1654
1655
1656
1657
1658
1659
1660void QAbstractItemView::setIconSize(
const QSize &size)
1662 Q_D(QAbstractItemView);
1663 if (size == d->iconSize)
1666 d->doDelayedItemsLayout();
1667 emit iconSizeChanged(size);
1670QSize QAbstractItemView::iconSize()
const
1672 Q_D(
const QAbstractItemView);
1677
1678
1679
1680
1681
1682
1683void QAbstractItemView::setTextElideMode(Qt::TextElideMode mode)
1685 Q_D(QAbstractItemView);
1686 d->textElideMode = mode;
1689Qt::TextElideMode QAbstractItemView::textElideMode()
const
1691 return d_func()->textElideMode;
1695
1696
1697bool QAbstractItemView::focusNextPrevChild(
bool next)
1699 Q_D(QAbstractItemView);
1700 if (d->tabKeyNavigation && isVisible() && isEnabled() && d->viewport->isEnabled()) {
1701 QKeyEvent event(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier);
1702 keyPressEvent(&event);
1703 if (event.isAccepted())
1706 return QAbstractScrollArea::focusNextPrevChild(next);
1710
1711
1712bool QAbstractItemView::event(QEvent *event)
1714 Q_D(QAbstractItemView);
1715 switch (event->type()) {
1719 d->executePostedLayout();
1722 d->executePostedLayout();
1723 if (d->shouldScrollToCurrentOnShow) {
1724 d->shouldScrollToCurrentOnShow =
false;
1725 const QModelIndex current = currentIndex();
1726 if (current.isValid() && (d->state == QAbstractItemView::EditingState || d->autoScroll))
1730 case QEvent::LocaleChange:
1731 viewport()->update();
1733 case QEvent::LayoutDirectionChange:
1734 case QEvent::ApplicationLayoutDirectionChange:
1737 case QEvent::StyleChange:
1739 if (!d->verticalScrollModeSet)
1740 resetVerticalScrollMode();
1741 if (!d->horizontalScrollModeSet)
1742 resetHorizontalScrollMode();
1744 case QEvent::FocusOut:
1745 d->checkPersistentEditorFocus();
1747 case QEvent::FontChange:
1748 d->doDelayedItemsLayout();
1753 return QAbstractScrollArea::event(event);
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767bool QAbstractItemView::viewportEvent(QEvent *event)
1769 Q_D(QAbstractItemView);
1770 switch (event->type()) {
1775 d->executePostedLayout();
1777 case QEvent::HoverMove:
1778 case QEvent::HoverEnter:
1779 d->setHoverIndex(indexAt(
static_cast<QHoverEvent*>(event)->position().toPoint()));
1781 case QEvent::HoverLeave:
1782 d->setHoverIndex(QModelIndex());
1785 d->viewportEnteredNeeded =
true;
1788 d->setHoverIndex(QModelIndex());
1789 #if QT_CONFIG(statustip)
1790 if (d->shouldClearStatusTip && d->parent) {
1792 QStatusTipEvent tip(empty);
1793 QCoreApplication::sendEvent(d->parent, &tip);
1794 d->shouldClearStatusTip =
false;
1797 d->enteredIndex = QModelIndex();
1799 case QEvent::ToolTip:
1800 case QEvent::QueryWhatsThis:
1801 case QEvent::WhatsThis: {
1802 QHelpEvent *he =
static_cast<QHelpEvent*>(event);
1803 const QModelIndex index = indexAt(he->pos());
1804 QStyleOptionViewItem option;
1805 initViewItemOption(&option);
1806 option.rect = visualRect(index);
1807 option.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
1809 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
1812 return delegate->helpEvent(he,
this, option, index);
1814 case QEvent::FontChange:
1815 d->doDelayedItemsLayout();
1817 case QEvent::WindowActivate:
1818 case QEvent::WindowDeactivate:
1819 d->viewport->update();
1821 case QEvent::ScrollPrepare:
1822 executeDelayedItemsLayout();
1823#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
1824 d->scollerConnection = QObjectPrivate::connect(
1825 QScroller::scroller(d->viewport), &QScroller::stateChanged,
1826 d, &QAbstractItemViewPrivate::scrollerStateChanged,
1827 Qt::UniqueConnection);
1834 return QAbstractScrollArea::viewportEvent(event);
1838
1839
1840
1841
1842void QAbstractItemView::mousePressEvent(QMouseEvent *event)
1844 Q_D(QAbstractItemView);
1845 d->releaseFromDoubleClick =
false;
1846 d->delayedAutoScroll.stop();
1847 QPoint pos = event->position().toPoint();
1848 QPersistentModelIndex index = indexAt(pos);
1851 d->pressClosedEditor = d->pressClosedEditorWatcher.isActive() && d->lastEditedIndex == index;
1853 if (!d->selectionModel || (d->state == EditingState && d->hasEditor(index)))
1856 d->pressedAlreadySelected = d->selectionModel->isSelected(index);
1857 d->pressedIndex = index;
1858 d->pressedModifiers = event->modifiers();
1859 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1860 d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid();
1861 QPoint offset = d->offset();
1862 d->draggedPosition = pos + offset;
1864#if QT_CONFIG(draganddrop)
1867 d->pressedPosition = d->draggedPosition;
1870 if (!(command & QItemSelectionModel::Current)) {
1871 d->pressedPosition = pos + offset;
1872 d->currentSelectionStartIndex = index;
1874 else if (!d->currentSelectionStartIndex.isValid())
1875 d->currentSelectionStartIndex = currentIndex();
1877 if (edit(index, NoEditTriggers, event))
1880 if (index.isValid() && d->isIndexEnabled(index)) {
1883 bool autoScroll = d->autoScroll;
1884 d->autoScroll =
false;
1885 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1886 d->autoScroll = autoScroll;
1887 if (command.testFlag(QItemSelectionModel::Toggle)) {
1888 command &= ~QItemSelectionModel::Toggle;
1889 d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
1890 command |= d->ctrlDragSelectionFlag;
1893 if (!(command & QItemSelectionModel::Current)) {
1894 setSelection(QRect(pos, QSize(1, 1)), command);
1896 QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
1897 setSelection(rect, command);
1901 emit pressed(index);
1902 if (d->autoScroll) {
1905 d->delayedAutoScroll.start(QApplication::doubleClickInterval()+100,
this);
1910 d->selectionModel->select(QModelIndex(), QItemSelectionModel::Select);
1915
1916
1917
1918
1919void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
1921 Q_D(QAbstractItemView);
1922 QPoint bottomRight = event->position().toPoint();
1924 d->draggedPosition = bottomRight + d->offset();
1926 if (state() == ExpandingState || state() == CollapsingState)
1929#if QT_CONFIG(draganddrop)
1930 if (state() == DraggingState) {
1931 d->maybeStartDrag(bottomRight);
1936 QPersistentModelIndex index = indexAt(bottomRight);
1937 QModelIndex buddy = d->model->buddy(d->pressedIndex);
1938 if ((state() == EditingState && d->hasEditor(buddy))
1939 || edit(index, NoEditTriggers, event))
1942 const QPoint topLeft =
1943 d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight;
1945 d->checkMouseMove(index);
1947#if QT_CONFIG(draganddrop)
1948 if (d->pressedIndex.isValid()
1950 && (state() != DragSelectingState)
1951 && (event->buttons() != Qt::NoButton)
1952 && !d->selectedDraggableIndexes().isEmpty()) {
1953 setState(DraggingState);
1954 d->maybeStartDrag(bottomRight);
1959 if ((event->buttons() & Qt::LeftButton) && d->selectionAllowed(index) && d->selectionModel) {
1960 setState(DragSelectingState);
1961 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1962 if (d->ctrlDragSelectionFlag != QItemSelectionModel::NoUpdate && command.testFlag(QItemSelectionModel::Toggle)) {
1963 command &= ~QItemSelectionModel::Toggle;
1964 command |= d->ctrlDragSelectionFlag;
1968 QRect selectionRect = QRect(topLeft, bottomRight);
1969 setSelection(selectionRect, command);
1972 if (index.isValid() && (index != d->selectionModel->currentIndex()) && d->isIndexEnabled(index))
1973 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1974 else if (d->shouldAutoScroll(event->pos()) && !d->autoScrollTimer.isActive())
1980
1981
1982
1983
1984
1985
1986void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
1988 Q_D(QAbstractItemView);
1989 const bool releaseFromDoubleClick = d->releaseFromDoubleClick;
1990 d->releaseFromDoubleClick =
false;
1992 QPoint pos = event->position().toPoint();
1993 QPersistentModelIndex index = indexAt(pos);
1995 if (state() == EditingState) {
1996 if (d->isIndexValid(index)
1997 && d->isIndexEnabled(index)
1998 && d->sendDelegateEvent(index, event))
2003 bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
2004 bool selectedClicked = click && d->pressedAlreadySelected
2005 && (event->button() == Qt::LeftButton)
2006 && (event->modifiers() == Qt::NoModifier);
2007 EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
2008 const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) :
false;
2010 d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
2012 if (d->selectionModel && d->noSelectionOnMousePress) {
2013 d->noSelectionOnMousePress =
false;
2014 if (!d->pressClosedEditor)
2015 d->selectionModel->select(index, selectionCommand(index, event));
2018 d->pressClosedEditor =
false;
2023 if (event->button() == Qt::LeftButton)
2024 emit clicked(index);
2027 QStyleOptionViewItem option;
2028 initViewItemOption(&option);
2029 if (d->pressedAlreadySelected)
2030 option.state |= QStyle::State_Selected;
2031 if ((d->model->flags(index) & Qt::ItemIsEnabled)
2032 && style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option,
this))
2033 emit activated(index);
2038
2039
2040
2041
2042void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
2044 Q_D(QAbstractItemView);
2046 QModelIndex index = indexAt(event->position().toPoint());
2047 if (!index.isValid()
2048 || !d->isIndexEnabled(index)
2049 || (d->pressedIndex != index)) {
2050 QMouseEvent me(QEvent::MouseButtonPress,
2051 event->position(), event->scenePosition(), event->globalPosition(),
2052 event->button(), event->buttons(), event->modifiers(),
2053 event->source(), event->pointingDevice());
2054 mousePressEvent(&me);
2058 QPersistentModelIndex persistent = index;
2059 emit doubleClicked(persistent);
2060 if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
2061 && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick,
nullptr,
this))
2062 emit activated(persistent);
2063 d->releaseFromDoubleClick =
true;
2066#if QT_CONFIG(draganddrop)
2069
2070
2071
2072
2073
2074
2075void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
2077 if (dragDropMode() == InternalMove
2078 && (event->source() !=
this|| !(event->possibleActions() & Qt::MoveAction)))
2081 if (d_func()->canDrop(event)) {
2083 setState(DraggingState);
2090
2091
2092
2093
2094
2095
2096
2097void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
2099 Q_D(QAbstractItemView);
2100 d->draggedPosition = event->position().toPoint() + d->offset();
2101 if (dragDropMode() == InternalMove
2102 && (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction)))
2108 QModelIndex index = indexAt(event->position().toPoint());
2110 if (!d->droppingOnItself(event, index)
2111 && d->canDrop(event)) {
2113 if (index.isValid() && d->showDropIndicator) {
2114 QRect rect = visualRect(index);
2115 d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
2116 if (d->selectionBehavior == QAbstractItemView::SelectRows
2117 && d->dropIndicatorPosition != OnViewport
2118 && (d->dropIndicatorPosition != OnItem || event->source() ==
this)) {
2119 const int maxCol = d->model->columnCount(index.parent()) - 1;
2120 const auto idx = index.column() > 0 ? index.siblingAtColumn(0) : index;
2121 rect = d->intersectedRect(viewport()->rect(), idx, idx.siblingAtColumn(maxCol));
2123 switch (d->dropIndicatorPosition) {
2125 if (d->isIndexDropEnabled(index.parent())) {
2126 d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0);
2127 event->acceptProposedAction();
2129 d->dropIndicatorRect = QRect();
2133 if (d->isIndexDropEnabled(index.parent())) {
2134 d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0);
2135 event->acceptProposedAction();
2137 d->dropIndicatorRect = QRect();
2141 if (d->isIndexDropEnabled(index)) {
2142 d->dropIndicatorRect = rect;
2143 event->acceptProposedAction();
2145 d->dropIndicatorRect = QRect();
2149 d->dropIndicatorRect = QRect();
2150 if (d->isIndexDropEnabled(rootIndex())) {
2151 event->acceptProposedAction();
2156 d->dropIndicatorRect = QRect();
2157 d->dropIndicatorPosition = OnViewport;
2158 if (d->isIndexDropEnabled(rootIndex())) {
2159 event->acceptProposedAction();
2162 d->viewport->update();
2165 if (d->shouldAutoScroll(event->position().toPoint()))
2170
2171
2172
2173
2174bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event,
const QModelIndex &index)
2176 Q_Q(QAbstractItemView);
2177 Qt::DropAction dropAction = event->dropAction();
2178 if (q->dragDropMode() == QAbstractItemView::InternalMove)
2179 dropAction = Qt::MoveAction;
2180 if (event->source() == q
2181 && event->possibleActions() & Qt::MoveAction
2182 && dropAction == Qt::MoveAction) {
2183 QModelIndexList selectedIndexes = q->selectedIndexes();
2184 QModelIndex child = index;
2185 while (child.isValid() && child != root) {
2186 if (selectedIndexes.contains(child))
2188 child = child.parent();
2195
2196
2197
2198
2199
2200void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *)
2202 Q_D(QAbstractItemView);
2205 d->hover = QModelIndex();
2206 d->viewport->update();
2210
2211
2212
2213
2214
2215
2216void QAbstractItemView::dropEvent(QDropEvent *event)
2218 Q_D(QAbstractItemView);
2219 if (dragDropMode() == InternalMove) {
2220 if (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction))
2227 if (d->dropOn(event, &row, &col, &index)) {
2228 const Qt::DropAction action = dragDropMode() == InternalMove ? Qt::MoveAction : event->dropAction();
2229 if (d->model->dropMimeData(event->mimeData(), action, row, col, index)) {
2230 if (action != event->dropAction()) {
2231 event->setDropAction(action);
2234 event->acceptProposedAction();
2240 d->viewport->update();
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254bool QAbstractItemViewPrivate::dropOn(QDropEvent *event,
int *dropRow,
int *dropCol, QModelIndex *dropIndex)
2256 Q_Q(QAbstractItemView);
2257 if (event->isAccepted())
2262 if (viewport->rect().contains(event->position().toPoint())) {
2263 index = q->indexAt(event->position().toPoint());
2264 if (!index.isValid())
2269 if (model->supportedDropActions() & event->dropAction()) {
2272 if (index != root) {
2273 dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
2274 switch (dropIndicatorPosition) {
2275 case QAbstractItemView::AboveItem:
2277 col = index.column();
2278 index = index.parent();
2280 case QAbstractItemView::BelowItem:
2281 row = index.row() + 1;
2282 col = index.column();
2283 index = index.parent();
2285 case QAbstractItemView::OnItem:
2286 case QAbstractItemView::OnViewport:
2290 dropIndicatorPosition = QAbstractItemView::OnViewport;
2295 if (!droppingOnItself(event, index))
2301QAbstractItemView::DropIndicatorPosition
2302QAbstractItemViewPrivate::position(
const QPoint &pos,
const QRect &rect,
const QModelIndex &index)
const
2304 QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport;
2306 const int margin = qBound(2, qRound(qreal(rect.height()) / 5.5), 12);
2307 if (pos.y() - rect.top() < margin) {
2308 r = QAbstractItemView::AboveItem;
2309 }
else if (rect.bottom() - pos.y() < margin) {
2310 r = QAbstractItemView::BelowItem;
2311 }
else if (rect.contains(pos,
true)) {
2312 r = QAbstractItemView::OnItem;
2315 QRect touchingRect = rect;
2316 touchingRect.adjust(-1, -1, 1, 1);
2317 if (touchingRect.contains(pos,
false)) {
2318 r = QAbstractItemView::OnItem;
2322 if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled)))
2323 r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem;
2331
2332
2333
2334
2335
2336void QAbstractItemView::focusInEvent(QFocusEvent *event)
2338 Q_D(QAbstractItemView);
2339 QAbstractScrollArea::focusInEvent(event);
2341 const QItemSelectionModel* model = selectionModel();
2342 bool currentIndexValid = currentIndex().isValid();
2345 && !d->currentIndexSet
2346 && !currentIndexValid) {
2347 bool autoScroll = d->autoScroll;
2348 d->autoScroll =
false;
2349 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2350 if (index.isValid() && d->isIndexEnabled(index) && event->reason() != Qt::MouseFocusReason) {
2351 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
2352 currentIndexValid =
true;
2354 d->autoScroll = autoScroll;
2357 if (model && currentIndexValid)
2358 setAttribute(Qt::WA_InputMethodEnabled, (currentIndex().flags() & Qt::ItemIsEditable));
2359 else if (!currentIndexValid)
2360 setAttribute(Qt::WA_InputMethodEnabled,
false);
2362 d->viewport->update();
2366
2367
2368
2369
2370
2371void QAbstractItemView::focusOutEvent(QFocusEvent *event)
2373 Q_D(QAbstractItemView);
2374 QAbstractScrollArea::focusOutEvent(event);
2375 d->viewport->update();
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389void QAbstractItemView::keyPressEvent(QKeyEvent *event)
2391 Q_D(QAbstractItemView);
2392 d->delayedAutoScroll.stop();
2394#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
2395 if (event == QKeySequence::Copy) {
2396 const QModelIndex index = currentIndex();
2397 if (index.isValid() && d->model) {
2398 const QVariant variant = d->model->data(index, Qt::DisplayRole);
2399 if (variant.canConvert<QString>())
2400 QGuiApplication::clipboard()->setText(variant.toString());
2406 QPersistentModelIndex newCurrent;
2407 d->moveCursorUpdatedView =
false;
2408 switch (event->key()) {
2410 newCurrent = moveCursor(MoveDown, event->modifiers());
2413 newCurrent = moveCursor(MoveUp, event->modifiers());
2416 newCurrent = moveCursor(MoveLeft, event->modifiers());
2419 newCurrent = moveCursor(MoveRight, event->modifiers());
2422 newCurrent = moveCursor(MoveHome, event->modifiers());
2425 newCurrent = moveCursor(MoveEnd, event->modifiers());
2427 case Qt::Key_PageUp:
2428 newCurrent = moveCursor(MovePageUp, event->modifiers());
2430 case Qt::Key_PageDown:
2431 newCurrent = moveCursor(MovePageDown, event->modifiers());
2434 if (d->tabKeyNavigation)
2435 newCurrent = moveCursor(MoveNext, event->modifiers());
2437 case Qt::Key_Backtab:
2438 if (d->tabKeyNavigation)
2439 newCurrent = moveCursor(MovePrevious, event->modifiers());
2443 QPersistentModelIndex oldCurrent = currentIndex();
2444 if (newCurrent != oldCurrent && newCurrent.isValid() && d->isIndexEnabled(newCurrent)) {
2445 if (!hasFocus() && QApplication::focusWidget() == indexWidget(oldCurrent))
2447 QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
2448 if (command != QItemSelectionModel::NoUpdate
2449 || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection,
nullptr,
this)) {
2451 if (command & QItemSelectionModel::Current) {
2452 d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
2453 if (!d->currentSelectionStartIndex.isValid())
2454 d->currentSelectionStartIndex = oldCurrent;
2455 QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center());
2456 setSelection(rect, command);
2458 d->selectionModel->setCurrentIndex(newCurrent, command);
2459 d->currentSelectionStartIndex = newCurrent;
2460 if (newCurrent.isValid()) {
2462 QRect rect(visualRect(newCurrent).center(), QSize(1, 1));
2463 setSelection(rect, command);
2471 switch (event->key()) {
2479 case Qt::Key_PageUp:
2480 case Qt::Key_PageDown:
2481 case Qt::Key_Escape:
2483 case Qt::Key_Control:
2484 case Qt::Key_Delete:
2485 case Qt::Key_Backspace:
2489 case Qt::Key_Select:
2490 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2491 if (d->selectionModel)
2492 d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
2493 if (event->key() == Qt::Key_Space) {
2494 keyboardSearch(event->text());
2501 case Qt::Key_Return:
2504 if (!edit(currentIndex(), EditKeyPressed, event) && d->editorIndexHash.isEmpty())
2509 if (!edit(currentIndex(), EditKeyPressed, event))
2513 case Qt::Key_Return:
2517 if (state() != EditingState || hasFocus()) {
2518 if (currentIndex().isValid())
2519 emit activated(currentIndex());
2525#ifndef QT_NO_SHORTCUT
2526 if (event == QKeySequence::SelectAll && selectionMode() != NoSelection) {
2532 if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
2533 emit activated(currentIndex());
2537 bool modified = (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
2538 if (!event->text().isEmpty() && !modified && !edit(currentIndex(), AnyKeyPressed, event)) {
2539 keyboardSearch(event->text());
2546 if (d->moveCursorUpdatedView)
2551
2552
2553
2554
2555
2556void QAbstractItemView::resizeEvent(QResizeEvent *event)
2558 QAbstractScrollArea::resizeEvent(event);
2563
2564
2565
2566
2567
2568void QAbstractItemView::timerEvent(QTimerEvent *event)
2570 Q_D(QAbstractItemView);
2571 if (event->timerId() == d->fetchMoreTimer.timerId())
2573 else if (event->timerId() == d->delayedReset.timerId())
2575 else if (event->timerId() == d->autoScrollTimer.timerId())
2577 else if (event->timerId() == d->updateTimer.timerId())
2578 d->updateDirtyRegion();
2579 else if (event->timerId() == d->delayedEditing.timerId()) {
2580 d->delayedEditing.stop();
2581 edit(currentIndex());
2582 }
else if (event->timerId() == d->delayedLayout.timerId()) {
2583 d->delayedLayout.stop();
2585 d->interruptDelayedItemsLayout();
2587 const QModelIndex current = currentIndex();
2588 if (current.isValid() && d->state == QAbstractItemView::EditingState)
2591 }
else if (event->timerId() == d->delayedAutoScroll.timerId()) {
2592 d->delayedAutoScroll.stop();
2595 if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
2596 scrollTo(d->pressedIndex);
2597 }
else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
2598 d->pressClosedEditorWatcher.stop();
2603
2604
2605void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
2607 Q_D(QAbstractItemView);
2614 bool forwardEventToEditor =
false;
2615 const bool commit = !event->commitString().isEmpty();
2616 const bool preediting = !event->preeditString().isEmpty();
2617 if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
2618 if (d->waitForIMCommit) {
2619 if (commit || !preediting) {
2621 d->waitForIMCommit =
false;
2622 QApplication::sendEvent(currentEditor, event);
2624 QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
2626 delegate->setEditorData(currentEditor, currentIndex());
2627 d->selectAllInEditor(currentEditor);
2629 if (currentEditor->focusPolicy() != Qt::NoFocus)
2630 currentEditor->setFocus();
2633 QApplication::sendEvent(currentEditor, event);
2637 }
else if (preediting) {
2639 d->waitForIMCommit =
true;
2641 forwardEventToEditor =
true;
2642 }
else if (!commit) {
2646 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2647 d->waitForIMCommit =
false;
2649 keyboardSearch(event->commitString());
2651 }
else if (QWidget *currentEditor; forwardEventToEditor
2652 && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
2653 QApplication::sendEvent(currentEditor, event);
2657#if QT_CONFIG(draganddrop)
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2677
2678
2679QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition()
const
2681 Q_D(
const QAbstractItemView);
2682 return d->dropIndicatorPosition;
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699QModelIndexList QAbstractItemView::selectedIndexes()
const
2701 Q_D(
const QAbstractItemView);
2702 QModelIndexList indexes;
2703 if (d->selectionModel) {
2704 indexes = d->selectionModel->selectedIndexes();
2705 auto isHidden = [
this](
const QModelIndex &idx) {
2706 return isIndexHidden(idx);
2708 indexes.removeIf(isHidden);
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726bool QAbstractItemView::edit(
const QModelIndex &index, EditTrigger trigger, QEvent *event)
2728 Q_D(QAbstractItemView);
2730 if (!d->isIndexValid(index))
2733 if (QWidget *w = (d->persistent.isEmpty() ?
static_cast<QWidget*>(
nullptr) : d->editorForIndex(index).widget.data())) {
2734 if (w->focusPolicy() == Qt::NoFocus)
2736 if (!d->waitForIMCommit)
2743 if (trigger == DoubleClicked) {
2744 d->delayedEditing.stop();
2745 d->delayedAutoScroll.stop();
2746 }
else if (trigger == CurrentChanged) {
2747 d->delayedEditing.stop();
2751 QPersistentModelIndex safeIndex(index);
2753 if (d->sendDelegateEvent(index, event)) {
2758 if (!safeIndex.isValid()) {
2763 EditTriggers lastTrigger = d->lastTrigger;
2764 d->lastTrigger = trigger;
2766 if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
2769 if (d->delayedEditing.isActive())
2774 if (lastTrigger == DoubleClicked && trigger == SelectedClicked)
2778 if (trigger == SelectedClicked)
2779 d->delayedEditing.start(QApplication::doubleClickInterval(),
this);
2781 d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event :
nullptr);
2787
2788
2789
2790void QAbstractItemView::updateEditorData()
2792 Q_D(QAbstractItemView);
2793 d->updateEditorData(QModelIndex(), QModelIndex());
2797
2798
2799
2800void QAbstractItemView::updateEditorGeometries()
2802 Q_D(QAbstractItemView);
2803 if (d->editorIndexHash.isEmpty())
2805 if (d->delayedPendingLayout) {
2807 d->executePostedLayout();
2810 QStyleOptionViewItem option;
2811 initViewItemOption(&option);
2812 QEditorIndexHash::iterator it = d->editorIndexHash.begin();
2813 QWidgetList editorsToRelease;
2814 QWidgetList editorsToHide;
2815 while (it != d->editorIndexHash.end()) {
2816 QModelIndex index = it.value();
2817 QWidget *editor = it.key();
2818 if (index.isValid() && editor) {
2819 option.rect = visualRect(index);
2820 if (option.rect.isValid()) {
2822 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2824 delegate->updateEditorGeometry(editor, option, index);
2826 editorsToHide << editor;
2830 d->indexEditorHash.remove(it.value());
2831 it = d->editorIndexHash.erase(it);
2832 editorsToRelease << editor;
2838 for (QWidget *editor : std::as_const(editorsToHide))
2840 for (QWidget *editor : std::as_const(editorsToRelease))
2841 d->releaseEditor(editor);
2845
2846
2847void QAbstractItemView::updateGeometries()
2849 Q_D(QAbstractItemView);
2850 updateEditorGeometries();
2851 d->fetchMoreTimer.start(0,
this);
2852 d->updateGeometry();
2856
2857
2858void QAbstractItemView::verticalScrollbarValueChanged(
int value)
2860 Q_D(QAbstractItemView);
2861 if (verticalScrollBar()->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::horizontalScrollbarValueChanged(
int value)
2873 Q_D(QAbstractItemView);
2874 if (horizontalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2875 d->model->fetchMore(d->root);
2876 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2877 if (viewport()->rect().contains(posInVp))
2878 d->checkMouseMove(posInVp);
2882
2883
2884void QAbstractItemView::verticalScrollbarAction(
int)
2890
2891
2892void QAbstractItemView::horizontalScrollbarAction(
int)
2898
2899
2900
2901
2902
2903
2904
2906void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
2908 Q_D(QAbstractItemView);
2912 const bool isPersistent = d->persistent.contains(editor);
2913 const QModelIndex index = d->indexForEditor(editor);
2914 if (!index.isValid()) {
2915 if (!editor->isVisible()) {
2924 "QAbstractItemView::closeEditor called with an editor that does not "
2925 "belong to this view");
2929 const bool hadFocus = editor->hasFocus();
2933 d->pressClosedEditorWatcher.start(0,
this);
2934 d->lastEditedIndex = index;
2936 if (!isPersistent) {
2938 QModelIndex index = d->indexForEditor(editor);
2939 editor->removeEventFilter(itemDelegateForIndex(index));
2940 d->removeEditor(editor);
2943 if (focusPolicy() != Qt::NoFocus)
2946 editor->clearFocus();
2948 d->checkPersistentEditorFocus();
2951 QPointer<QWidget> ed = editor;
2952 QCoreApplication::sendPostedEvents(editor, 0);
2955 if (!isPersistent && editor)
2956 d->releaseEditor(editor, index);
2961 QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::NoUpdate;
2962 if (d->selectionMode != NoSelection)
2963 flags = QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
2965 case QAbstractItemDelegate::EditNextItem: {
2966 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2967 if (index.isValid()) {
2968 QPersistentModelIndex persistent(index);
2969 d->selectionModel->setCurrentIndex(persistent, flags);
2971 if (index.flags() & Qt::ItemIsEditable
2972 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2975 case QAbstractItemDelegate::EditPreviousItem: {
2976 QModelIndex index = moveCursor(MovePrevious, Qt::NoModifier);
2977 if (index.isValid()) {
2978 QPersistentModelIndex persistent(index);
2979 d->selectionModel->setCurrentIndex(persistent, flags);
2981 if (index.flags() & Qt::ItemIsEditable
2982 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
2985 case QAbstractItemDelegate::SubmitModelCache:
2988 case QAbstractItemDelegate::RevertModelCache:
2997
2998
2999
3000
3001void QAbstractItemView::commitData(QWidget *editor)
3003 Q_D(QAbstractItemView);
3004 if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
3006 QModelIndex index = d->indexForEditor(editor);
3007 if (!index.isValid()) {
3009 "QAbstractItemView::commitData called with an editor that does not belong to "
3013 d->currentlyCommittingEditor = editor;
3014 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
3015 editor->removeEventFilter(delegate);
3016 delegate->setModelData(editor, d->model, index);
3017 editor->installEventFilter(delegate);
3018 d->currentlyCommittingEditor =
nullptr;
3022
3023
3024
3025
3026void QAbstractItemView::editorDestroyed(QObject *editor)
3028 Q_D(QAbstractItemView);
3029 QWidget *w = qobject_cast<QWidget*>(editor);
3031 d->persistent.remove(w);
3032 if (state() == EditingState)
3039
3040
3041
3042
3043
3044
3045
3046void QAbstractItemView::keyboardSearch(
const QString &search)
3048 Q_D(QAbstractItemView);
3049 if (!d->model->rowCount(d->root) || !d->model->columnCount(d->root))
3052 QModelIndex start = currentIndex().isValid() ? currentIndex()
3053 : d->model->index(0, 0, d->root);
3054 bool skipRow =
false;
3055 bool keyboardTimeWasValid = d->keyboardInputTime.isValid();
3056 qint64 keyboardInputTimeElapsed;
3057 if (keyboardTimeWasValid)
3058 keyboardInputTimeElapsed = d->keyboardInputTime.restart();
3060 d->keyboardInputTime.start();
3061 if (search.isEmpty() || !keyboardTimeWasValid
3062 || keyboardInputTimeElapsed > QApplication::keyboardInputInterval()) {
3063 d->keyboardInput = search;
3064 skipRow = currentIndex().isValid();
3066 d->keyboardInput += search;
3070 bool sameKey =
false;
3071 if (d->keyboardInput.size() > 1) {
3072 int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
3073 sameKey = (c == d->keyboardInput.size());
3080 QModelIndex parent = start.parent();
3081 int newRow = (start.row() < d->model->rowCount(parent) - 1) ? start.row() + 1 : 0;
3082 start = d->model->index(newRow, start.column(), parent);
3086 QModelIndex current = start;
3087 QModelIndexList match;
3088 QModelIndex firstMatch;
3089 QModelIndex startMatch;
3090 QModelIndexList previous;
3092 match = d->model->match(current, Qt::DisplayRole, d->keyboardInput, 1,
3093 d->keyboardSearchFlags);
3094 if (match == previous)
3096 firstMatch = match.value(0);
3098 if (firstMatch.isValid()) {
3099 if (d->isIndexEnabled(firstMatch)) {
3100 setCurrentIndex(firstMatch);
3103 int row = firstMatch.row() + 1;
3104 if (row >= d->model->rowCount(firstMatch.parent()))
3106 current = firstMatch.sibling(row, firstMatch.column());
3109 if (!startMatch.isValid())
3110 startMatch = firstMatch;
3111 else if (startMatch == firstMatch)
3114 }
while (current != start && firstMatch.isValid());
3118
3119
3120
3121
3122
3123QSize QAbstractItemView::sizeHintForIndex(
const QModelIndex &index)
const
3125 Q_D(
const QAbstractItemView);
3126 if (!d->isIndexValid(index))
3128 const auto delegate = itemDelegateForIndex(index);
3129 QStyleOptionViewItem option;
3130 initViewItemOption(&option);
3131 return delegate ? delegate->sizeHint(option, index) : QSize();
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150int QAbstractItemView::sizeHintForRow(
int row)
const
3152 Q_D(
const QAbstractItemView);
3154 if (row < 0 || row >= d->model->rowCount(d->root))
3159 QStyleOptionViewItem option;
3160 initViewItemOption(&option);
3162 int colCount = d->model->columnCount(d->root);
3163 for (
int c = 0; c < colCount; ++c) {
3164 const QModelIndex index = d->model->index(row, c, d->root);
3165 if (QWidget *editor = d->editorForIndex(index).widget.data())
3166 height = qMax(height, editor->height());
3167 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3168 height = qMax(height, delegate->sizeHint(option, index).height());
3174
3175
3176
3177
3178
3179
3180
3181int QAbstractItemView::sizeHintForColumn(
int column)
const
3183 Q_D(
const QAbstractItemView);
3185 if (column < 0 || column >= d->model->columnCount(d->root))
3190 QStyleOptionViewItem option;
3191 initViewItemOption(&option);
3193 int rows = d->model->rowCount(d->root);
3194 for (
int r = 0; r < rows; ++r) {
3195 const QModelIndex index = d->model->index(r, column, d->root);
3196 if (QWidget *editor = d->editorForIndex(index).widget.data())
3197 width = qMax(width, editor->sizeHint().width());
3198 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3199 width = qMax(width, delegate->sizeHint(option, index).width());
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221int QAbstractItemView::updateThreshold()
const
3223 Q_D(
const QAbstractItemView);
3224 return d->updateThreshold;
3227void QAbstractItemView::setUpdateThreshold(
int threshold)
3229 Q_D(QAbstractItemView);
3230 if (d->updateThreshold == threshold)
3232 d->updateThreshold = threshold;
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3247Qt::MatchFlags QAbstractItemView::keyboardSearchFlags()
const
3249 Q_D(
const QAbstractItemView);
3250 return d->keyboardSearchFlags;
3253void QAbstractItemView::setKeyboardSearchFlags(Qt::MatchFlags searchFlags)
3255 Q_D(QAbstractItemView);
3256 d->keyboardSearchFlags = searchFlags;
3260
3261
3262
3263
3264
3265void QAbstractItemView::openPersistentEditor(
const QModelIndex &index)
3267 Q_D(QAbstractItemView);
3268 QStyleOptionViewItem options;
3269 initViewItemOption(&options);
3270 options.rect = visualRect(index);
3271 options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
3273 QWidget *editor = d->editor(index, options);
3276 d->persistent.insert(editor);
3281
3282
3283
3284
3285void QAbstractItemView::closePersistentEditor(
const QModelIndex &index)
3287 Q_D(QAbstractItemView);
3288 if (QWidget *editor = d->editorForIndex(index).widget.data()) {
3289 if (index == selectionModel()->currentIndex())
3290 closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
3291 d->persistent.remove(editor);
3292 d->removeEditor(editor);
3293 d->releaseEditor(editor, index);
3298
3299
3300
3301
3302
3303
3304bool QAbstractItemView::isPersistentEditorOpen(
const QModelIndex &index)
const
3306 Q_D(
const QAbstractItemView);
3307 QWidget *editor = d->editorForIndex(index).widget;
3308 return editor && d->persistent.contains(editor);
4077void QAbstractItemView::doAutoScroll()
4080 Q_D(QAbstractItemView);
4081 QScrollBar *verticalScroll = verticalScrollBar();
4082 QScrollBar *horizontalScroll = horizontalScrollBar();
4086 QHeaderView *hv = qobject_cast<QHeaderView*>(
this);
4088 QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
4090 if (hv->orientation() == Qt::Horizontal) {
4091 if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
4092 horizontalScroll = parent->horizontalScrollBar();
4094 if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
4095 verticalScroll = parent->verticalScrollBar();
4100 const int verticalStep = verticalScroll->pageStep();
4101 const int horizontalStep = horizontalScroll->pageStep();
4102 if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
4103 ++d->autoScrollCount;
4105 const int margin = d->autoScrollMargin;
4106 const int verticalValue = verticalScroll->value();
4107 const int horizontalValue = horizontalScroll->value();
4109 const QPoint pos = d->draggedPosition - d->offset();
4110 const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
4113 if (pos.y() - area.top() < margin)
4114 verticalScroll->setValue(verticalValue - d->autoScrollCount);
4115 else if (area.bottom() - pos.y() < margin)
4116 verticalScroll->setValue(verticalValue + d->autoScrollCount);
4117 if (pos.x() - area.left() < margin)
4118 horizontalScroll->setValue(horizontalValue - d->autoScrollCount);
4119 else if (area.right() - pos.x() < margin)
4120 horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
4122 const bool verticalUnchanged = (verticalValue == verticalScroll->value());
4123 const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
4124 if (verticalUnchanged && horizontalUnchanged) {
4127#if QT_CONFIG(draganddrop)
4128 d->dropIndicatorRect = QRect();
4129 d->dropIndicatorPosition = QAbstractItemView::OnViewport;
4132 case QAbstractItemView::DragSelectingState: {
4135 const QPoint globalPos = d->viewport->mapToGlobal(pos);
4136 const QPoint windowPos = window()->mapFromGlobal(globalPos);
4137 QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
4138 Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
4139 Qt::MouseEventSynthesizedByQt);
4140 QApplication::sendEvent(viewport(), &mm);
4143 case QAbstractItemView::DraggingState: {
4149 d->draggedPosition = pos + d->offset();
4155 d->viewport->update();
4259QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
4260 const QModelIndex &index,
const QEvent *event)
const
4262 Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
4263 ?
static_cast<
const QInputEvent*>(event)->modifiers()
4264 : QGuiApplication::keyboardModifiers();
4266 switch (event->type()) {
4267 case QEvent::MouseMove: {
4269 if (modifiers & Qt::ControlModifier)
4270 return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
4273 case QEvent::MouseButtonPress: {
4274 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4275 const bool rightButtonPressed = button & Qt::RightButton;
4276 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4277 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4278 const bool indexIsSelected = selectionModel->isSelected(index);
4279 if ((shiftKeyPressed || controlKeyPressed) && rightButtonPressed)
4280 return QItemSelectionModel::NoUpdate;
4281 if (!shiftKeyPressed && !controlKeyPressed && indexIsSelected)
4282 return QItemSelectionModel::NoUpdate;
4283 if (!index.isValid() && !rightButtonPressed && !shiftKeyPressed && !controlKeyPressed)
4284 return QItemSelectionModel::Clear;
4285 if (!index.isValid())
4286 return QItemSelectionModel::NoUpdate;
4288 if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
4289#if QT_CONFIG(draganddrop)
4290 && dragEnabled && isIndexDragEnabled(index)
4293 return QItemSelectionModel::NoUpdate;
4297 case QEvent::MouseButtonRelease: {
4299 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4300 const bool rightButtonPressed = button & Qt::RightButton;
4301 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4302 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4303 if (((index == pressedIndex && selectionModel->isSelected(index))
4304 || !index.isValid()) && state != QAbstractItemView::DragSelectingState
4305 && !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
4306 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
4307 if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
4308#if QT_CONFIG(draganddrop)
4309 && dragEnabled && isIndexDragEnabled(index)
4314 return QItemSelectionModel::NoUpdate;
4316 case QEvent::KeyPress: {
4318 switch (
static_cast<
const QKeyEvent*>(event)->key()) {
4319 case Qt::Key_Backtab:
4320 modifiers = modifiers & ~Qt::ShiftModifier;
4328 case Qt::Key_PageUp:
4329 case Qt::Key_PageDown:
4331 if (modifiers & Qt::ControlModifier)
4332 return QItemSelectionModel::NoUpdate;
4334 case Qt::Key_Select:
4335 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4337 if (modifiers & Qt::ControlModifier)
4338 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4339 return QItemSelectionModel::Select|selectionBehaviorFlags();
4350 if (modifiers & Qt::ShiftModifier)
4351 return QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4352 if (modifiers & Qt::ControlModifier)
4353 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4354 if (state == QAbstractItemView::DragSelectingState) {
4356 return QItemSelectionModel::Clear|QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4359 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();