📄 qtableview.cpp
字号:
} else if (verticalMoved) { int top = d->visualRow(tl.row()); int bottom = d->visualRow(br.row()); for (int visual = top; visual <= bottom; ++visual) { int row = d->logicalRow(visual); QModelIndex topLeft = d->model->index(row, tl.column(), d->root); QModelIndex bottomRight = d->model->index(row, br.column(), d->root); selection.append(QItemSelectionRange(topLeft, bottomRight)); } } else { // nothing moved selection.append(QItemSelectionRange(tl, br)); } d->selectionModel->select(selection, command);}/*! \internal Returns the rectangle from the viewport of the items in the given \a selection.*/QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) const{ Q_D(const QTableView); if (selection.isEmpty()) return QRegion(); QRegion selectionRegion; bool verticalMoved = verticalHeader()->sectionsMoved(); bool horizontalMoved = horizontalHeader()->sectionsMoved(); if ((verticalMoved && horizontalMoved) || d->hasSpans()) { for (int i = 0; i < selection.count(); ++i) { QItemSelectionRange range = selection.at(i); if (range.parent() != d->root || !range.isValid()) continue; for (int r = range.top(); r <= range.bottom(); ++r) for (int c = range.left(); c <= range.right(); ++c) selectionRegion += QRegion(visualRect(d->model->index(r, c, d->root))); } } else if (horizontalMoved) { for (int i = 0; i < selection.count(); ++i) { QItemSelectionRange range = selection.at(i); if (range.parent() != d->root || !range.isValid()) continue; int top = rowViewportPosition(range.top()); int bottom = rowViewportPosition(range.bottom()) + rowHeight(range.bottom()); if (top > bottom) qSwap<int>(top, bottom); int height = bottom - top; for (int c = range.left(); c <= range.right(); ++c) selectionRegion += QRegion(QRect(columnViewportPosition(c), top, columnWidth(c), height)); } } else if (verticalMoved) { for (int i = 0; i < selection.count(); ++i) { QItemSelectionRange range = selection.at(i); if (range.parent() != d->root || !range.isValid()) continue; int left = columnViewportPosition(range.left()); int right = columnViewportPosition(range.right()) + columnWidth(range.right()); if (left > right) qSwap<int>(left, right); int width = right - left; for (int r = range.top(); r <= range.bottom(); ++r) selectionRegion += QRegion(QRect(left, rowViewportPosition(r), width, rowHeight(r))); } } else { // nothing moved for (int i = 0; i < selection.count(); ++i) { QItemSelectionRange range = selection.at(i); if (range.parent() != d->root || !range.isValid()) continue; d->trimHiddenSelections(&range); QRect tl = visualRect(range.topLeft()); QRect br = visualRect(range.bottomRight()); selectionRegion += QRegion(tl|br); } } return selectionRegion;}/*! \reimp*/QModelIndexList QTableView::selectedIndexes() const{ Q_D(const QTableView); QModelIndexList viewSelected; QModelIndexList modelSelected; if (d->selectionModel) modelSelected = d->selectionModel->selectedIndexes(); for (int i = 0; i < modelSelected.count(); ++i) { QModelIndex index = modelSelected.at(i); if (!isIndexHidden(index) && index.parent() == d->root) viewSelected.append(index); } return viewSelected;}/*! This slot is called whenever rows are added or deleted. The previous number of rows is specified by \a oldCount, and the new number of rows is specified by \a newCount.*/void QTableView::rowCountChanged(int /*oldCount*/, int /*newCount*/ ){ Q_D(QTableView); updateGeometries(); if (verticalScrollMode() == QAbstractItemView::ScrollPerItem) d->verticalHeader->setOffsetToSectionPosition(verticalScrollBar()->value()); else d->verticalHeader->setOffset(verticalScrollBar()->value()); d->viewport->update();}/*! This slot is called whenever columns are added or deleted. The previous number of columns is specified by \a oldCount, and the new number of columns is specified by \a newCount.*/void QTableView::columnCountChanged(int, int){ Q_D(QTableView); updateGeometries(); if (horizontalScrollMode() == QAbstractItemView::ScrollPerItem) d->horizontalHeader->setOffsetToSectionPosition(horizontalScrollBar()->value()); else d->horizontalHeader->setOffset(horizontalScrollBar()->value()); d->viewport->update();}/*! \internal*/void QTableView::updateGeometries(){ Q_D(QTableView); if (d->geometryRecursionBlock) return; d->geometryRecursionBlock = true; int width = 0; if (!d->verticalHeader->isHidden()) { width = qMax(d->verticalHeader->minimumWidth(), d->verticalHeader->sizeHint().width()); width = qMin(width, d->verticalHeader->maximumWidth()); } int height = 0; if (!d->horizontalHeader->isHidden()) { height = qMax(d->horizontalHeader->minimumHeight(), d->horizontalHeader->sizeHint().height()); height = qMin(height, d->horizontalHeader->maximumHeight()); } bool reverse = isRightToLeft(); if (reverse) setViewportMargins(0, height, width, 0); else setViewportMargins(width, height, 0, 0); // update headers QRect vg = d->viewport->geometry(); int verticalLeft = reverse ? vg.right() + 1 : (vg.left() - width); d->verticalHeader->setGeometry(verticalLeft, vg.top(), width, vg.height()); if (d->verticalHeader->isHidden()) QMetaObject::invokeMethod(d->verticalHeader, "updateGeometries"); int horizontalTop = vg.top() - height; d->horizontalHeader->setGeometry(vg.left(), horizontalTop, vg.width(), height); if (d->horizontalHeader->isHidden()) QMetaObject::invokeMethod(d->horizontalHeader, "updateGeometries"); // update cornerWidget if (d->horizontalHeader->isHidden() || d->verticalHeader->isHidden()) d->cornerWidget->setHidden(true); else d->cornerWidget->setGeometry(verticalLeft, horizontalTop, width, height); // update scroll bars // ### move this block into the if QSize vsize = d->viewport->size(); QSize max = maximumViewportSize(); uint horizontalLength = d->horizontalHeader->length(); uint verticalLength = d->verticalHeader->length(); if ((uint)max.width() >= horizontalLength && (uint)max.height() >= verticalLength) vsize = max; // horizontal scroll bar if (horizontalScrollMode() == QAbstractItemView::ScrollPerItem) { const int columnCount = d->horizontalHeader->count(); const int viewportWidth = vsize.width(); int columnsInViewport = 0; for (int width = 0, column = columnCount - 1; column >= 0; --column) { int logical = d->horizontalHeader->logicalIndex(column); if (!d->horizontalHeader->isSectionHidden(logical)) { width += d->horizontalHeader->sectionSize(logical); if (width > viewportWidth) break; ++columnsInViewport; } } const int visibleColumns = columnCount - d->horizontalHeader->hiddenSectionCount(); horizontalScrollBar()->setRange(0, visibleColumns - columnsInViewport); horizontalScrollBar()->setPageStep(columnsInViewport); if (columnsInViewport >= visibleColumns) d->horizontalHeader->setOffset(0); } else { // ScrollPerPixel horizontalScrollBar()->setPageStep(vsize.width()); horizontalScrollBar()->setRange(0, horizontalLength - vsize.width()); } // vertical scroll bar if (verticalScrollMode() == QAbstractItemView::ScrollPerItem) { const int rowCount = d->verticalHeader->count(); const int viewportHeight = vsize.height(); int rowsInViewport = 0; for (int height = 0, row = rowCount - 1; row >= 0; --row) { int logical = d->verticalHeader->logicalIndex(row); if (!d->verticalHeader->isSectionHidden(logical)) { height += d->verticalHeader->sectionSize(logical); if (height > viewportHeight) break; ++rowsInViewport; } } const int visibleRows = rowCount - d->verticalHeader->hiddenSectionCount(); verticalScrollBar()->setRange(0, visibleRows - rowsInViewport); verticalScrollBar()->setPageStep(rowsInViewport); if (rowsInViewport >= visibleRows) d->verticalHeader->setOffset(0); } else { // ScrollPerPixel verticalScrollBar()->setPageStep(vsize.height()); verticalScrollBar()->setRange(0, verticalLength - vsize.height()); } d->geometryRecursionBlock = false; QAbstractItemView::updateGeometries();}/*! Returns the size hint for the given \a row's height or -1 if there is no model. If you need to set the height of a given row to a fixed value, call QHeaderView::resizeSection() on the table's vertical header. If you reimplement this function in a subclass, note that the value you return is only used when resizeRowToContents() is called. In that case, if a larger row height is required by either the vertical header or the item delegate, that width will be used instead. \sa QWidget::sizeHint, verticalHeader()*/int QTableView::sizeHintForRow(int row) const{ Q_D(const QTableView); if (!model()) return -1; int left = qMax(0, columnAt(0)); int right = columnAt(d->viewport->width()); if (right == -1) // the table don't have enough columns to fill the viewport right = d->model->columnCount(d->root) - 1; QStyleOptionViewItemV3 option = d->viewOptionsV3(); int hint = 0; QModelIndex index; for (int column = left; column <= right; ++column) { int logicalColumn = d->horizontalHeader->logicalIndex(column); if (d->horizontalHeader->isSectionHidden(logicalColumn)) continue; index = d->model->index(row, logicalColumn, d->root); if (d->wrapItemText) {// for wrapping boundaries option.rect.setY(rowViewportPosition(index.row())); option.rect.setHeight(rowHeight(index.row())); option.rect.setX(columnViewportPosition(index.column())); option.rect.setWidth(columnWidth(index.column())); } hint = qMax(hint, itemDelegate(index)->sizeHint(option, index).height()); } return d->showGrid ? hint + 1 : hint;}/*! Returns the size hint for the given \a column's width or -1 if there is no model. If you need to set the width of a given column to a fixed value, call QHeaderView::resizeSection() on the table's horizontal header. If you reimplement this function in a subclass, note that the value you return is only used when resizeColumnToContents() is called. In that case, if a larger column width is required by either the horizontal header or the item delegate, that width will be used instead. \sa QWidget::sizeHint, horizontalHeader()*/int QTableView::sizeHintForColumn(int column) const{ Q_D(const QTableView); if (!model()) return -1; int top = qMax(0, rowAt(0)); int bottom = rowAt(d->viewport->height()); if (!isVisible() || bottom == -1) // the table don't have enough rows to fill the viewport bottom = d->model->rowCount(d->root) - 1; QStyleOptionViewItemV3 option = d->viewOptionsV3(); int hint = 0; QModelIndex index; for (int row = top; row <= bottom; ++row) { int logicalRow = d->verticalHeader->logicalIndex(row); if (d->verticalHeader->isSectionHidden(logicalRow)) continue; index = d->model->index(logicalRow, column, d->root); hint = qMax(hint, itemDelegate(index)->sizeHint(option, index).width()); } return d->showGrid ? hint + 1 : hint;}/*! Returns the y-coordinate in contents coordinates of the given \a row.*/int QTableView::rowViewportPosition(int row) const{ Q_D(const QTableView); return d->verticalHeader->sectionViewportPosition(row);}/*! Returns the row in which the given y-coordinate, \a y, in contents coordinates is located. \sa columnAt()*/int QTableView::rowAt(int y) const{ Q_D(const QTableView); return d->verticalHeader->logicalIndexAt(y);}/*! \since 4.1 Sets the height of the given \a row to be \a height.*/void QTableView::setRowHeight(int row, int height){ Q_D(const QTableView); d->verticalHeader->resizeSection(row, height);}/*! Returns the height of the given \a row. \sa resizeRowToContents(), columnWidth()*/int QTableView::rowHeight(int row) const{ Q_D(const QTableView); return d->verticalHeader->sectionSize(row);}/*! Returns the x-coordinate in contents coordinates of the given \a column.*/int QTableView::columnViewportPosition(int column) const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -