📄 qabstractitemview.cpp
字号:
QModelIndex index; // if we drop on the viewport if (d->viewport->rect().contains(event->pos())) { index = indexAt(event->pos()); if (!index.isValid()) index = rootIndex(); // drop on viewport } // if we are allowed to do the drop if (model()->supportedDropActions() & event->proposedAction()) { int row = -1; int col = -1; if (index.isValid() && (model()->flags(index) & Qt::ItemIsDropEnabled || model()->flags(index.parent()) & Qt::ItemIsDropEnabled)) { d->dropIndicatorPosition = d->position(event->pos(), visualRect(index), 2); switch (d->dropIndicatorPosition) { case AboveItem: row = index.row(); col = index.column(); index = index.parent(); break; case BelowItem: row = index.row() + 1; col = index.column(); index = index.parent(); break; case OnItem: case OnViewport: break; } } else { d->dropIndicatorPosition = QAbstractItemView::OnViewport; } if (model()->dropMimeData(event->mimeData(), event->proposedAction(), row, col, index)) event->acceptProposedAction(); } stopAutoScroll(); setState(NoState); d->viewport->update();}#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){ QAbstractScrollArea::focusInEvent(event); QModelIndex index = currentIndex(); if (index.isValid()) d_func()->viewport->update(visualRect(index));}/*! This function is called with the given \a event when the widget obtains the focus. By default, the event is ignored. \sa clearFocus(), focusInEvent()*/void QAbstractItemView::focusOutEvent(QFocusEvent *event){ QAbstractScrollArea::focusOutEvent(event); QModelIndex index = currentIndex(); if (index.isValid()) d_func()->viewport->update(visualRect(index));}/*! 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, and emits the returnPressed(), spacePressed(), and deletePressed() signals if the associated key is pressed. This function is where editing is initiated by key press, e.g. if F2 is pressed. \sa edit()*/void QAbstractItemView::keyPressEvent(QKeyEvent *event){ Q_D(QAbstractItemView); if (!model()) return;#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 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: newCurrent = moveCursor(MoveNext, event->modifiers()); break; case Qt::Key_Backtab: newCurrent = moveCursor(MovePrevious, event->modifiers()); break; } QPersistentModelIndex oldCurrent = currentIndex(); if (newCurrent != oldCurrent && newCurrent.isValid()) { QItemSelectionModel::SelectionFlags command = selectionCommand(newCurrent, event); if (command & QItemSelectionModel::Current) { selectionModel()->setCurrentIndex(newCurrent, QItemSelectionModel::NoUpdate); if (d->pressedPosition == QPoint(-1, -1)) d->pressedPosition = visualRect(oldCurrent).center(); QRect rect(d->pressedPosition - d->offset(), visualRect(newCurrent).center()); setSelection(rect.normalized(), command); } else { selectionModel()->setCurrentIndex(newCurrent, command); d->pressedPosition = visualRect(newCurrent).center() + d->offset(); } return; } switch (event->key()) { // ignored keys case Qt::Key_Down: case Qt::Key_Up:#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled()) { event->accept(); // don't change focus break; }#endif case Qt::Key_Left: case Qt::Key_Right: case Qt::Key_Home: case Qt::Key_End: case Qt::Key_PageUp: case Qt::Key_PageDown: case Qt::Key_Escape: case Qt::Key_Shift: case Qt::Key_Control: event->ignore(); break; case Qt::Key_Space: selectionModel()->select(currentIndex(), selectionCommand(currentIndex(), event)); break;#ifdef Q_WS_MAC case Qt::Key_Enter: case Qt::Key_Return: if (!edit(currentIndex(), EditKeyPressed, event)) event->ignore(); break; case Qt::Key_O: if (event->modifiers() & Qt::ControlModifier && currentIndex().isValid()) emit activated(currentIndex()); break;#else case Qt::Key_F2: if (!edit(currentIndex(), EditKeyPressed, event)) event->ignore(); break; case Qt::Key_Enter: case Qt::Key_Return: case Qt::Key_Select: if (currentIndex().isValid()) emit activated(currentIndex()); break;#endif case Qt::Key_A: if (event->modifiers() & Qt::ControlModifier) { SelectionMode mode = selectionMode(); if (mode == MultiSelection || mode == ExtendedSelection) selectAll(); break; } default: { bool modified = (event->modifiers() == Qt::ControlModifier) || (event->modifiers() == Qt::AltModifier) || (event->modifiers() == Qt::MetaModifier); if (!event->text().isEmpty() && !modified) { if (!edit(currentIndex(), AnyKeyPressed, event)) keyboardSearch(event->text()); } event->ignore(); break; } }}/*! This function is called with the given \a event when a resize event is sent to the widget. \sa QWidget::resizeEvent()*/void QAbstractItemView::resizeEvent(QResizeEvent *event){ QAbstractScrollArea::resizeEvent(event); updateGeometries();}/*! This function is called with the given \a event when a timer event is sent to the widget. \sa QObject::timerEvent()*/void QAbstractItemView::timerEvent(QTimerEvent *event){ Q_D(QAbstractItemView); if (event->timerId() == d->autoScrollTimer.timerId()) doAutoScroll(); else if (event->timerId() == d->updateTimer.timerId()) d->updateDirtyRegion(); else if (event->timerId() == d->delayedEditing.timerId()) { d->delayedEditing.stop(); d->openEditor(currentIndex(), 0); } else if (event->timerId() == d->delayedLayout.timerId()) { d->delayedLayout.stop(); doItemsLayout(); }}#ifndef QT_NO_DRAGANDDROP/*! \enum QAbstractItemView::DropIndicatorPosition This enum indicates the position of the drop indicator in relation to the index at the current mouse position: \value OnItem The item will be dropped on the index. \value AboveItem The item will be dropped above the index. \value BelowItem The item will be dropped below the index. \value OnViewport The item will be dropped onto a region of the viewport withno items. The way each view handles items dropped onto the viewport depends onthe behavior of the underlying model in use.*//*! \since 4.1 Returns the position of the drop indicator in relation to the closest item.*/QAbstractItemView::DropIndicatorPosition QAbstractItemView::dropIndicatorPosition() const{ Q_D(const QAbstractItemView); return d->dropIndicatorPosition;}#endif/*! This convenience function returns a list of all selected and non-hidden item indexes in the view. The list contains no duplicates, and is not sorted. The default implementation does nothing.*/QModelIndexList QAbstractItemView::selectedIndexes() const{ return QModelIndexList();}/*! Starts editing the item at \a index, creating an editor if necessary, and returns true if the view's \l{State} is now EditingState; otherwise returns false. The action that caused the editing process is described by \a trigger, and the associated event is specified by \a event. \sa closeEditor()*/bool QAbstractItemView::edit(const QModelIndex &index, EditTrigger trigger, QEvent *event){ Q_D(QAbstractItemView); if (!model() || !index.isValid()) return false; if (trigger == DoubleClicked) d->delayedEditing.stop(); if (d->sendDelegateEvent(index, event)) return true; if (!d->shouldEdit(trigger, model()->buddy(index))) return false; if (trigger == SelectedClicked) // we may get a double click event later d->delayedEditing.start(QApplication::doubleClickInterval() + 100, this); else d->openEditor(index, d->shouldForwardEvent(trigger, event) ? event : 0); return true;}/*! \internal*/void QAbstractItemView::updateEditorData(){ Q_D(QAbstractItemView); _q_abstractitemview_editor_iterator it = d->editors.begin(); for (; it != d->editors.end(); ++it) { if (d->editorForIterator(it) && d->indexForIterator(it).isValid()) itemDelegate()->setEditorData(d->editorForIterator(it), d->indexForIterator(it)); }}/*! \internal*/void QAbstractItemView::updateEditorGeometries(){ Q_D(QAbstractItemView); QStyleOptionViewItem option = viewOptions(); _q_abstractitemview_editor_iterator it = d->editors.begin(); while (it != d->editors.end()) { QModelIndex index = d->indexForIterator(it); QWidget *editor = d->editorForIterator(it); if (index.isValid() && editor) { option.rect = visualRect(index); if (option.rect.isValid()) { editor->show(); itemDelegate()->updateEditorGeometry(editor, option, index); } else { editor->hide(); } ++it; } else { d->releaseEditor(editor); it = d->editors.erase(it); } }}/*! \internal*/void QAbstractItemView::updateGeometries(){ updateEditorGeometries(); d_func()->fetchMore();}/*! \internal*/void QAbstractItemView::verticalScrollbarValueChanged(int value){ if (verticalScrollBar()->maximum() == value && model()) model()->fetchMore(rootIndex());}/*! \internal*/void QAbstractItemView::horizontalScrollbarValueChanged(int value){ if (horizontalScrollBar()->maximum() == value && model()) model()->fetchMore(rootIndex());}/*! \internal*/void QAbstractItemView::verticalScrollbarAction(int){ //do nothing}/*! \internal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -