📄 qabstractitemview.cpp
字号:
{ Q_D(QAbstractItemView); QModelIndex index = indexAt(event->pos()); if (!index.isValid() || (d->pressedIndex != index)) { QMouseEvent me(QEvent::MouseButtonPress, event->pos(), event->button(), event->buttons(), event->modifiers()); mousePressEvent(&me); return; } // signal handlers may change the model QPersistentModelIndex persistent = index; emit doubleClicked(persistent); if ((event->button() & Qt::LeftButton) && !edit(persistent, DoubleClicked, event) && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick, 0, this)) emit activated(persistent);}#ifndef QT_NO_DRAGANDDROP/*! This function is called with the given \a event when a drag and drop operation enters the widget. If the drag is over a valid dropping place (e.g. over an item that accepts drops), the event is accepted; otherwise it is ignored. \sa dropEvent() startDrag()*/void QAbstractItemView::dragEnterEvent(QDragEnterEvent *event){ if (dragDropMode() == InternalMove && (event->source() != this|| !(event->possibleActions() & Qt::MoveAction))) return; if (d_func()->canDecode(event)) { event->accept(); setState(DraggingState); } else { event->ignore(); }}/*! This function is called continuously with the given \a event during a drag and drop operation over the widget. It can cause the view to scroll if, for example, the user drags a selection to view's right or bottom edge. In this case, the event will be accepted; otherwise it will be ignored. \sa dropEvent() startDrag()*/void QAbstractItemView::dragMoveEvent(QDragMoveEvent *event){ Q_D(QAbstractItemView); if (dragDropMode() == InternalMove && (event->source() != this || !(event->possibleActions() & Qt::MoveAction))) return; // ignore by default event->ignore(); QModelIndex index = indexAt(event->pos()); if (!d->droppingOnItself(event, index) && d->canDecode(event)) { Qt::DropAction dropAction = (d->model->supportedDropActions() & event->proposedAction()) ? event->proposedAction() : Qt::IgnoreAction; if (index.isValid() && d->showDropIndicator) { QRect rect = visualRect(index); d->dropIndicatorPosition = d->position(event->pos(), rect, index); switch (d->dropIndicatorPosition) { case AboveItem: if (d->model->flags(index.parent()) & Qt::ItemIsDropEnabled) { d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0); event->setDropAction(dropAction); event->accept(); } else { d->dropIndicatorRect = QRect(); } break; case BelowItem: if (d->model->flags(index.parent()) & Qt::ItemIsDropEnabled) { d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0); event->setDropAction(dropAction); event->accept(); } else { d->dropIndicatorRect = QRect(); } break; case OnItem: if (d->model->flags(index) & Qt::ItemIsDropEnabled) { d->dropIndicatorRect = rect; event->setDropAction(dropAction); event->accept(); } else { d->dropIndicatorRect = QRect(); } break; case OnViewport: d->dropIndicatorRect = QRect(); if (d->model->flags(rootIndex()) & Qt::ItemIsDropEnabled) { event->setDropAction(dropAction); event->accept(); // allow dropping in empty areas } break; } } else { d->dropIndicatorRect = QRect(); d->dropIndicatorPosition = OnViewport; if (d->model->flags(rootIndex()) & Qt::ItemIsDropEnabled) { event->setDropAction(dropAction); event->accept(); // allow dropping in empty areas } } d->viewport->update(); } // can decode if (d->shouldAutoScroll(event->pos())) startAutoScroll();}/*! \internal Return true if this is a move from ourself and \a index is a child of the selection that is being moved. */bool QAbstractItemViewPrivate::droppingOnItself(QDropEvent *event, const QModelIndex &index){ Q_Q(QAbstractItemView); Qt::DropAction dropAction = event->dropAction(); if (q->dragDropMode() == QAbstractItemView::InternalMove) dropAction = Qt::MoveAction; if (event->source() == q && event->possibleActions() & Qt::MoveAction && dropAction == Qt::MoveAction) { QModelIndexList selectedIndexes = q->selectedIndexes(); QModelIndex child = index; while (child.isValid() && child != root) { if (selectedIndexes.contains(child)) return true; child = child.parent(); } } return false;}/*! \fn void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *event) This function is called when the item being dragged leaves the view. The \a event describes the state of the drag and drop operation.*/void QAbstractItemView::dragLeaveEvent(QDragLeaveEvent *){ Q_D(QAbstractItemView); stopAutoScroll(); setState(NoState); d->viewport->update();}/*! This function is called with the given \a event when a drop event occurs over the widget. If the model accepts the even position the drop event is accepted; otherwise it is ignored. \sa startDrag()*/void QAbstractItemView::dropEvent(QDropEvent *event){ Q_D(QAbstractItemView); if (dragDropMode() == InternalMove) { if (event->source() != this || !(event->possibleActions() & Qt::MoveAction)) return; } QModelIndex index; int col = -1; int row = -1; if (d->dropOn(event, &row, &col, &index)) { if (d->model->dropMimeData(event->mimeData(), dragDropMode() == InternalMove ? Qt::MoveAction : event->proposedAction(), row, col, index)) { if (dragDropMode() == InternalMove) { event->setDropAction(Qt::MoveAction); event->accept(); } else { event->acceptProposedAction(); } } } stopAutoScroll(); setState(NoState); d->viewport->update();}/*! If the event hasn't already been accepted, determines the index to drop on. if (row == -1 && col == -1) // append to this drop index else // place at row, col in drop index If it returns true a drop can be done, and dropRow, dropCol and dropIndex reflects the position of the drop. \internal */bool QAbstractItemViewPrivate::dropOn(QDropEvent *event, int *dropRow, int *dropCol, QModelIndex *dropIndex){ Q_Q(QAbstractItemView); if (event->isAccepted()) return false; QModelIndex index; // rootIndex() (i.e. the viewport) might be a valid index if (viewport->rect().contains(event->pos())) { index = q->indexAt(event->pos()); if (!index.isValid() || !q->visualRect(index).contains(event->pos())) index = root; } // If we are allowed to do the drop if (model->supportedDropActions() & event->proposedAction()) { int row = -1; int col = -1; if (index != root) { dropIndicatorPosition = position(event->pos(), q->visualRect(index), index); switch (dropIndicatorPosition) { case QAbstractItemView::AboveItem: row = index.row(); col = index.column(); index = index.parent(); break; case QAbstractItemView::BelowItem: row = index.row() + 1; col = index.column(); index = index.parent(); break; case QAbstractItemView::OnItem: case QAbstractItemView::OnViewport: break; } } else { dropIndicatorPosition = QAbstractItemView::OnViewport; } *dropIndex = index; *dropRow = row; *dropCol = col; if (!droppingOnItself(event, index)) return true; } return false;}QAbstractItemView::DropIndicatorPositionQAbstractItemViewPrivate::position(const QPoint &pos, const QRect &rect, const QModelIndex &index) const{ QAbstractItemView::DropIndicatorPosition r = QAbstractItemView::OnViewport; if (!overwrite) { const int margin = 2; if (pos.y() - rect.top() < margin) { r = QAbstractItemView::AboveItem; } else if (rect.bottom() - pos.y() < margin) { r = QAbstractItemView::BelowItem; } else if (rect.contains(pos, true)) { r = QAbstractItemView::OnItem; } } else { QRect touchingRect = rect; touchingRect.adjust(-1, -1, 1, 1); if (touchingRect.contains(pos, false)) { r = QAbstractItemView::OnItem; } } if (r == QAbstractItemView::OnItem && (!(model->flags(index) & Qt::ItemIsDropEnabled))) r = pos.y() < rect.center().y() ? QAbstractItemView::AboveItem : QAbstractItemView::BelowItem; return r;}#endif // QT_NO_DRAGANDDROP/*! This function is called with the given \a event when the widget obtains the focus. By default, the event is ignored. \sa setFocus(), focusOutEvent()*/void QAbstractItemView::focusInEvent(QFocusEvent *event){ Q_D(QAbstractItemView); QAbstractScrollArea::focusInEvent(event); if (selectionModel() && !d->currentIndexSet && !currentIndex().isValid()) { bool autoScroll = d->autoScroll; d->autoScroll = false; selectionModel()->setCurrentIndex( moveCursor(MoveNext, Qt::NoModifier), // first visible index QItemSelectionModel::NoUpdate); d->autoScroll = autoScroll; } d->viewport->update();}/*! This function is called with the given \a event when the widget looses the focus. By default, the event is ignored. \sa clearFocus(), focusInEvent()*/void QAbstractItemView::focusOutEvent(QFocusEvent *event){ Q_D(QAbstractItemView); QAbstractScrollArea::focusOutEvent(event); d->viewport->update();}/*! This function is called with the given \a event when a key event is sent to the widget. The default implementation handles basic cursor movement, e.g. Up, Down, Left, Right, Home, PageUp, and PageDown; the activated() signal is emitted if the current index is valid and the activation key is pressed (e.g. Enter or Return, depending on the platform). This function is where editing is initiated by key press, e.g. if F2 is pressed. \sa edit(), moveCursor(), keyboardSearch(), tabKeyNavigation*/void QAbstractItemView::keyPressEvent(QKeyEvent *event){ Q_D(QAbstractItemView);#ifdef QT_KEYPAD_NAVIGATION switch (event->key()) { case Qt::Key_Select: if (QApplication::keypadNavigationEnabled()) { if (!hasEditFocus()) { setEditFocus(true); return; } } break; case Qt::Key_Back: if (QApplication::keypadNavigationEnabled() && hasEditFocus()) setEditFocus(false); else event->ignore(); return; default: if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { event->ignore(); return; } }#endif#if !defined(QT_NO_CLIPBOARD) && !defined(QT_NO_SHORTCUT) if (event == QKeySequence::Copy) { QVariant variant = model()->data(currentIndex(), Qt::DisplayRole); if (variant.type() == QVariant::String) QApplication::clipboard()->setText(variant.toString()); event->accept(); }#endif QPersistentModelIndex newCurrent; switch (event->key()) { case Qt::Key_Down: newCurrent = moveCursor(MoveDown, event->modifiers()); break; case Qt::Key_Up: newCurrent = moveCursor(MoveUp, event->modifiers()); break; case Qt::Key_Left: newCurrent = moveCursor(MoveLeft, event->modifiers()); break; case Qt::Key_Right: newCurrent = moveCursor(MoveRight, event->modifiers()); break; case Qt::Key_Home: newCurrent = moveCursor(MoveHome, event->modifiers()); break; case Qt::Key_End: newCurrent = moveCursor(MoveEnd, event->modifiers()); break; case Qt::Key_PageUp: newCurrent = moveCursor(MovePageUp, event->modifiers()); break; case Qt::Key_PageDown: newCurrent = moveCursor(MovePageDown, event->modifiers()); break; case Qt::Key_Tab: newCur
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -