📄 qtableview.cpp
字号:
}/*! \internal Returns the rectangle on the viewport occupied by the given \a index. If the index is hidden in the view it will return a null QRect.*/QRect QTableView::visualRect(const QModelIndex &index) const{ if (!index.isValid() ||index.parent() != rootIndex() || isIndexHidden(index) ) return QRect(); d_func()->executePostedLayout(); const int i = showGrid() ? 1 : 0; return QRect(columnViewportPosition(index.column()), rowViewportPosition(index.row()), columnWidth(index.column()) - i, rowHeight(index.row()) - i);}/*! \internal Makes sure that the given \a item is visible in the table view, scrolling if necessary.*/void QTableView::scrollTo(const QModelIndex &index, ScrollHint hint){ Q_D(QTableView); // check if we really need to do anything if (!index.isValid() || !model() || (model()->parent(index) != rootIndex()) || isIndexHidden(index)) return; // Adjust horizontal position int viewportWidth = d->viewport->width(); int horizontalOffset = d->horizontalHeader->offset(); int horizontalPosition = d->horizontalHeader->sectionPosition(index.column()); int cellWidth = d->horizontalHeader->sectionSize(index.column()); if (horizontalPosition - horizontalOffset < 0 || cellWidth > viewportWidth) horizontalScrollBar()->setValue(horizontalPosition); else if (horizontalPosition - horizontalOffset + cellWidth > viewportWidth) horizontalScrollBar()->setValue(horizontalPosition - viewportWidth + cellWidth); // Adjust vertical position int viewportHeight = d->viewport->height(); int verticalOffset = d->verticalHeader->offset(); int verticalPosition = d->verticalHeader->sectionPosition(index.row()); int cellHeight = d->verticalHeader->sectionSize(index.row()); if (verticalPosition - verticalOffset < 0 || cellHeight > viewportHeight) { if (hint == EnsureVisible) hint = PositionAtTop; } else if (verticalPosition - verticalOffset + cellHeight > viewportHeight) { if (hint == EnsureVisible) hint = PositionAtBottom; } if (hint == PositionAtTop) verticalScrollBar()->setValue(verticalPosition); else if (hint == PositionAtBottom) verticalScrollBar()->setValue(verticalPosition - viewportHeight + cellHeight); d->setDirtyRegion(visualRect(index));}/*! This slot is called to change the height of the given \a row. The old height is specified by \a oldHeight, and the new height by \a newHeight. \sa columnResized()*/void QTableView::rowResized(int row, int, int){ Q_D(QTableView); d->rowsToUpdate.append(row); if (d->rowResizeTimerID == 0) d->rowResizeTimerID = startTimer(0);}/*! This slot is called to change the width of the given \a column. The old width is specified by \a oldWidth, and the new width by \a newWidth. \sa rowResized()*/void QTableView::columnResized(int column, int, int){ Q_D(QTableView); d->columnsToUpdate.append(column); if (d->columnResizeTimerID == 0) d->columnResizeTimerID = startTimer(0);}/*! \reimp */void QTableView::timerEvent(QTimerEvent *event){ Q_D(QTableView); if (event->timerId() == d->columnResizeTimerID) { updateGeometries(); killTimer(d->columnResizeTimerID); d->columnResizeTimerID = 0; QRect rect; int viewportHeight = d->viewport->height(); int viewportWidth = d->viewport->width(); for (int i = d->columnsToUpdate.size()-1; i >= 0; --i) { int column = d->columnsToUpdate.at(i); int x = columnViewportPosition(column); if (isRightToLeft()) rect |= QRect(0, 0, x + columnWidth(column), viewportHeight); else rect |= QRect(x, 0, viewportWidth - x, viewportHeight); } d->viewport->update(rect.normalized()); d->columnsToUpdate.clear(); } if (event->timerId() == d->rowResizeTimerID) { updateGeometries(); killTimer(d->rowResizeTimerID); d->rowResizeTimerID = 0; int viewportHeight = d->viewport->height(); int viewportWidth = d->viewport->width(); int top = viewportHeight; for (int i = d->rowsToUpdate.size()-1; i >= 0; --i) { int y = rowViewportPosition(d->rowsToUpdate.at(i)); top = qMin(top, y); } d->viewport->update(QRect(0, top, viewportWidth, viewportHeight - top)); d->rowsToUpdate.clear(); } QAbstractItemView::timerEvent(event);}/*! This slot is called to change the index of the given \a row in the table view. The old index is specified by \a oldIndex, and the new index by \a newIndex. \sa columnMoved()*/void QTableView::rowMoved(int, int oldIndex, int newIndex){ Q_D(QTableView); int logicalOldIndex = d->verticalHeader->logicalIndex(oldIndex); int logicalNewIndex = d->verticalHeader->logicalIndex(newIndex); int oldTop = rowViewportPosition(logicalOldIndex); int newTop = rowViewportPosition(logicalNewIndex); int oldBottom = oldTop + rowHeight(logicalOldIndex); int newBottom = newTop + rowHeight(logicalNewIndex); int top = qMin(oldTop, newTop); int bottom = qMax(oldBottom, newBottom); int height = bottom - top; updateGeometries(); d->viewport->update(0, top, d->viewport->width(), height);}/*! This slot is called to change the index of the given \a column in the table view. The old index is specified by \a oldIndex, and the new index by \a newIndex. \sa rowMoved()*/void QTableView::columnMoved(int, int oldIndex, int newIndex){ Q_D(QTableView); int logicalOldIndex = d->horizontalHeader->logicalIndex(oldIndex); int logicalNewIndex = d->horizontalHeader->logicalIndex(newIndex); int oldLeft = columnViewportPosition(logicalOldIndex); int newLeft = columnViewportPosition(logicalNewIndex); int oldRight = oldLeft + columnWidth(logicalOldIndex); int newRight = newLeft + columnWidth(logicalNewIndex); int left = qMin(oldLeft, newLeft); int right = qMax(oldRight, newRight); int width = right - left; updateGeometries(); d->viewport->update(left, 0, width, d->viewport->height());}/*! Selects the given \a row in the table view if the current SelectionMode and SelectionBehavior allows rows to be selected. \sa selectColumn()*/void QTableView::selectRow(int row){ Q_D(QTableView); if (selectionBehavior() == SelectColumns || (selectionMode() == SingleSelection && selectionBehavior() == SelectItems)) return; if (row >= 0 && row < model()->rowCount(rootIndex())) { QModelIndex index = model()->index(row, 0, rootIndex()); QItemSelectionModel::SelectionFlags command = selectionCommand(index); selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate); if (!(command & QItemSelectionModel::Current)) d->rowSectionAnchor = row; QModelIndex tl = model()->index(qMin(d->rowSectionAnchor, row), 0, rootIndex()); QModelIndex br = model()->index(qMax(d->rowSectionAnchor, row), model()->columnCount(rootIndex()) - 1, rootIndex()); selectionModel()->select(QItemSelection(tl, br), command); }}/*! Selects the given \a column in the table view if the current SelectionMode and SelectionBehavior allows columns to be selected. \sa selectRow()*/void QTableView::selectColumn(int column){ Q_D(QTableView); if (selectionBehavior() == SelectRows || (selectionMode() == SingleSelection && selectionBehavior() == SelectItems)) return; if (column >= 0 && column < model()->columnCount(rootIndex())) { QModelIndex index = model()->index(0, column, rootIndex()); QItemSelectionModel::SelectionFlags command = selectionCommand(index); selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate); if (!(command & QItemSelectionModel::Current)) d->columnSectionAnchor = column; QModelIndex tl = model()->index(0, qMin(d->columnSectionAnchor, column), rootIndex()); QModelIndex br = model()->index(model()->rowCount(rootIndex()) - 1, qMax(d->columnSectionAnchor, column), rootIndex()); selectionModel()->select(QItemSelection(tl, br), command); }}/*! Hide the given \a row. \sa showRow() hideColumn()*/void QTableView::hideRow(int row){ d_func()->verticalHeader->hideSection(row);}/*! Hide the given \a column. \sa showColumn() hideRow()*/void QTableView::hideColumn(int column){ d_func()->horizontalHeader->hideSection(column);}/*! Show the given \a row. \sa hideRow() showColumn()*/void QTableView::showRow(int row){ d_func()->verticalHeader->showSection(row);}/*! Show the given \a column. \sa hideColumn() showRow()*/void QTableView::showColumn(int column){ d_func()->horizontalHeader->showSection(column);}/*! Resizes the given \a row based on the size hints of the delegate used to render each item in the row.*/void QTableView::resizeRowToContents(int row){ Q_D(QTableView); int content = sizeHintForRow(row); int header = d->verticalHeader->isHidden() ? 0 : d->verticalHeader->sectionSizeHint(row); d->verticalHeader->resizeSection(row, qMax(content, header));}/*! Resizes all rows based on the size hints of the delegate used to render each item in the rows.*/void QTableView::resizeRowsToContents(){ d_func()->verticalHeader->resizeSections(QHeaderView::Custom);}/*! Resizes the given \a column based on the size hints of the delegate used to render each item in the column.*/void QTableView::resizeColumnToContents(int column){ Q_D(QTableView); int content = sizeHintForColumn(column); int header = d->horizontalHeader->isHidden() ? 0 : d->horizontalHeader->sectionSizeHint(column); d->horizontalHeader->resizeSection(column, qMax(content, header));}/*! Resizes all columns based on the size hints of the delegate used to render each item in the columns.*/void QTableView::resizeColumnsToContents(){ d_func()->horizontalHeader->resizeSections(QHeaderView::Custom);}/*! Sorts the model by the values in the given \a column. */void QTableView::sortByColumn(int column){ Q_D(QTableView); if (!d->model) return; bool ascending = (horizontalHeader()->sortIndicatorSection() == column && horizontalHeader()->sortIndicatorOrder() == Qt::DescendingOrder); Qt::SortOrder order = ascending ? Qt::AscendingOrder : Qt::DescendingOrder; horizontalHeader()->setSortIndicator(column, order); d->model->sort(column, order);}/*! \internal*/void QTableView::verticalScrollbarAction(int action){ QAbstractItemView::verticalScrollbarAction(action);}/*! \internal*/void QTableView::horizontalScrollbarAction(int action){ QAbstractItemView::horizontalScrollbarAction(action);}/*! \reimp*/bool QTableView::isIndexHidden(const QModelIndex &index) const{ Q_ASSERT(index.isValid()); return isRowHidden(index.row()) || isColumnHidden(index.column());}#endif // QT_NO_TABLEVIEW
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -