📄 qabstractitemview.cpp
字号:
By default, this property is false.*/void QAbstractItemView::setAlternatingRowColors(bool enable){ d_func()->alternatingColors = enable; if (isVisible()) d_func()->viewport->update();}bool QAbstractItemView::alternatingRowColors() const{ return d_func()->alternatingColors;}/*! \property QAbstractItemView::iconSize \brief the size of items Setting this property when the view is visible will cause the items to be laid out again.*/void QAbstractItemView::setIconSize(const QSize &size){ d_func()->iconSize = size; d_func()->doDelayedItemsLayout();}QSize QAbstractItemView::iconSize() const{ return d_func()->iconSize;}/*! \property QAbstractItemView::textElideMode \brief the the position of the "..." in elided text.*/void QAbstractItemView::setTextElideMode(Qt::TextElideMode mode){ d_func()->textElideMode = mode;}Qt::TextElideMode QAbstractItemView::textElideMode() const{ return d_func()->textElideMode;}/*! \reimp*/bool QAbstractItemView::event(QEvent *event){ if (event->type() == QEvent::KeyPress && d_func()->tabKeyNavigation) { QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); if (keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Backtab) { keyPressEvent(keyEvent); return keyEvent->isAccepted(); } } else if (event->type() == QEvent::InputMethod) { if (!edit(currentIndex(), AnyKeyPressed, event)) event->ignore(); return true; } return QAbstractScrollArea::event(event);}/*! \fn bool QAbstractItemView::viewportEvent(QEvent *event) This function is used to handle tool tips, and What's This? mode, if the given \a event is a QEvent::ToolTip,or a QEvent::WhatsThis. It passes all other events on to its base class viewportEvent() handler.*/bool QAbstractItemView::viewportEvent(QEvent *event){ Q_D(QAbstractItemView); switch (event->type()) { case QEvent::HoverEnter: { QHoverEvent *he = static_cast<QHoverEvent*>(event); d->hover = indexAt(he->pos()); d->viewport->update(visualRect(d->hover)); break; } case QEvent::HoverLeave: { d->viewport->update(visualRect(d->hover)); // update old d->hover = QModelIndex(); break; } case QEvent::HoverMove: { QHoverEvent *he = static_cast<QHoverEvent*>(event); QModelIndex old = d->hover; d->hover = indexAt(he->pos()); if (d->hover != old) d->viewport->update(visualRect(old)|visualRect(d->hover)); break; }#ifndef QT_NO_TOOLTIP case QEvent::ToolTip: { if (!isActiveWindow()) break; QHelpEvent *he = static_cast<QHelpEvent*>(event); QModelIndex index = indexAt(he->pos()); if (index.isValid()) { QString tooltip = model()->data(index, Qt::ToolTipRole).toString(); QToolTip::showText(he->globalPos(), tooltip, this); return true; } else { QString emptyString; QToolTip::showText(he->globalPos(), emptyString, this); } break;}#endif#ifndef QT_NO_WHATSTHIS case QEvent::QueryWhatsThis: { QHelpEvent *he = static_cast<QHelpEvent*>(event); QModelIndex index = indexAt(he->pos()); if (index.isValid() && model()->data(index, Qt::WhatsThisRole).isValid()) return true; break ; } case QEvent::WhatsThis: { QHelpEvent *he = static_cast<QHelpEvent*>(event); QModelIndex index = indexAt(he->pos()); if (index.isValid()) { QString whatsthis = model()->data(index, Qt::WhatsThisRole).toString(); QWhatsThis::showText(he->globalPos(), whatsthis, this); return true; } break ; }#endif case QEvent::FontChange: d->doDelayedItemsLayout(); // the size of the items will change break; case QEvent::WindowActivate: case QEvent::WindowDeactivate: d->viewport->update(); break; default: break; } return QAbstractScrollArea::viewportEvent(event);}/*! This function is called with the given \a event when a mouse button is pressed while the cursor is inside the widget. If a valid item is pressed on it is made into the current item. This function emits the pressed() signal.*/void QAbstractItemView::mousePressEvent(QMouseEvent *event){ Q_D(QAbstractItemView); QPoint pos = event->pos(); QModelIndex idx = indexAt(pos); if (!selectionModel() || (d->state == EditingState && d->hasEditor(idx))) return; QPersistentModelIndex index = idx; bool alreadySelected = selectionModel()->isSelected(index); d->pressedIndex = index; d->pressedModifiers = event->modifiers(); QItemSelectionModel::SelectionFlags command = selectionCommand(index, event); QPoint offset = d->offset(); if ((command & QItemSelectionModel::Current) == 0) d->pressedPosition = pos + offset; if (d->pressedPosition == QPoint(-1, -1)) d->pressedPosition = visualRect(selectionModel()->currentIndex()).center() + offset; QRect rect(d->pressedPosition - offset, pos); setSelection(rect.normalized(), command); if (index.isValid()) selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate); // signal handlers may change the model if (index.isValid()) { emit pressed(index); if (alreadySelected) edit(index, SelectedClicked, event); }}/*! This function is called with the given \a event when a mouse move event is sent to the widget. If a selection is in progress and new items are moved over the selection is extended; if a drag is in progress it is continued.*/void QAbstractItemView::mouseMoveEvent(QMouseEvent *event){ Q_D(QAbstractItemView); QPoint topLeft; QPoint bottomRight = event->pos(); if (state() == ExpandingState || state() == CollapsingState) return;#ifndef QT_NO_DRAGANDDROP if (state() == DraggingState) { topLeft = d->pressedPosition - d->offset(); if ((topLeft - bottomRight).manhattanLength() > QApplication::startDragDistance()) { startDrag(model()->supportedDropActions()); setState(NoState); // the startDrag will return when the dnd operation is done stopAutoScroll(); } return; }#endif // QT_NO_DRAGANDDROP if (d->selectionMode != SingleSelection) topLeft = d->pressedPosition - d->offset(); else topLeft = bottomRight; QModelIndex index = indexAt(bottomRight); QModelIndex buddy = model() ? model()->buddy(d->pressedIndex) : QModelIndex(); if (state() == EditingState && d->hasEditor(buddy)) return; if (d->enteredIndex != index) { // signal handlers may change the model QPersistentModelIndex persistent = index; if (persistent.isValid()) { emit entered(persistent);#ifndef QT_NO_STATUSTIP QString statustip = model()->data(persistent, Qt::StatusTipRole).toString(); if (parent() && !statustip.isEmpty()) { QStatusTipEvent tip(statustip); QApplication::sendEvent(parent(), &tip); }#endif } else {#ifndef QT_NO_STATUSTIP if (parent()) { QString emptyString; QStatusTipEvent tip(emptyString); QApplication::sendEvent(parent(), &tip); }#endif emit viewportEntered(); } d->enteredIndex = persistent; index = persistent; }#ifndef QT_NO_DRAGANDDROP if (index.isValid() && d->dragEnabled && state() != DragSelectingState) { bool dragging = model()->flags(index) & Qt::ItemIsDragEnabled; bool selected = selectionModel()->isSelected(index); if (dragging && selected) { setState(DraggingState); return; } }#endif if ((event->buttons() & Qt::LeftButton) && d->selectionAllowed(index) && selectionModel()) { setState(DragSelectingState); QItemSelectionModel::SelectionFlags command = selectionCommand(index, event); // Do the normalize ourselves, since QRect::normalized() is flawed if (topLeft.y() > bottomRight.y()) qSwap(topLeft.ry(), bottomRight.ry()); if (topLeft.x() > bottomRight.x()) qSwap(topLeft.rx(), bottomRight.rx()); QRect selectionRect = QRect(topLeft, bottomRight); QPersistentModelIndex persistent = index; setSelection(selectionRect, command); // set at the end because it might scroll the view if (persistent.isValid()) selectionModel()->setCurrentIndex(persistent, QItemSelectionModel::NoUpdate); }}/*! This function is called with the given \a event when a mouse button is released while the cursor is inside the widget. It will emit the clicked() signal if an item was being pressed.*/void QAbstractItemView::mouseReleaseEvent(QMouseEvent *event){ Q_D(QAbstractItemView); d->pressedPosition = QPoint(-1, -1); if (state() == EditingState) return; QPoint pos = event->pos(); QPersistentModelIndex index = indexAt(pos); setState(NoState); if (selectionModel()) selectionModel()->select(index, selectionCommand(index, event)); if (index == d_func()->pressedIndex && index.isValid()) { // signal handlers may change the model bool edited = edit(index, NoEditTriggers, event); // send event to delegate emit clicked(index); if (edited) // if the delegate handled the click, the item is not activated return; if (style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick)) emit activated(index); }}/*! This function is called with the given \a event when a mouse button is double clicked inside the widget. If the double-click is on a valid item it emits the doubleClicked() signal and calls edit() on the item.*/void QAbstractItemView::mouseDoubleClickEvent(QMouseEvent *event){ QModelIndex index = indexAt(event->pos()); if (!index.isValid()) return; // signal handlers may change the model QPersistentModelIndex persistent = index; emit doubleClicked(persistent); if (!edit(persistent, DoubleClicked, event) && !style()->styleHint(QStyle::SH_ItemView_ActivateItemOnSingleClick)) 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 (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); // the ignore by default event->ignore(); if (!model()) return; QModelIndex index = indexAt(event->pos()); if (d->canDecode(event)) { if (index.isValid() && d->showDropIndicator) { QRect rect = visualRect(index); d->dropIndicatorPosition = d->position(event->pos(), rect, 2); switch (d->dropIndicatorPosition) { case AboveItem: d->dropIndicatorRect = QRect(rect.left(), rect.top(), rect.width(), 0); event->accept(); break; case BelowItem: d->dropIndicatorRect = QRect(rect.left(), rect.bottom(), rect.width(), 0); event->accept(); break; case OnItem: if (model()->flags(index) & Qt::ItemIsDropEnabled) { d->dropIndicatorRect = rect; event->accept(); } break; case OnViewport: break; } d->viewport->update(); } else { d->dropIndicatorRect = QRect(); d->dropIndicatorPosition = QAbstractItemView::OnViewport; event->accept(); // allow dropping in empty areas } } // can decode if (d->shouldAutoScroll(event->pos())) startAutoScroll();}/*! \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 *){ stopAutoScroll(); setState(NoState); d_func()->viewport->update();}/*! This function is called with the given \a event when a drop event occurs over the widget. If there's a valid item under the mouse pointer when the drop occurs, the drop event is accepted; otherwise it is ignored. \sa startDrag()*/void QAbstractItemView::dropEvent(QDropEvent *event){ Q_D(QAbstractItemView);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -