190 for (
int role : roles) {
191 if (role == Qt::AccessibleTextRole
192 || (role == Qt::DisplayRole
193 && index.data(Qt::AccessibleTextRole).toString().isEmpty())) {
194 QAccessibleEvent event(q, QAccessible::NameChanged);
195 event.setChild(childIndex);
196 QAccessible::updateAccessibility(&event);
197 }
else if (role == Qt::AccessibleDescriptionRole) {
198 QAccessibleEvent event(q, QAccessible::DescriptionChanged);
199 event.setChild(childIndex);
200 QAccessible::updateAccessibility(&event);
201 }
else if (role == Qt::CheckStateRole) {
202 QAccessible::State state;
203 state.checked =
true;
204 QAccessibleStateChangeEvent event(q, state);
205 event.setChild(childIndex);
206 QAccessible::updateAccessibility(&event);
212#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
215void QAbstractItemViewPrivate::scrollerStateChanged()
217 Q_Q(QAbstractItemView);
219 if (QScroller *scroller = QScroller::scroller(viewport)) {
220 switch (scroller->state()) {
221 case QScroller::Pressed:
223 if (q->selectionModel()) {
224 oldSelection = q->selectionModel()->selection();
225 oldCurrent = q->selectionModel()->currentIndex();
229 case QScroller::Dragging:
231 if (q->selectionModel()) {
232 q->selectionModel()->select(oldSelection, QItemSelectionModel::ClearAndSelect);
234 const bool wasAutoScroll = autoScroll;
236 q->selectionModel()->setCurrentIndex(oldCurrent, QItemSelectionModel::NoUpdate);
237 autoScroll = wasAutoScroll;
242 oldSelection = QItemSelection();
243 oldCurrent = QModelIndex();
251void QAbstractItemViewPrivate::delegateSizeHintChanged(
const QModelIndex &index)
253 Q_Q(QAbstractItemView);
255 if (!model->checkIndex(index))
256 qWarning(
"Delegate size hint changed for a model index that does not belong to this view");
258 QMetaObject::invokeMethod(q, &QAbstractItemView::doItemsLayout, Qt::QueuedConnection);
261void QAbstractItemViewPrivate::connectDelegate(QAbstractItemDelegate *delegate)
265 Q_Q(QAbstractItemView);
266 QObject::connect(delegate, &QAbstractItemDelegate::closeEditor,
267 q, &QAbstractItemView::closeEditor);
268 QObject::connect(delegate, &QAbstractItemDelegate::commitData,
269 q, &QAbstractItemView::commitData);
270 QObjectPrivate::connect(delegate, &QAbstractItemDelegate::sizeHintChanged,
271 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
274void QAbstractItemViewPrivate::disconnectDelegate(QAbstractItemDelegate *delegate)
278 Q_Q(QAbstractItemView);
279 QObject::disconnect(delegate, &QAbstractItemDelegate::closeEditor,
280 q, &QAbstractItemView::closeEditor);
281 QObject::disconnect(delegate, &QAbstractItemDelegate::commitData,
282 q, &QAbstractItemView::commitData);
283 QObjectPrivate::disconnect(delegate, &QAbstractItemDelegate::sizeHintChanged,
284 this, &QAbstractItemViewPrivate::delegateSizeHintChanged);
287void QAbstractItemViewPrivate::disconnectAll()
289 Q_Q(QAbstractItemView);
290 for (QMetaObject::Connection &connection : modelConnections)
291 QObject::disconnect(connection);
292 for (QMetaObject::Connection &connection : scrollbarConnections)
293 QObject::disconnect(connection);
294 disconnectDelegate(itemDelegate);
295 for (QAbstractItemDelegate *delegate : std::as_const(rowDelegates))
296 disconnectDelegate(delegate);
297 for (QAbstractItemDelegate *delegate : std::as_const(columnDelegates))
298 disconnectDelegate(delegate);
299 if (model && selectionModel) {
300 QObject::disconnect(model, &QAbstractItemModel::destroyed,
301 selectionModel, &QItemSelectionModel::deleteLater);
303 if (selectionModel) {
304 QObject::disconnect(selectionModel, &QItemSelectionModel::selectionChanged,
305 q, &QAbstractItemView::selectionChanged);
306 QObject::disconnect(selectionModel, &QItemSelectionModel::currentChanged,
307 q, &QAbstractItemView::currentChanged);
309 for (
const auto &info : std::as_const(indexEditorHash)) {
310 if (!info.isStatic && info.widget)
311 QObject::disconnect(info.widget, &QWidget::destroyed, q, &QAbstractItemView::editorDestroyed);
313#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
314 QObject::disconnect(scollerConnection);
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
455
456
457
458
459
460
463
464
465
466
467
468
469
470
471
472
476
477
478
479
480
481
482
483
484
485
486
487
488
489
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
510
511
512
513
514
515
516
517
518
519
520
521
522
525
526
527
528
529
530
531
532
533
534
537
538
539
540
541
542
543
544
545
546
547
550
551
552
553
554
555
556
559
560
561
562
563
564
565
566
569
570
571
572
573
574
575
576
577
580
581
582
583
584
585
586
587
590
591
592
593
594
595
596
599
600
601
602
603
604
605
606
607
608
609
612
613
614
615
616
617
618
619
622
623
624
625
626
627
628
629
632
633
634
635
636
637
638
639
642
643
644
645
646
647
648
649
652
653
654
655
656
657
658
659
662
663
664
665
666
667
668
669
670
671
674
675
676
677
678
679
680
681
682
683
684
685
688
689
690
691
692
693
694
695
696
699
700
701QAbstractItemView::QAbstractItemView(QWidget *parent)
702 : QAbstractScrollArea(*(
new QAbstractItemViewPrivate), parent)
708
709
710QAbstractItemView::QAbstractItemView(QAbstractItemViewPrivate &dd, QWidget *parent)
711 : QAbstractScrollArea(dd, parent)
717
718
719QAbstractItemView::~QAbstractItemView()
721 Q_D(QAbstractItemView);
723 d->delayedReset.stop();
724 d->updateTimer.stop();
725 d->delayedEditing.stop();
726 d->delayedAutoScroll.stop();
727 d->autoScrollTimer.stop();
728 d->delayedLayout.stop();
729 d->fetchMoreTimer.stop();
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753void QAbstractItemView::setModel(QAbstractItemModel *model)
755 Q_D(QAbstractItemView);
756 if (model == d->model)
758 if (d->model && d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
759 for (QMetaObject::Connection &connection : d->modelConnections)
760 disconnect(connection);
762 d->model = (model ? model : QAbstractItemModelPrivate::staticEmptyModel());
764 if (d->model != QAbstractItemModelPrivate::staticEmptyModel()) {
765 d->modelConnections = {
766 QObjectPrivate::connect(d->model, &QAbstractItemModel::destroyed,
767 d, &QAbstractItemViewPrivate::modelDestroyed),
768 QObject::connect(d->model, &QAbstractItemModel::dataChanged,
769 this, &QAbstractItemView::dataChanged),
770 QObjectPrivate::connect(d->model, &QAbstractItemModel::headerDataChanged,
771 d, &QAbstractItemViewPrivate::headerDataChanged),
772 QObject::connect(d->model, &QAbstractItemModel::rowsInserted,
773 this, &QAbstractItemView::rowsInserted),
774 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsInserted,
775 d, &QAbstractItemViewPrivate::rowsInserted),
776 QObject::connect(d->model, &QAbstractItemModel::rowsAboutToBeRemoved,
777 this, &QAbstractItemView::rowsAboutToBeRemoved),
778 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsRemoved,
779 d, &QAbstractItemViewPrivate::rowsRemoved),
780 QObjectPrivate::connect(d->model, &QAbstractItemModel::rowsMoved,
781 d, &QAbstractItemViewPrivate::rowsMoved),
782 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsAboutToBeRemoved,
783 d, &QAbstractItemViewPrivate::columnsAboutToBeRemoved),
784 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsRemoved,
785 d, &QAbstractItemViewPrivate::columnsRemoved),
786 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsInserted,
787 d, &QAbstractItemViewPrivate::columnsInserted),
788 QObjectPrivate::connect(d->model, &QAbstractItemModel::columnsMoved,
789 d, &QAbstractItemViewPrivate::columnsMoved),
790 QObject::connect(d->model, &QAbstractItemModel::modelReset,
791 this, &QAbstractItemView::reset),
792 QObjectPrivate::connect(d->model, &QAbstractItemModel::layoutChanged,
793 d, &QAbstractItemViewPrivate::layoutChanged),
797 QItemSelectionModel *selection_model =
new QItemSelectionModel(d->model,
this);
798 connect(d->model, &QAbstractItemModel::destroyed,
799 selection_model, &QItemSelectionModel::deleteLater);
800 setSelectionModel(selection_model);
806
807
808QAbstractItemModel *QAbstractItemView::model()
const
810 Q_D(
const QAbstractItemView);
811 return (d->model == QAbstractItemModelPrivate::staticEmptyModel() ?
nullptr : d->model);
815
816
817
818
819
820
821
822
823
824
825
826
827
828void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
831 Q_ASSERT(selectionModel);
832 Q_D(QAbstractItemView);
834 if (Q_UNLIKELY(selectionModel->model() != d->model)) {
835 qWarning(
"QAbstractItemView::setSelectionModel() failed: "
836 "Trying to set a selection model, which works on "
837 "a different model than the view.");
841 QItemSelection oldSelection;
842 QModelIndex oldCurrentIndex;
844 if (d->selectionModel) {
845 if (d->selectionModel->model() == selectionModel->model()) {
846 oldSelection = d->selectionModel->selection();
847 oldCurrentIndex = d->selectionModel->currentIndex();
849 disconnect(d->selectionModel, &QItemSelectionModel::selectionChanged,
850 this, &QAbstractItemView::selectionChanged);
851 disconnect(d->selectionModel, &QItemSelectionModel::currentChanged,
852 this, &QAbstractItemView::currentChanged);
855 d->selectionModel = selectionModel;
857 if (d->selectionModel) {
858 connect(d->selectionModel, &QItemSelectionModel::selectionChanged,
859 this, &QAbstractItemView::selectionChanged);
860 connect(d->selectionModel, &QItemSelectionModel::currentChanged,
861 this, &QAbstractItemView::currentChanged);
863 selectionChanged(d->selectionModel->selection(), oldSelection);
864 currentChanged(d->selectionModel->currentIndex(), oldCurrentIndex);
869
870
871
872
873QItemSelectionModel* QAbstractItemView::selectionModel()
const
875 Q_D(
const QAbstractItemView);
876 return d->selectionModel;
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894void QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate)
896 Q_D(QAbstractItemView);
897 if (delegate == d->itemDelegate)
900 if (d->itemDelegate) {
901 if (d->delegateRefCount(d->itemDelegate) == 1)
902 d->disconnectDelegate(d->itemDelegate);
906 if (d->delegateRefCount(delegate) == 0)
907 d->connectDelegate(delegate);
909 d->itemDelegate = delegate;
910 viewport()->update();
911 d->doDelayedItemsLayout();
915
916
917
918
919
920QAbstractItemDelegate *QAbstractItemView::itemDelegate()
const
922 return d_func()->itemDelegate;
926
927
928QVariant QAbstractItemView::inputMethodQuery(Qt::InputMethodQuery query)
const
930 Q_D(
const QAbstractItemView);
931 const QModelIndex current = currentIndex();
933 if (current.isValid()) {
934 if (QWidget *currentEditor;
935 d->waitForIMCommit && (currentEditor = d->editorForIndex(current).widget)) {
938 result = currentEditor->inputMethodQuery(query);
939 if (result.typeId() == QMetaType::QRect) {
940 const QRect editorRect = result.value<QRect>();
941 result = QRect(currentEditor->mapTo(
this, editorRect.topLeft()), editorRect.size());
943 }
else if (query == Qt::ImCursorRectangle) {
944 const QRect visRect = visualRect(current);
945 result = QRect(d->viewport->mapTo(
this, visRect.topLeft()), visRect.size());
948 if (!result.isValid())
949 result = QAbstractScrollArea::inputMethodQuery(query);
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972void QAbstractItemView::setItemDelegateForRow(
int row, QAbstractItemDelegate *delegate)
974 Q_D(QAbstractItemView);
975 if (QAbstractItemDelegate *rowDelegate = d->rowDelegates.value(row,
nullptr)) {
976 if (d->delegateRefCount(rowDelegate) == 1)
977 d->disconnectDelegate(rowDelegate);
978 d->rowDelegates.remove(row);
981 if (d->delegateRefCount(delegate) == 0)
982 d->connectDelegate(delegate);
983 d->rowDelegates.insert(row, delegate);
985 viewport()->update();
986 d->doDelayedItemsLayout();
990
991
992
993
994
995
996QAbstractItemDelegate *QAbstractItemView::itemDelegateForRow(
int row)
const
998 Q_D(
const QAbstractItemView);
999 return d->rowDelegates.value(row,
nullptr);
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020void QAbstractItemView::setItemDelegateForColumn(
int column, QAbstractItemDelegate *delegate)
1022 Q_D(QAbstractItemView);
1023 if (QAbstractItemDelegate *columnDelegate = d->columnDelegates.value(column,
nullptr)) {
1024 if (d->delegateRefCount(columnDelegate) == 1)
1025 d->disconnectDelegate(columnDelegate);
1026 d->columnDelegates.remove(column);
1029 if (d->delegateRefCount(delegate) == 0)
1030 d->connectDelegate(delegate);
1031 d->columnDelegates.insert(column, delegate);
1033 viewport()->update();
1034 d->doDelayedItemsLayout();
1038
1039
1040
1041
1042
1043
1044QAbstractItemDelegate *QAbstractItemView::itemDelegateForColumn(
int column)
const
1046 Q_D(
const QAbstractItemView);
1047 return d->columnDelegates.value(column,
nullptr);
1051
1052
1053
1054
1055
1058
1059
1060
1061
1062
1063
1064
1065QAbstractItemDelegate *QAbstractItemView::itemDelegateForIndex(
const QModelIndex &index)
const
1067 Q_D(
const QAbstractItemView);
1068 return d->delegateForIndex(index);
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081void QAbstractItemView::setSelectionMode(SelectionMode mode)
1083 Q_D(QAbstractItemView);
1084 d->selectionMode = mode;
1087QAbstractItemView::SelectionMode QAbstractItemView::selectionMode()
const
1089 Q_D(
const QAbstractItemView);
1090 return d->selectionMode;
1094
1095
1096
1097
1098
1099
1100
1101
1103void QAbstractItemView::setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
1105 Q_D(QAbstractItemView);
1106 d->selectionBehavior = behavior;
1109QAbstractItemView::SelectionBehavior QAbstractItemView::selectionBehavior()
const
1111 Q_D(
const QAbstractItemView);
1112 return d->selectionBehavior;
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129void QAbstractItemView::setCurrentIndex(
const QModelIndex &index)
1131 Q_D(QAbstractItemView);
1132 if (d->selectionModel && (!index.isValid() || d->isIndexEnabled(index))) {
1133 QItemSelectionModel::SelectionFlags command = selectionCommand(index,
nullptr);
1134 d->selectionModel->setCurrentIndex(index, command);
1135 d->currentIndexSet =
true;
1140
1141
1142
1143
1144QModelIndex QAbstractItemView::currentIndex()
const
1146 Q_D(
const QAbstractItemView);
1147 return d->selectionModel ? d->selectionModel->currentIndex() : QModelIndex();
1152
1153
1154
1155
1156
1157
1158
1159
1160void QAbstractItemView::reset()
1162 Q_D(QAbstractItemView);
1163 d->delayedReset.stop();
1167 const auto copy = d->indexEditorHash;
1168 for (
const auto &[index, info] : copy.asKeyValueRange()) {
1170 d->releaseEditor(info.widget.data(), d->indexForEditor(info.widget.data()));
1172 d->editorIndexHash.clear();
1173 d->indexEditorHash.clear();
1174 d->persistent.clear();
1175 d->currentIndexSet =
false;
1177 setRootIndex(QModelIndex());
1178 if (d->selectionModel)
1179 d->selectionModel->reset();
1180#if QT_CONFIG(accessibility)
1181 if (QAccessible::isActive()) {
1182 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1183 QAccessible::updateAccessibility(&accessibleEvent);
1186 d->updateGeometry();
1190
1191
1192
1193
1194void QAbstractItemView::setRootIndex(
const QModelIndex &index)
1196 Q_D(QAbstractItemView);
1197 if (Q_UNLIKELY(index.isValid() && index.model() != d->model)) {
1198 qWarning(
"QAbstractItemView::setRootIndex failed : index must be from the currently set model");
1202#if QT_CONFIG(accessibility)
1203 if (QAccessible::isActive()) {
1204 QAccessibleTableModelChangeEvent accessibleEvent(
this, QAccessibleTableModelChangeEvent::ModelReset);
1205 QAccessible::updateAccessibility(&accessibleEvent);
1208 d->doDelayedItemsLayout();
1209 d->updateGeometry();
1213
1214
1215
1216
1217
1218QModelIndex QAbstractItemView::rootIndex()
const
1220 return QModelIndex(d_func()->root);
1224
1225
1226
1227
1228
1229
1230void QAbstractItemView::selectAll()
1232 Q_D(QAbstractItemView);
1233 const SelectionMode mode = d->selectionMode;
1235 case MultiSelection:
1236 case ExtendedSelection:
1237 d->selectAll(QItemSelectionModel::ClearAndSelect
1238 | d->selectionBehaviorFlags());
1241 case ContiguousSelection:
1242 if (d->model->hasChildren(d->root))
1243 d->selectAll(selectionCommand(d->model->index(0, 0, d->root)));
1245 case SingleSelection:
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262void QAbstractItemView::edit(
const QModelIndex &index)
1264 Q_D(QAbstractItemView);
1265 if (Q_UNLIKELY(!d->isIndexValid(index)))
1266 qWarning(
"edit: index was invalid");
1267 if (Q_UNLIKELY(!edit(index, AllEditTriggers,
nullptr)))
1268 qWarning(
"edit: editing failed");
1272
1273
1274
1275
1276void QAbstractItemView::clearSelection()
1278 Q_D(QAbstractItemView);
1279 if (d->selectionModel)
1280 d->selectionModel->clearSelection();
1284
1285
1286
1287
1288
1289void QAbstractItemView::doItemsLayout()
1291 Q_D(QAbstractItemView);
1292 d->interruptDelayedItemsLayout();
1294 d->viewport->update();
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312void QAbstractItemView::setEditTriggers(EditTriggers actions)
1314 Q_D(QAbstractItemView);
1315 d->editTriggers = actions;
1318QAbstractItemView::EditTriggers QAbstractItemView::editTriggers()
const
1320 Q_D(
const QAbstractItemView);
1321 return d->editTriggers;
1325
1326
1327
1328
1329
1330
1331
1333void QAbstractItemView::setVerticalScrollMode(ScrollMode mode)
1335 Q_D(QAbstractItemView);
1336 d->verticalScrollModeSet =
true;
1337 if (mode == d->verticalScrollMode)
1339 QModelIndex topLeft = indexAt(QPoint(0, 0));
1340 d->verticalScrollMode = mode;
1341 if (mode == ScrollPerItem)
1342 verticalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1344 verticalScrollBar()->setSingleStep(-1);
1346 scrollTo(topLeft, QAbstractItemView::PositionAtTop);
1349QAbstractItemView::ScrollMode QAbstractItemView::verticalScrollMode()
const
1351 Q_D(
const QAbstractItemView);
1352 return d->verticalScrollMode;
1355void QAbstractItemView::resetVerticalScrollMode()
1357 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1358 setVerticalScrollMode(sm);
1359 d_func()->verticalScrollModeSet =
false;
1363
1364
1365
1366
1367
1368
1369
1371void QAbstractItemView::setHorizontalScrollMode(ScrollMode mode)
1373 Q_D(QAbstractItemView);
1374 d->horizontalScrollModeSet =
true;
1375 if (mode == d->horizontalScrollMode)
1377 d->horizontalScrollMode = mode;
1378 if (mode == ScrollPerItem)
1379 horizontalScrollBar()->d_func()->itemviewChangeSingleStep(1);
1381 horizontalScrollBar()->setSingleStep(-1);
1385QAbstractItemView::ScrollMode QAbstractItemView::horizontalScrollMode()
const
1387 Q_D(
const QAbstractItemView);
1388 return d->horizontalScrollMode;
1391void QAbstractItemView::resetHorizontalScrollMode()
1393 auto sm =
static_cast<ScrollMode>(style()->styleHint(QStyle::SH_ItemView_ScrollMode,
nullptr,
this,
nullptr));
1394 setHorizontalScrollMode(sm);
1395 d_func()->horizontalScrollModeSet =
false;
1398#if QT_CONFIG(draganddrop)
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419void QAbstractItemView::setDragDropOverwriteMode(
bool overwrite)
1421 Q_D(QAbstractItemView);
1422 d->overwrite = overwrite;
1425bool QAbstractItemView::dragDropOverwriteMode()
const
1427 Q_D(
const QAbstractItemView);
1428 return d->overwrite;
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1446void QAbstractItemView::setAutoScroll(
bool enable)
1448 Q_D(QAbstractItemView);
1449 d->autoScroll = enable;
1452bool QAbstractItemView::hasAutoScroll()
const
1454 Q_D(
const QAbstractItemView);
1455 return d->autoScroll;
1459
1460
1461
1462
1463
1464
1465void QAbstractItemView::setAutoScrollMargin(
int margin)
1467 Q_D(QAbstractItemView);
1468 d->autoScrollMargin = margin;
1471int QAbstractItemView::autoScrollMargin()
const
1473 Q_D(
const QAbstractItemView);
1474 return d->autoScrollMargin;
1478
1479
1480
1482void QAbstractItemView::setTabKeyNavigation(
bool enable)
1484 Q_D(QAbstractItemView);
1485 d->tabKeyNavigation = enable;
1488bool QAbstractItemView::tabKeyNavigation()
const
1490 Q_D(
const QAbstractItemView);
1491 return d->tabKeyNavigation;
1495
1496
1497
1498QSize QAbstractItemView::viewportSizeHint()
const
1500 return QAbstractScrollArea::viewportSizeHint();
1503#if QT_CONFIG(draganddrop)
1505
1506
1507
1508
1509
1511void QAbstractItemView::setDropIndicatorShown(
bool enable)
1513 Q_D(QAbstractItemView);
1514 d->showDropIndicator = enable;
1517bool QAbstractItemView::showDropIndicator()
const
1519 Q_D(
const QAbstractItemView);
1520 return d->showDropIndicator;
1524
1525
1526
1527
1528
1530void QAbstractItemView::setDragEnabled(
bool enable)
1532 Q_D(QAbstractItemView);
1533 d->dragEnabled = enable;
1536bool QAbstractItemView::dragEnabled()
const
1538 Q_D(
const QAbstractItemView);
1539 return d->dragEnabled;
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1562
1563
1564
1565
1566
1567void QAbstractItemView::setDragDropMode(DragDropMode behavior)
1569 Q_D(QAbstractItemView);
1570 d->dragDropMode = behavior;
1571 setDragEnabled(behavior == DragOnly || behavior == DragDrop || behavior == InternalMove);
1572 setAcceptDrops(behavior == DropOnly || behavior == DragDrop || behavior == InternalMove);
1575QAbstractItemView::DragDropMode QAbstractItemView::dragDropMode()
const
1577 Q_D(
const QAbstractItemView);
1578 DragDropMode setBehavior = d->dragDropMode;
1579 if (!dragEnabled() && !acceptDrops())
1582 if (dragEnabled() && !acceptDrops())
1585 if (!dragEnabled() && acceptDrops())
1588 if (dragEnabled() && acceptDrops()) {
1589 if (setBehavior == InternalMove)
1599
1600
1601
1602
1603
1604
1605
1606
1607void QAbstractItemView::setDefaultDropAction(Qt::DropAction dropAction)
1609 Q_D(QAbstractItemView);
1610 d->defaultDropAction = dropAction;
1613Qt::DropAction QAbstractItemView::defaultDropAction()
const
1615 Q_D(
const QAbstractItemView);
1616 return d->defaultDropAction;
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631void QAbstractItemView::setAlternatingRowColors(
bool enable)
1633 Q_D(QAbstractItemView);
1634 d->alternatingColors = enable;
1636 d->viewport->update();
1639bool QAbstractItemView::alternatingRowColors()
const
1641 Q_D(
const QAbstractItemView);
1642 return d->alternatingColors;
1646
1647
1648
1649
1650
1651
1652void QAbstractItemView::setIconSize(
const QSize &size)
1654 Q_D(QAbstractItemView);
1655 if (size == d->iconSize)
1658 d->doDelayedItemsLayout();
1659 emit iconSizeChanged(size);
1662QSize QAbstractItemView::iconSize()
const
1664 Q_D(
const QAbstractItemView);
1669
1670
1671
1672
1673
1674
1675void QAbstractItemView::setTextElideMode(Qt::TextElideMode mode)
1677 Q_D(QAbstractItemView);
1678 d->textElideMode = mode;
1681Qt::TextElideMode QAbstractItemView::textElideMode()
const
1683 return d_func()->textElideMode;
1687
1688
1689bool QAbstractItemView::focusNextPrevChild(
bool next)
1691 Q_D(QAbstractItemView);
1692 if (d->tabKeyNavigation && isVisible() && isEnabled() && d->viewport->isEnabled()) {
1693 QKeyEvent event(QEvent::KeyPress, next ? Qt::Key_Tab : Qt::Key_Backtab, Qt::NoModifier);
1694 keyPressEvent(&event);
1695 if (event.isAccepted())
1698 return QAbstractScrollArea::focusNextPrevChild(next);
1702
1703
1704bool QAbstractItemView::event(QEvent *event)
1706 Q_D(QAbstractItemView);
1707 switch (event->type()) {
1711 d->executePostedLayout();
1714 d->executePostedLayout();
1715 if (d->shouldScrollToCurrentOnShow) {
1716 d->shouldScrollToCurrentOnShow =
false;
1717 const QModelIndex current = currentIndex();
1718 if (current.isValid() && (d->state == QAbstractItemView::EditingState || d->autoScroll))
1722 case QEvent::LocaleChange:
1723 viewport()->update();
1725 case QEvent::LayoutDirectionChange:
1726 case QEvent::ApplicationLayoutDirectionChange:
1729 case QEvent::StyleChange:
1731 if (!d->verticalScrollModeSet)
1732 resetVerticalScrollMode();
1733 if (!d->horizontalScrollModeSet)
1734 resetHorizontalScrollMode();
1736 case QEvent::FocusOut:
1737 d->checkPersistentEditorFocus();
1739 case QEvent::FontChange:
1740 d->doDelayedItemsLayout();
1745 return QAbstractScrollArea::event(event);
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759bool QAbstractItemView::viewportEvent(QEvent *event)
1761 Q_D(QAbstractItemView);
1762 switch (event->type()) {
1767 d->executePostedLayout();
1769 case QEvent::HoverMove:
1770 case QEvent::HoverEnter:
1771 d->setHoverIndex(indexAt(
static_cast<QHoverEvent*>(event)->position().toPoint()));
1773 case QEvent::HoverLeave:
1774 d->setHoverIndex(QModelIndex());
1777 d->viewportEnteredNeeded =
true;
1780 d->setHoverIndex(QModelIndex());
1781 #if QT_CONFIG(statustip)
1782 if (d->shouldClearStatusTip && d->parent) {
1784 QStatusTipEvent tip(empty);
1785 QCoreApplication::sendEvent(d->parent, &tip);
1786 d->shouldClearStatusTip =
false;
1789 d->enteredIndex = QModelIndex();
1791 case QEvent::ToolTip:
1792 case QEvent::QueryWhatsThis:
1793 case QEvent::WhatsThis: {
1794 QHelpEvent *he =
static_cast<QHelpEvent*>(event);
1795 const QModelIndex index = indexAt(he->pos());
1796 QStyleOptionViewItem option;
1797 initViewItemOption(&option);
1798 option.rect = visualRect(index);
1799 option.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
1801 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
1804 return delegate->helpEvent(he,
this, option, index);
1806 case QEvent::FontChange:
1807 d->doDelayedItemsLayout();
1809 case QEvent::WindowActivate:
1810 case QEvent::WindowDeactivate:
1811 d->viewport->update();
1813 case QEvent::ScrollPrepare:
1814 executeDelayedItemsLayout();
1815#if QT_CONFIG(gestures) && QT_CONFIG(scroller)
1816 d->scollerConnection = QObjectPrivate::connect(
1817 QScroller::scroller(d->viewport), &QScroller::stateChanged,
1818 d, &QAbstractItemViewPrivate::scrollerStateChanged,
1819 Qt::UniqueConnection);
1826 return QAbstractScrollArea::viewportEvent(event);
1830
1831
1832
1833
1834void QAbstractItemView::mousePressEvent(QMouseEvent *event)
1836 Q_D(QAbstractItemView);
1837 d->releaseFromDoubleClick =
false;
1838 d->delayedAutoScroll.stop();
1839 QPoint pos = event->position().toPoint();
1840 QPersistentModelIndex index = indexAt(pos);
1843 d->pressClosedEditor = d->pressClosedEditorWatcher.isActive() && d->lastEditedIndex == index;
1845 if (!d->selectionModel || (d->state == EditingState && d->hasEditor(index)))
1848 d->pressedAlreadySelected = d->selectionModel->isSelected(index);
1849 d->pressedIndex = index;
1850 d->pressedModifiers = event->modifiers();
1851 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1852 d->noSelectionOnMousePress = command == QItemSelectionModel::NoUpdate || !index.isValid();
1853 QPoint offset = d->offset();
1854 d->draggedPosition = pos + offset;
1856#if QT_CONFIG(draganddrop)
1859 d->pressedPosition = d->draggedPosition;
1862 if (!(command & QItemSelectionModel::Current)) {
1863 d->pressedPosition = pos + offset;
1864 d->currentSelectionStartIndex = index;
1866 else if (!d->currentSelectionStartIndex.isValid())
1867 d->currentSelectionStartIndex = currentIndex();
1869 if (edit(index, NoEditTriggers, event))
1872 if (index.isValid() && d->isIndexEnabled(index)) {
1875 bool autoScroll = d->autoScroll;
1876 d->autoScroll =
false;
1877 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1878 d->autoScroll = autoScroll;
1879 if (command.testFlag(QItemSelectionModel::Toggle)) {
1880 command &= ~QItemSelectionModel::Toggle;
1881 d->ctrlDragSelectionFlag = d->selectionModel->isSelected(index) ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
1882 command |= d->ctrlDragSelectionFlag;
1885 if (!(command & QItemSelectionModel::Current)) {
1886 setSelection(QRect(pos, QSize(1, 1)), command);
1888 QRect rect(visualRect(d->currentSelectionStartIndex).center(), pos);
1889 setSelection(rect, command);
1893 emit pressed(index);
1894 if (d->autoScroll) {
1897 d->delayedAutoScroll.start(QApplication::doubleClickInterval()+100,
this);
1902 d->selectionModel->select(QModelIndex(), QItemSelectionModel::Select);
1907
1908
1909
1910
1911void QAbstractItemView::mouseMoveEvent(QMouseEvent *event)
1913 Q_D(QAbstractItemView);
1914 QPoint bottomRight = event->position().toPoint();
1916 d->draggedPosition = bottomRight + d->offset();
1918 if (state() == ExpandingState || state() == CollapsingState)
1921#if QT_CONFIG(draganddrop)
1922 if (state() == DraggingState) {
1923 d->maybeStartDrag(bottomRight);
1928 QPersistentModelIndex index = indexAt(bottomRight);
1929 QModelIndex buddy = d->model->buddy(d->pressedIndex);
1930 if ((state() == EditingState && d->hasEditor(buddy))
1931 || edit(index, NoEditTriggers, event))
1934 const QPoint topLeft =
1935 d->selectionMode != SingleSelection ? d->pressedPosition - d->offset() : bottomRight;
1937 d->checkMouseMove(index);
1939#if QT_CONFIG(draganddrop)
1940 if (d->pressedIndex.isValid()
1942 && (state() != DragSelectingState)
1943 && (event->buttons() != Qt::NoButton)
1944 && !d->selectedDraggableIndexes().isEmpty()) {
1945 setState(DraggingState);
1946 d->maybeStartDrag(bottomRight);
1951 if ((event->buttons() & Qt::LeftButton) && d->selectionAllowed(index) && d->selectionModel) {
1952 setState(DragSelectingState);
1953 QItemSelectionModel::SelectionFlags command = selectionCommand(index, event);
1954 if (d->ctrlDragSelectionFlag != QItemSelectionModel::NoUpdate && command.testFlag(QItemSelectionModel::Toggle)) {
1955 command &= ~QItemSelectionModel::Toggle;
1956 command |= d->ctrlDragSelectionFlag;
1960 QRect selectionRect = QRect(topLeft, bottomRight);
1961 setSelection(selectionRect, command);
1964 if (index.isValid() && (index != d->selectionModel->currentIndex()) && d->isIndexEnabled(index))
1965 d->selectionModel->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
1966 else if (d->shouldAutoScroll(event->pos()) && !d->autoScrollTimer.isActive())
1972
1973
1974
1975
1976
1977
1978void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event)
1980 Q_D(QAbstractItemView);
1981 const bool releaseFromDoubleClick = d->releaseFromDoubleClick;
1982 d->releaseFromDoubleClick =
false;
1984 QPoint pos = event->position().toPoint();
1985 QPersistentModelIndex index = indexAt(pos);
1987 if (state() == EditingState) {
1988 if (d->isIndexValid(index)
1989 && d->isIndexEnabled(index)
1990 && d->sendDelegateEvent(index, event))
1995 bool click = (index == d->pressedIndex && index.isValid() && !releaseFromDoubleClick);
1996 bool selectedClicked = click && d->pressedAlreadySelected
1997 && (event->button() == Qt::LeftButton)
1998 && (event->modifiers() == Qt::NoModifier);
1999 EditTrigger trigger = (selectedClicked ? SelectedClicked : NoEditTriggers);
2000 const bool edited = click && !d->pressClosedEditor ? edit(index, trigger, event) :
false;
2002 d->ctrlDragSelectionFlag = QItemSelectionModel::NoUpdate;
2004 if (d->selectionModel && d->noSelectionOnMousePress) {
2005 d->noSelectionOnMousePress =
false;
2006 if (!d->pressClosedEditor)
2007 d->selectionModel->select(index, selectionCommand(index, event));
2010 d->pressClosedEditor =
false;
2014 if (event->button() == Qt::LeftButton)
2015 emit clicked(index);
2018 QStyleOptionViewItem option;
2019 initViewItemOption(&option);
2020 if (d->pressedAlreadySelected)
2021 option.state |= QStyle::State_Selected;
2022 if ((d->model->flags(index) & Qt::ItemIsEnabled)
2023 && style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, &option,
this))
2024 emit activated(index);
2029
2030
2031
2032
2033void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event)
2035 Q_D(QAbstractItemView);
2037 QModelIndex index = indexAt(event->position().toPoint());
2038 if (!index.isValid()
2039 || !d->isIndexEnabled(index)
2040 || (d->pressedIndex != index)) {
2041 QMouseEvent me(QEvent::MouseButtonPress,
2042 event->position(), event->scenePosition(), event->globalPosition(),
2043 event->button(), event->buttons(), event->modifiers(),
2044 event->source(), event->pointingDevice());
2045 mousePressEvent(&me);
2049 QPersistentModelIndex persistent = index;
2050 emit doubleClicked(persistent);
2051 if ((event->button() == Qt::LeftButton) && !edit(persistent, DoubleClicked, event)
2052 && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick,
nullptr,
this))
2053 emit activated(persistent);
2054 d->releaseFromDoubleClick =
true;
2057#if QT_CONFIG(draganddrop)
2060
2061
2062
2063
2064
2065
2066void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event)
2068 if (dragDropMode() == InternalMove
2069 && (event->source() !=
this|| !(event->possibleActions() & Qt::MoveAction)))
2072 if (d_func()->canDrop(event)) {
2074 setState(DraggingState);
2081
2082
2083
2084
2085
2086
2087
2088void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event)
2090 Q_D(QAbstractItemView);
2091 d->draggedPosition = event->position().toPoint() + d->offset();
2092 if (dragDropMode() == InternalMove
2093 && (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction)))
2099 QModelIndex index = indexAt(event->position().toPoint());
2101 if (!d->droppingOnItself(event, index)
2102 && d->canDrop(event)) {
2104 if (index.isValid() && d->showDropIndicator) {
2105 QRect rect = visualRect(index);
2106 d->dropIndicatorPosition = d->position(event->position().toPoint(), rect, index);
2107 if (d->selectionBehavior == QAbstractItemView::SelectRows
2108 && d->dropIndicatorPosition != OnViewport
2109 && (d->dropIndicatorPosition != OnItem || event->source() ==
this)) {
2110 const int maxCol = d->model->columnCount(index.parent()) - 1;
2111 const auto idx = index.column() > 0 ? index.siblingAtColumn(0) : index;
2112 rect = d->intersectedRect(viewport()->rect(), idx, idx.siblingAtColumn(maxCol));
2114 switch (d->dropIndicatorPosition) {
2116 if (d->isIndexDropEnabled(index.parent())) {
2117 d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0);
2118 event->acceptProposedAction();
2120 d->dropIndicatorRect = QRect();
2124 if (d->isIndexDropEnabled(index.parent())) {
2125 d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0);
2126 event->acceptProposedAction();
2128 d->dropIndicatorRect = QRect();
2132 if (d->isIndexDropEnabled(index)) {
2133 d->dropIndicatorRect = rect;
2134 event->acceptProposedAction();
2136 d->dropIndicatorRect = QRect();
2140 d->dropIndicatorRect = QRect();
2141 if (d->isIndexDropEnabled(rootIndex())) {
2142 event->acceptProposedAction();
2147 d->dropIndicatorRect = QRect();
2148 d->dropIndicatorPosition = OnViewport;
2149 if (d->isIndexDropEnabled(rootIndex())) {
2150 event->acceptProposedAction();
2153 d->viewport->update();
2156 if (d->shouldAutoScroll(event->position().toPoint()))
2161
2162
2163
2164
2165bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event,
const QModelIndex &index)
2167 Q_Q(QAbstractItemView);
2168 Qt::DropAction dropAction = event->dropAction();
2169 if (q->dragDropMode() == QAbstractItemView::InternalMove)
2170 dropAction = Qt::MoveAction;
2171 if (event->source() == q
2172 && event->possibleActions() & Qt::MoveAction
2173 && dropAction == Qt::MoveAction) {
2174 QModelIndexList selectedIndexes = q->selectedIndexes();
2175 QModelIndex child = index;
2176 while (child.isValid() && child != root) {
2177 if (selectedIndexes.contains(child))
2179 child = child.parent();
2186
2187
2188
2189
2190
2191void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *)
2193 Q_D(QAbstractItemView);
2196 d->hover = QModelIndex();
2197 d->viewport->update();
2201
2202
2203
2204
2205
2206
2207void QAbstractItemView::dropEvent(QDropEvent *event)
2209 Q_D(QAbstractItemView);
2210 if (dragDropMode() == InternalMove) {
2211 if (event->source() !=
this || !(event->possibleActions() & Qt::MoveAction))
2218 if (d->dropOn(event, &row, &col, &index)) {
2219 const Qt::DropAction action = dragDropMode() == InternalMove ? Qt::MoveAction : event->dropAction();
2220 if (d->model->dropMimeData(event->mimeData(), action, row, col, index)) {
2221 if (action != event->dropAction()) {
2222 event->setDropAction(action);
2225 event->acceptProposedAction();
2231 d->viewport->update();
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245bool QAbstractItemViewPrivate::dropOn(QDropEvent *event,
int *dropRow,
int *dropCol, QModelIndex *dropIndex)
2247 Q_Q(QAbstractItemView);
2248 if (event->isAccepted())
2253 if (viewport->rect().contains(event->position().toPoint())) {
2254 index = q->indexAt(event->position().toPoint());
2255 if (!index.isValid())
2260 if (model->supportedDropActions() & event->dropAction()) {
2263 if (index != root) {
2264 dropIndicatorPosition = position(event->position().toPoint(), q->visualRect(index), index);
2265 switch (dropIndicatorPosition) {
2266 case QAbstractItemView::AboveItem:
2268 col = index.column();
2269 index = index.parent();
2271 case QAbstractItemView::BelowItem:
2272 row = index.row() + 1;
2273 col = index.column();
2274 index = index.parent();
2276 case QAbstractItemView::OnItem:
2277 case QAbstractItemView::OnViewport:
2281 dropIndicatorPosition = QAbstractItemView::OnViewport;
2286 if (!droppingOnItself(event, index))
2292QAbstractItemView::DropIndicatorPosition
2293QAbstractItemViewPrivate::position(
const QPoint &pos,
const QRect &rect,
const QModelIndex &index)
const
2295 QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport;
2297 const int margin = qBound(2, qRound(qreal(rect.height()) / 5.5), 12);
2298 if (pos.y() - rect.top() < margin) {
2299 r = QAbstractItemView::AboveItem;
2300 }
else if (rect.bottom() - pos.y() < margin) {
2301 r = QAbstractItemView::BelowItem;
2302 }
else if (rect.contains(pos,
true)) {
2303 r = QAbstractItemView::OnItem;
2306 QRect touchingRect = rect;
2307 touchingRect.adjust(-1, -1, 1, 1);
2308 if (touchingRect.contains(pos,
false)) {
2309 r = QAbstractItemView::OnItem;
2313 if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled)))
2314 r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem;
2322
2323
2324
2325
2326
2327void QAbstractItemView::focusInEvent(QFocusEvent *event)
2329 Q_D(QAbstractItemView);
2330 QAbstractScrollArea::focusInEvent(event);
2332 const QItemSelectionModel* model = selectionModel();
2333 bool currentIndexValid = currentIndex().isValid();
2336 && !d->currentIndexSet
2337 && !currentIndexValid) {
2338 bool autoScroll = d->autoScroll;
2339 d->autoScroll =
false;
2340 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
2341 if (index.isValid() && d->isIndexEnabled(index) && event->reason() != Qt::MouseFocusReason) {
2342 selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);
2343 currentIndexValid =
true;
2345 d->autoScroll = autoScroll;
2348 if (model && currentIndexValid)
2349 setAttribute(Qt::WA_InputMethodEnabled, (currentIndex().flags() & Qt::ItemIsEditable));
2350 else if (!currentIndexValid)
2351 setAttribute(Qt::WA_InputMethodEnabled,
false);
2353 d->viewport->update();
2357
2358
2359
2360
2361
2362void QAbstractItemView::focusOutEvent(QFocusEvent *event)
2364 Q_D(QAbstractItemView);
2365 QAbstractScrollArea::focusOutEvent(event);
2366 d->viewport->update();
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380void QAbstractItemView::keyPressEvent(QKeyEvent *event)
2382 Q_D(QAbstractItemView);
2383 d->delayedAutoScroll.stop();
2385#ifdef QT_KEYPAD_NAVIGATION
2386 switch (event->key()) {
2387 case Qt::Key_Select:
2388 if (QApplicationPrivate::keypadNavigationEnabled()) {
2389 if (!hasEditFocus()) {
2396 if (QApplicationPrivate::keypadNavigationEnabled() && hasEditFocus()) {
2397 setEditFocus(
false);
2407 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()
2408 && QWidgetPrivate::canKeypadNavigate(Qt::Vertical)) {
2416 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()
2417 && (QWidgetPrivate::canKeypadNavigate(Qt::Horizontal) || QWidgetPrivate::inTabWidget(
this))) {
2423 if (QApplicationPrivate::keypadNavigationEnabled() && !hasEditFocus()) {
2430#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT)
2431 if (event == QKeySequence::Copy) {
2432 const QModelIndex index = currentIndex();
2433 if (index.isValid() && d->model) {
2434 const QVariant variant = d->model->data(index, Qt::DisplayRole);
2435 if (variant.canConvert<QString>())
2436 QGuiApplication::clipboard()->setText(variant.toString());
2442 QPersistentModelIndex newCurrent;
2443 d->moveCursorUpdatedView =
false;
2444 switch (event->key()) {
2446 newCurrent = moveCursor(MoveDown, event->modifiers());
2449 newCurrent = moveCursor(MoveUp, event->modifiers());
2452 newCurrent = moveCursor(MoveLeft, event->modifiers());
2455 newCurrent = moveCursor(MoveRight, event->modifiers());
2458 newCurrent = moveCursor(MoveHome, event->modifiers());
2461 newCurrent = moveCursor(MoveEnd, event->modifiers());
2463 case Qt::Key_PageUp:
2464 newCurrent = moveCursor(MovePageUp, event->modifiers());
2466 case Qt::Key_PageDown:
2467 newCurrent = moveCursor(MovePageDown, event->modifiers());
2470 if (d->tabKeyNavigation)
2471 newCurrent = moveCursor(MoveNext, event->modifiers());
2473 case Qt::Key_Backtab:
2474 if (d->tabKeyNavigation)
2475 newCurrent = moveCursor(MovePrevious, event->modifiers());
2479 QPersistentModelIndex oldCurrent = currentIndex();
2480 if (newCurrent != oldCurrent && newCurrent.isValid() && d->isIndexEnabled(newCurrent)) {
2481 if (!hasFocus() && QApplication::focusWidget() == indexWidget(oldCurrent))
2483 QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event);
2484 if (command != QItemSelectionModel::NoUpdate
2485 || style()->styleHint(QStyle::SH_ItemView_MovementWithoutUpdatingSelection,
nullptr,
this)) {
2487 if (command & QItemSelectionModel::Current) {
2488 d->selectionModel->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate);
2489 if (!d->currentSelectionStartIndex.isValid())
2490 d->currentSelectionStartIndex = oldCurrent;
2491 QRect rect(visualRect(d->currentSelectionStartIndex).center(), visualRect(newCurrent).center());
2492 setSelection(rect, command);
2494 d->selectionModel->setCurrentIndex(newCurrent, command);
2495 d->currentSelectionStartIndex = newCurrent;
2496 if (newCurrent.isValid()) {
2498 QRect rect(visualRect(newCurrent).center(), QSize(1, 1));
2499 setSelection(rect, command);
2507 switch (event->key()) {
2511#ifdef QT_KEYPAD_NAVIGATION
2512 if (QApplicationPrivate::keypadNavigationEnabled()
2513 && QWidgetPrivate::canKeypadNavigate(Qt::Vertical)) {
2520#ifdef QT_KEYPAD_NAVIGATION
2521 if (QApplication::navigationMode() == Qt::NavigationModeKeypadDirectional
2522 && (QWidgetPrivate::canKeypadNavigate(Qt::Horizontal)
2523 || (QWidgetPrivate::inTabWidget(
this) && d->model->columnCount(d->root) > 1))) {
2530 case Qt::Key_PageUp:
2531 case Qt::Key_PageDown:
2532 case Qt::Key_Escape:
2534 case Qt::Key_Control:
2535 case Qt::Key_Delete:
2536 case Qt::Key_Backspace:
2540 case Qt::Key_Select:
2541 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2542 if (d->selectionModel)
2543 d->selectionModel->select(currentIndex(), selectionCommand(currentIndex(), event));
2544 if (event->key() == Qt::Key_Space) {
2545 keyboardSearch(event->text());
2549#ifdef QT_KEYPAD_NAVIGATION
2550 if ( event->key()==Qt::Key_Select ) {
2552 if (currentIndex().isValid()) {
2553 if (state() != EditingState)
2554 emit activated(currentIndex());
2563 case Qt::Key_Return:
2566 if (!edit(currentIndex(), EditKeyPressed, event) && d->editorIndexHash.isEmpty())
2571 if (!edit(currentIndex(), EditKeyPressed, event))
2575 case Qt::Key_Return:
2579 if (state() != EditingState || hasFocus()) {
2580 if (currentIndex().isValid())
2581 emit activated(currentIndex());
2587#ifndef QT_NO_SHORTCUT
2588 if (event == QKeySequence::SelectAll && selectionMode() != NoSelection) {
2594 if (event->key() == Qt::Key_O && event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) {
2595 emit activated(currentIndex());
2599 bool modified = (event->modifiers() & (Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier));
2600 if (!event->text().isEmpty() && !modified && !edit(currentIndex(), AnyKeyPressed, event)) {
2601 keyboardSearch(event->text());
2608 if (d->moveCursorUpdatedView)
2613
2614
2615
2616
2617
2618void QAbstractItemView::resizeEvent(QResizeEvent *event)
2620 QAbstractScrollArea::resizeEvent(event);
2625
2626
2627
2628
2629
2630void QAbstractItemView::timerEvent(QTimerEvent *event)
2632 Q_D(QAbstractItemView);
2633 if (event->timerId() == d->fetchMoreTimer.timerId())
2635 else if (event->timerId() == d->delayedReset.timerId())
2637 else if (event->timerId() == d->autoScrollTimer.timerId())
2639 else if (event->timerId() == d->updateTimer.timerId())
2640 d->updateDirtyRegion();
2641 else if (event->timerId() == d->delayedEditing.timerId()) {
2642 d->delayedEditing.stop();
2643 edit(currentIndex());
2644 }
else if (event->timerId() == d->delayedLayout.timerId()) {
2645 d->delayedLayout.stop();
2647 d->interruptDelayedItemsLayout();
2649 const QModelIndex current = currentIndex();
2650 if (current.isValid() && d->state == QAbstractItemView::EditingState)
2653 }
else if (event->timerId() == d->delayedAutoScroll.timerId()) {
2654 d->delayedAutoScroll.stop();
2657 if (d->pressedIndex.isValid() && d->pressedIndex == currentIndex())
2658 scrollTo(d->pressedIndex);
2659 }
else if (event->timerId() == d->pressClosedEditorWatcher.timerId()) {
2660 d->pressClosedEditorWatcher.stop();
2665
2666
2667void QAbstractItemView::inputMethodEvent(QInputMethodEvent *event)
2669 Q_D(QAbstractItemView);
2676 bool forwardEventToEditor =
false;
2677 const bool commit = !event->commitString().isEmpty();
2678 const bool preediting = !event->preeditString().isEmpty();
2679 if (QWidget *currentEditor = d->editorForIndex(currentIndex()).widget) {
2680 if (d->waitForIMCommit) {
2681 if (commit || !preediting) {
2683 d->waitForIMCommit =
false;
2684 QApplication::sendEvent(currentEditor, event);
2686 QAbstractItemDelegate *delegate = itemDelegateForIndex(currentIndex());
2688 delegate->setEditorData(currentEditor, currentIndex());
2689 d->selectAllInEditor(currentEditor);
2691 if (currentEditor->focusPolicy() != Qt::NoFocus)
2692 currentEditor->setFocus();
2695 QApplication::sendEvent(currentEditor, event);
2699 }
else if (preediting) {
2701 d->waitForIMCommit =
true;
2703 forwardEventToEditor =
true;
2704 }
else if (!commit) {
2708 if (!edit(currentIndex(), AnyKeyPressed, event)) {
2709 d->waitForIMCommit =
false;
2711 keyboardSearch(event->commitString());
2713 }
else if (QWidget *currentEditor; forwardEventToEditor
2714 && (currentEditor = d->editorForIndex(currentIndex()).widget)) {
2715 QApplication::sendEvent(currentEditor, event);
2719#if QT_CONFIG(draganddrop)
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2739
2740
2741QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition()
const
2743 Q_D(
const QAbstractItemView);
2744 return d->dropIndicatorPosition;
2749
2750
2751
2752
2753
2754
2755QModelIndexList QAbstractItemView::selectedIndexes()
const
2757 Q_D(
const QAbstractItemView);
2758 QModelIndexList indexes;
2759 if (d->selectionModel) {
2760 indexes = d->selectionModel->selectedIndexes();
2761 auto isHidden = [
this](
const QModelIndex &idx) {
2762 return isIndexHidden(idx);
2764 indexes.removeIf(isHidden);
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782bool QAbstractItemView::edit(
const QModelIndex &index, EditTrigger trigger, QEvent *event)
2784 Q_D(QAbstractItemView);
2786 if (!d->isIndexValid(index))
2789 if (QWidget *w = (d->persistent.isEmpty() ?
static_cast<QWidget*>(
nullptr) : d->editorForIndex(index).widget.data())) {
2790 if (w->focusPolicy() == Qt::NoFocus)
2792 if (!d->waitForIMCommit)
2799 if (trigger == DoubleClicked) {
2800 d->delayedEditing.stop();
2801 d->delayedAutoScroll.stop();
2802 }
else if (trigger == CurrentChanged) {
2803 d->delayedEditing.stop();
2807 QPersistentModelIndex safeIndex(index);
2809 if (d->sendDelegateEvent(index, event)) {
2814 if (!safeIndex.isValid()) {
2819 EditTriggers lastTrigger = d->lastTrigger;
2820 d->lastTrigger = trigger;
2822 if (!d->shouldEdit(trigger, d->model->buddy(safeIndex)))
2825 if (d->delayedEditing.isActive())
2830 if (lastTrigger == DoubleClicked && trigger == SelectedClicked)
2834 if (trigger == SelectedClicked)
2835 d->delayedEditing.start(QApplication::doubleClickInterval(),
this);
2837 d->openEditor(safeIndex, d->shouldForwardEvent(trigger, event) ? event :
nullptr);
2843
2844
2845
2846void QAbstractItemView::updateEditorData()
2848 Q_D(QAbstractItemView);
2849 d->updateEditorData(QModelIndex(), QModelIndex());
2853
2854
2855
2856void QAbstractItemView::updateEditorGeometries()
2858 Q_D(QAbstractItemView);
2859 if (d->editorIndexHash.isEmpty())
2861 if (d->delayedPendingLayout) {
2863 d->executePostedLayout();
2866 QStyleOptionViewItem option;
2867 initViewItemOption(&option);
2868 QEditorIndexHash::iterator it = d->editorIndexHash.begin();
2869 QWidgetList editorsToRelease;
2870 QWidgetList editorsToHide;
2871 while (it != d->editorIndexHash.end()) {
2872 QModelIndex index = it.value();
2873 QWidget *editor = it.key();
2874 if (index.isValid() && editor) {
2875 option.rect = visualRect(index);
2876 if (option.rect.isValid()) {
2878 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
2880 delegate->updateEditorGeometry(editor, option, index);
2882 editorsToHide << editor;
2886 d->indexEditorHash.remove(it.value());
2887 it = d->editorIndexHash.erase(it);
2888 editorsToRelease << editor;
2894 for (
int i = 0; i < editorsToHide.size(); ++i) {
2895 editorsToHide.at(i)->hide();
2897 for (
int i = 0; i < editorsToRelease.size(); ++i) {
2898 d->releaseEditor(editorsToRelease.at(i));
2903
2904
2905void QAbstractItemView::updateGeometries()
2907 Q_D(QAbstractItemView);
2908 updateEditorGeometries();
2909 d->fetchMoreTimer.start(0,
this);
2910 d->updateGeometry();
2914
2915
2916void QAbstractItemView::verticalScrollbarValueChanged(
int value)
2918 Q_D(QAbstractItemView);
2919 if (verticalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2920 d->model->fetchMore(d->root);
2921 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2922 if (viewport()->rect().contains(posInVp))
2923 d->checkMouseMove(posInVp);
2927
2928
2929void QAbstractItemView::horizontalScrollbarValueChanged(
int value)
2931 Q_D(QAbstractItemView);
2932 if (horizontalScrollBar()->maximum() == value && d->model->canFetchMore(d->root))
2933 d->model->fetchMore(d->root);
2934 QPoint posInVp = viewport()->mapFromGlobal(QCursor::pos());
2935 if (viewport()->rect().contains(posInVp))
2936 d->checkMouseMove(posInVp);
2940
2941
2942void QAbstractItemView::verticalScrollbarAction(
int)
2948
2949
2950void QAbstractItemView::horizontalScrollbarAction(
int)
2956
2957
2958
2959
2960
2961
2962
2964void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint)
2966 Q_D(QAbstractItemView);
2970 const bool isPersistent = d->persistent.contains(editor);
2971 const QModelIndex index = d->indexForEditor(editor);
2972 if (!index.isValid()) {
2973 if (!editor->isVisible()) {
2981 qWarning(
"QAbstractItemView::closeEditor called with an editor that does not belong to this view");
2985 const bool hadFocus = editor->hasFocus();
2989 d->pressClosedEditorWatcher.start(0,
this);
2990 d->lastEditedIndex = index;
2992 if (!isPersistent) {
2994 QModelIndex index = d->indexForEditor(editor);
2995 editor->removeEventFilter(itemDelegateForIndex(index));
2996 d->removeEditor(editor);
2999 if (focusPolicy() != Qt::NoFocus)
3002 editor->clearFocus();
3004 d->checkPersistentEditorFocus();
3007 QPointer<QWidget> ed = editor;
3008 QCoreApplication::sendPostedEvents(editor, 0);
3011 if (!isPersistent && editor)
3012 d->releaseEditor(editor, index);
3017 QItemSelectionModel::SelectionFlags flags = QItemSelectionModel::NoUpdate;
3018 if (d->selectionMode != NoSelection)
3019 flags = QItemSelectionModel::ClearAndSelect | d->selectionBehaviorFlags();
3021 case QAbstractItemDelegate::EditNextItem: {
3022 QModelIndex index = moveCursor(MoveNext, Qt::NoModifier);
3023 if (index.isValid()) {
3024 QPersistentModelIndex persistent(index);
3025 d->selectionModel->setCurrentIndex(persistent, flags);
3027 if (index.flags() & Qt::ItemIsEditable
3028 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
3031 case QAbstractItemDelegate::EditPreviousItem: {
3032 QModelIndex index = moveCursor(MovePrevious, Qt::NoModifier);
3033 if (index.isValid()) {
3034 QPersistentModelIndex persistent(index);
3035 d->selectionModel->setCurrentIndex(persistent, flags);
3037 if (index.flags() & Qt::ItemIsEditable
3038 && (!(editTriggers() & QAbstractItemView::CurrentChanged)))
3041 case QAbstractItemDelegate::SubmitModelCache:
3044 case QAbstractItemDelegate::RevertModelCache:
3053
3054
3055
3056
3057void QAbstractItemView::commitData(QWidget *editor)
3059 Q_D(QAbstractItemView);
3060 if (!editor || !d->itemDelegate || d->currentlyCommittingEditor)
3062 QModelIndex index = d->indexForEditor(editor);
3063 if (!index.isValid()) {
3064 qWarning(
"QAbstractItemView::commitData called with an editor that does not belong to this view");
3067 d->currentlyCommittingEditor = editor;
3068 QAbstractItemDelegate *delegate = itemDelegateForIndex(index);
3069 editor->removeEventFilter(delegate);
3070 delegate->setModelData(editor, d->model, index);
3071 editor->installEventFilter(delegate);
3072 d->currentlyCommittingEditor =
nullptr;
3076
3077
3078
3079
3080void QAbstractItemView::editorDestroyed(QObject *editor)
3082 Q_D(QAbstractItemView);
3083 QWidget *w = qobject_cast<QWidget*>(editor);
3085 d->persistent.remove(w);
3086 if (state() == EditingState)
3093
3094
3095
3096
3097
3098
3099
3100void QAbstractItemView::keyboardSearch(
const QString &search)
3102 Q_D(QAbstractItemView);
3103 if (!d->model->rowCount(d->root) || !d->model->columnCount(d->root))
3106 QModelIndex start = currentIndex().isValid() ? currentIndex()
3107 : d->model->index(0, 0, d->root);
3108 bool skipRow =
false;
3109 bool keyboardTimeWasValid = d->keyboardInputTime.isValid();
3110 qint64 keyboardInputTimeElapsed;
3111 if (keyboardTimeWasValid)
3112 keyboardInputTimeElapsed = d->keyboardInputTime.restart();
3114 d->keyboardInputTime.start();
3115 if (search.isEmpty() || !keyboardTimeWasValid
3116 || keyboardInputTimeElapsed > QApplication::keyboardInputInterval()) {
3117 d->keyboardInput = search;
3118 skipRow = currentIndex().isValid();
3120 d->keyboardInput += search;
3124 bool sameKey =
false;
3125 if (d->keyboardInput.size() > 1) {
3126 int c = d->keyboardInput.count(d->keyboardInput.at(d->keyboardInput.size() - 1));
3127 sameKey = (c == d->keyboardInput.size());
3134 QModelIndex parent = start.parent();
3135 int newRow = (start.row() < d->model->rowCount(parent) - 1) ? start.row() + 1 : 0;
3136 start = d->model->index(newRow, start.column(), parent);
3140 QModelIndex current = start;
3141 QModelIndexList match;
3142 QModelIndex firstMatch;
3143 QModelIndex startMatch;
3144 QModelIndexList previous;
3146 match = d->model->match(current, Qt::DisplayRole, d->keyboardInput, 1,
3147 d->keyboardSearchFlags);
3148 if (match == previous)
3150 firstMatch = match.value(0);
3152 if (firstMatch.isValid()) {
3153 if (d->isIndexEnabled(firstMatch)) {
3154 setCurrentIndex(firstMatch);
3157 int row = firstMatch.row() + 1;
3158 if (row >= d->model->rowCount(firstMatch.parent()))
3160 current = firstMatch.sibling(row, firstMatch.column());
3163 if (!startMatch.isValid())
3164 startMatch = firstMatch;
3165 else if (startMatch == firstMatch)
3168 }
while (current != start && firstMatch.isValid());
3172
3173
3174
3175
3176
3177QSize QAbstractItemView::sizeHintForIndex(
const QModelIndex &index)
const
3179 Q_D(
const QAbstractItemView);
3180 if (!d->isIndexValid(index))
3182 const auto delegate = itemDelegateForIndex(index);
3183 QStyleOptionViewItem option;
3184 initViewItemOption(&option);
3185 return delegate ? delegate->sizeHint(option, index) : QSize();
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204int QAbstractItemView::sizeHintForRow(
int row)
const
3206 Q_D(
const QAbstractItemView);
3208 if (row < 0 || row >= d->model->rowCount(d->root))
3213 QStyleOptionViewItem option;
3214 initViewItemOption(&option);
3216 int colCount = d->model->columnCount(d->root);
3217 for (
int c = 0; c < colCount; ++c) {
3218 const QModelIndex index = d->model->index(row, c, d->root);
3219 if (QWidget *editor = d->editorForIndex(index).widget.data())
3220 height = qMax(height, editor->height());
3221 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3222 height = qMax(height, delegate->sizeHint(option, index).height());
3228
3229
3230
3231
3232
3233
3234
3235int QAbstractItemView::sizeHintForColumn(
int column)
const
3237 Q_D(
const QAbstractItemView);
3239 if (column < 0 || column >= d->model->columnCount(d->root))
3244 QStyleOptionViewItem option;
3245 initViewItemOption(&option);
3247 int rows = d->model->rowCount(d->root);
3248 for (
int r = 0; r < rows; ++r) {
3249 const QModelIndex index = d->model->index(r, column, d->root);
3250 if (QWidget *editor = d->editorForIndex(index).widget.data())
3251 width = qMax(width, editor->sizeHint().width());
3252 if (
const QAbstractItemDelegate *delegate = itemDelegateForIndex(index))
3253 width = qMax(width, delegate->sizeHint(option, index).width());
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275int QAbstractItemView::updateThreshold()
const
3277 Q_D(
const QAbstractItemView);
3278 return d->updateThreshold;
3281void QAbstractItemView::setUpdateThreshold(
int threshold)
3283 Q_D(QAbstractItemView);
3284 if (d->updateThreshold == threshold)
3286 d->updateThreshold = threshold;
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3301Qt::MatchFlags QAbstractItemView::keyboardSearchFlags()
const
3303 Q_D(
const QAbstractItemView);
3304 return d->keyboardSearchFlags;
3307void QAbstractItemView::setKeyboardSearchFlags(Qt::MatchFlags searchFlags)
3309 Q_D(QAbstractItemView);
3310 d->keyboardSearchFlags = searchFlags;
3314
3315
3316
3317
3318
3319void QAbstractItemView::openPersistentEditor(
const QModelIndex &index)
3321 Q_D(QAbstractItemView);
3322 QStyleOptionViewItem options;
3323 initViewItemOption(&options);
3324 options.rect = visualRect(index);
3325 options.state |= (index == currentIndex() ? QStyle::State_HasFocus : QStyle::State_None);
3327 QWidget *editor = d->editor(index, options);
3330 d->persistent.insert(editor);
3335
3336
3337
3338
3339void QAbstractItemView::closePersistentEditor(
const QModelIndex &index)
3341 Q_D(QAbstractItemView);
3342 if (QWidget *editor = d->editorForIndex(index).widget.data()) {
3343 if (index == selectionModel()->currentIndex())
3344 closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
3345 d->persistent.remove(editor);
3346 d->removeEditor(editor);
3347 d->releaseEditor(editor, index);
3352
3353
3354
3355
3356
3357
3358bool QAbstractItemView::isPersistentEditorOpen(
const QModelIndex &index)
const
3360 Q_D(
const QAbstractItemView);
3361 QWidget *editor = d->editorForIndex(index).widget;
3362 return editor && d->persistent.contains(editor);
4130void QAbstractItemView::doAutoScroll()
4133 Q_D(QAbstractItemView);
4134 QScrollBar *verticalScroll = verticalScrollBar();
4135 QScrollBar *horizontalScroll = horizontalScrollBar();
4139 QHeaderView *hv = qobject_cast<QHeaderView*>(
this);
4141 QAbstractScrollArea *parent = qobject_cast<QAbstractScrollArea*>(parentWidget());
4143 if (hv->orientation() == Qt::Horizontal) {
4144 if (!hv->horizontalScrollBar() || !hv->horizontalScrollBar()->isVisible())
4145 horizontalScroll = parent->horizontalScrollBar();
4147 if (!hv->verticalScrollBar() || !hv->verticalScrollBar()->isVisible())
4148 verticalScroll = parent->verticalScrollBar();
4153 const int verticalStep = verticalScroll->pageStep();
4154 const int horizontalStep = horizontalScroll->pageStep();
4155 if (d->autoScrollCount < qMax(verticalStep, horizontalStep))
4156 ++d->autoScrollCount;
4158 const int margin = d->autoScrollMargin;
4159 const int verticalValue = verticalScroll->value();
4160 const int horizontalValue = horizontalScroll->value();
4162 const QPoint pos = d->draggedPosition - d->offset();
4163 const QRect area = QWidgetPrivate::get(d->viewport)->clipRect();
4166 if (pos.y() - area.top() < margin)
4167 verticalScroll->setValue(verticalValue - d->autoScrollCount);
4168 else if (area.bottom() - pos.y() < margin)
4169 verticalScroll->setValue(verticalValue + d->autoScrollCount);
4170 if (pos.x() - area.left() < margin)
4171 horizontalScroll->setValue(horizontalValue - d->autoScrollCount);
4172 else if (area.right() - pos.x() < margin)
4173 horizontalScroll->setValue(horizontalValue + d->autoScrollCount);
4175 const bool verticalUnchanged = (verticalValue == verticalScroll->value());
4176 const bool horizontalUnchanged = (horizontalValue == horizontalScroll->value());
4177 if (verticalUnchanged && horizontalUnchanged) {
4180#if QT_CONFIG(draganddrop)
4181 d->dropIndicatorRect = QRect();
4182 d->dropIndicatorPosition = QAbstractItemView::OnViewport;
4185 case QAbstractItemView::DragSelectingState: {
4188 const QPoint globalPos = d->viewport->mapToGlobal(pos);
4189 const QPoint windowPos = window()->mapFromGlobal(globalPos);
4190 QMouseEvent mm(QEvent::MouseMove, pos, windowPos, globalPos,
4191 Qt::NoButton, Qt::LeftButton, d->pressedModifiers,
4192 Qt::MouseEventSynthesizedByQt);
4193 QApplication::sendEvent(viewport(), &mm);
4196 case QAbstractItemView::DraggingState: {
4202 d->draggedPosition = pos + d->offset();
4208 d->viewport->update();
4312QItemSelectionModel::SelectionFlags QAbstractItemViewPrivate::extendedSelectionCommand(
4313 const QModelIndex &index,
const QEvent *event)
const
4315 Qt::KeyboardModifiers modifiers = event && event->isInputEvent()
4316 ?
static_cast<
const QInputEvent*>(event)->modifiers()
4317 : QGuiApplication::keyboardModifiers();
4319 switch (event->type()) {
4320 case QEvent::MouseMove: {
4322 if (modifiers & Qt::ControlModifier)
4323 return QItemSelectionModel::ToggleCurrent|selectionBehaviorFlags();
4326 case QEvent::MouseButtonPress: {
4327 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4328 const bool rightButtonPressed = button & Qt::RightButton;
4329 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4330 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4331 const bool indexIsSelected = selectionModel->isSelected(index);
4332 if ((shiftKeyPressed || controlKeyPressed) && rightButtonPressed)
4333 return QItemSelectionModel::NoUpdate;
4334 if (!shiftKeyPressed && !controlKeyPressed && indexIsSelected)
4335 return QItemSelectionModel::NoUpdate;
4336 if (!index.isValid() && !rightButtonPressed && !shiftKeyPressed && !controlKeyPressed)
4337 return QItemSelectionModel::Clear;
4338 if (!index.isValid())
4339 return QItemSelectionModel::NoUpdate;
4341 if (controlKeyPressed && !rightButtonPressed && pressedAlreadySelected
4342#if QT_CONFIG(draganddrop)
4343 && dragEnabled && isIndexDragEnabled(index)
4346 return QItemSelectionModel::NoUpdate;
4350 case QEvent::MouseButtonRelease: {
4352 const Qt::MouseButton button =
static_cast<
const QMouseEvent*>(event)->button();
4353 const bool rightButtonPressed = button & Qt::RightButton;
4354 const bool shiftKeyPressed = modifiers & Qt::ShiftModifier;
4355 const bool controlKeyPressed = modifiers & Qt::ControlModifier;
4356 if (((index == pressedIndex && selectionModel->isSelected(index))
4357 || !index.isValid()) && state != QAbstractItemView::DragSelectingState
4358 && !shiftKeyPressed && !controlKeyPressed && (!rightButtonPressed || !index.isValid()))
4359 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();
4360 if (index == pressedIndex && controlKeyPressed && !rightButtonPressed
4361#if QT_CONFIG(draganddrop)
4362 && dragEnabled && isIndexDragEnabled(index)
4367 return QItemSelectionModel::NoUpdate;
4369 case QEvent::KeyPress: {
4371 switch (
static_cast<
const QKeyEvent*>(event)->key()) {
4372 case Qt::Key_Backtab:
4373 modifiers = modifiers & ~Qt::ShiftModifier;
4381 case Qt::Key_PageUp:
4382 case Qt::Key_PageDown:
4384 if (modifiers & Qt::ControlModifier
4385#ifdef QT_KEYPAD_NAVIGATION
4387 || QApplication::navigationMode() == Qt::NavigationModeKeypadTabOrder
4390 return QItemSelectionModel::NoUpdate;
4392 case Qt::Key_Select:
4393 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4395 if (modifiers & Qt::ControlModifier)
4396 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4397 return QItemSelectionModel::Select|selectionBehaviorFlags();
4408 if (modifiers & Qt::ShiftModifier)
4409 return QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4410 if (modifiers & Qt::ControlModifier)
4411 return QItemSelectionModel::Toggle|selectionBehaviorFlags();
4412 if (state == QAbstractItemView::DragSelectingState) {
4414 return QItemSelectionModel::Clear|QItemSelectionModel::SelectCurrent|selectionBehaviorFlags();
4417 return QItemSelectionModel::ClearAndSelect|selectionBehaviorFlags();