QTableView Class

The QTableView class provides a default model/view implementation of a table view. 更多...

头文件: #include <QTableView>
qmake: QT += widgets
基类: QAbstractItemView
派生类:

QTableWidget

属性

公有函数

QTableView(QWidget *parent = Q_NULLPTR)
~QTableView()
void clearSpans()
int columnAt(int x) const
int columnSpan(int row, int column) const
int columnViewportPosition(int column) const
int columnWidth(int column) const
Qt::PenStyle gridStyle() const
QHeaderView *horizontalHeader() const
bool isColumnHidden(int column) const
bool isCornerButtonEnabled() const
bool isRowHidden(int row) const
bool isSortingEnabled() const
int rowAt(int y) const
int rowHeight(int row) const
int rowSpan(int row, int column) const
int rowViewportPosition(int row) const
void setColumnHidden(int column, bool hide)
void setColumnWidth(int column, int width)
void setCornerButtonEnabled(bool enable)
void setGridStyle(Qt::PenStyle style)
void setHorizontalHeader(QHeaderView *header)
void setRowHeight(int row, int height)
void setRowHidden(int row, bool hide)
void setSortingEnabled(bool enable)
void setSpan(int row, int column, int rowSpanCount, int columnSpanCount)
void setVerticalHeader(QHeaderView *header)
void setWordWrap(bool on)
bool showGrid() const
void sortByColumn(int column, Qt::SortOrder order)
QHeaderView *verticalHeader() const
bool wordWrap() const

重新实现的公有函数

virtual QModelIndex indexAt(const QPoint &pos) const
virtual void setModel(QAbstractItemModel *model)
virtual void setRootIndex(const QModelIndex &index)
virtual void setSelectionModel(QItemSelectionModel *selectionModel)

公有槽函数

void hideColumn(int column)
void hideRow(int row)
void resizeColumnToContents(int column)
void resizeColumnsToContents()
void resizeRowToContents(int row)
void resizeRowsToContents()
void selectColumn(int column)
void selectRow(int row)
void setShowGrid(bool show)
void showColumn(int column)
void showRow(int row)

重新实现的受保护函数

virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous)
virtual int horizontalOffset() const
virtual bool isIndexHidden(const QModelIndex &index) const
virtual QModelIndex moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)
virtual void paintEvent(QPaintEvent *event)
virtual QModelIndexList selectedIndexes() const
virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)
virtual int sizeHintForColumn(int column) const
virtual int sizeHintForRow(int row) const
virtual void timerEvent(QTimerEvent *event)
virtual void updateGeometries()
virtual int verticalOffset() const
virtual QStyleOptionViewItem viewOptions() const
virtual QSize viewportSizeHint() const

Protected Slots

void columnCountChanged(int oldCount, int newCount)
void columnMoved(int column, int oldIndex, int newIndex)
void columnResized(int column, int oldWidth, int newWidth)
void rowCountChanged(int oldCount, int newCount)
void rowMoved(int row, int oldIndex, int newIndex)
void rowResized(int row, int oldHeight, int newHeight)

其他继承的成员

详细描述

The QTableView class provides a default model/view implementation of a table view.

A QTableView implements a table view that displays items from a model. This class is used to provide standard tables that were previously provided by the QTable class, but using the more flexible approach provided by Qt's model/view architecture.

The QTableView class is one of the Model/View Classes and is part of Qt's model/view framework.

QTableView implements the interfaces defined by the QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel class.

Navigation

You can navigate the cells in the table by clicking on a cell with the mouse, or by using the arrow keys. Because QTableView enables tabKeyNavigation by default, you can also hit Tab and Backtab to move from cell to cell.

Visual Appearance

The table has a vertical header that can be obtained using the verticalHeader() function, and a horizontal header that is available through the horizontalHeader() function. The height of each row in the table can be found by using rowHeight(); similarly, the width of columns can be found using columnWidth(). Since both of these are plain widgets, you can hide either of them using their hide() functions.

Rows and columns can be hidden and shown with hideRow(), hideColumn(), showRow(), and showColumn(). They can be selected with selectRow() and selectColumn(). The table will show a grid depending on the showGrid property.

The items shown in a table view, like those in the other item views, are rendered and edited using standard delegates. However, for some tasks it is sometimes useful to be able to insert widgets in a table instead. Widgets are set for particular indexes with the setIndexWidget() function, and later retrieved with indexWidget().

By default, the cells in a table do not expand to fill the available space.

You can make the cells fill the available space by stretching the last header section. Access the relevant header using horizontalHeader() or verticalHeader() and set the header's stretchLastSection property.

To distribute the available space according to the space requirement of each column or row, call the view's resizeColumnsToContents() or resizeRowsToContents() functions.

Coordinate Systems

For some specialized forms of tables it is useful to be able to convert between row and column indexes and widget coordinates. The rowAt() function provides the y-coordinate within the view of the specified row; the row index can be used to obtain a corresponding y-coordinate with rowViewportPosition(). The columnAt() and columnViewportPosition() functions provide the equivalent conversion operations between x-coordinates and column indexes.

Styles

QTableView is styled appropriately for each platform. The following images show how it looks on three different platforms. Go to the Qt Widget Gallery to see its appearance in other styles.

Screenshot of a Windows Vista style table viewScreenshot of a Macintosh style table viewScreenshot of a Fusion style table view
A Windows Vista style table view.A Macintosh style table view.A Fusion style table view.

参见 QTableWidget, View Classes, QAbstractItemModel, QAbstractItemView, Chart Example, Pixelator Example, and Table Model Example.

属性

cornerButtonEnabled : bool

This property holds whether the button in the top-left corner is enabled

If this property is true then button in the top-left corner of the table view is enabled. Clicking on this button will select all the cells in the table view.

This property is true by default.

This property was introduced in Qt 4.3.

访问函数:

bool isCornerButtonEnabled() const
void setCornerButtonEnabled(bool enable)

gridStyle : Qt::PenStyle

This property holds the pen style used to draw the grid.

This property holds the style used when drawing the grid (see showGrid).

访问函数:

Qt::PenStyle gridStyle() const
void setGridStyle(Qt::PenStyle style)

showGrid : bool

This property holds whether the grid is shown

If this property is true a grid is drawn for the table; if the property is false, no grid is drawn. The default value is true.

访问函数:

bool showGrid() const
void setShowGrid(bool show)

sortingEnabled : bool

This property holds whether sorting is enabled

If this property is true, sorting is enabled for the table. If this property is false, sorting is not enabled. The default value is false.

Note: . Setting the property to true with setSortingEnabled() immediately triggers a call to sortByColumn() with the current sort section and order.

This property was introduced in Qt 4.2.

访问函数:

bool isSortingEnabled() const
void setSortingEnabled(bool enable)

参见 sortByColumn().

wordWrap : bool

This property holds the item text word-wrapping policy

If this property is true then the item text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. This property is true by default.

Note that even of wrapping is enabled, the cell will not be expanded to fit all text. Ellipsis will be inserted according to the current textElideMode.

This property was introduced in Qt 4.3.

访问函数:

bool wordWrap() const
void setWordWrap(bool on)

成员函数

QTableView::QTableView(QWidget *parent = Q_NULLPTR)

Constructs a table view with a parent to represent the data.

参见 QAbstractItemModel.

QTableView::~QTableView()

Destroys the table view.

void QTableView::clearSpans()

Removes all row and column spans in the table view.

This function was introduced in Qt 4.4.

参见 setSpan().

int QTableView::columnAt(int x) const

Returns the column in which the given x-coordinate, x, in contents coordinates is located.

Note: This function returns -1 if the given coordinate is not valid (has no column).

参见 rowAt().

[protected slot] void QTableView::columnCountChanged(int oldCount, int newCount)

This slot is called whenever columns are added or deleted. The previous number of columns is specified by oldCount, and the new number of columns is specified by newCount.

[protected slot] void QTableView::columnMoved(int column, int oldIndex, int newIndex)

This slot is called to change the index of the given column in the table view. The old index is specified by oldIndex, and the new index by newIndex.

参见 rowMoved().

[protected slot] void QTableView::columnResized(int column, int oldWidth, int newWidth)

This slot is called to change the width of the given column. The old width is specified by oldWidth, and the new width by newWidth.

参见 rowResized().

int QTableView::columnSpan(int row, int column) const

Returns the column span of the table element at (row, column). The default is 1.

This function was introduced in Qt 4.2.

参见 setSpan() and rowSpan().

int QTableView::columnViewportPosition(int column) const

Returns the x-coordinate in contents coordinates of the given column.

int QTableView::columnWidth(int column) const

Returns the width of the given column.

参见 setColumnWidth(), resizeColumnToContents(), and rowHeight().

[virtual protected] void QTableView::currentChanged(const QModelIndex &current, const QModelIndex &previous)

重新实现 QAbstractItemView::currentChanged().

[slot] void QTableView::hideColumn(int column)

Hide the given column.

参见 showColumn() and hideRow().

[slot] void QTableView::hideRow(int row)

Hide the given row.

参见 showRow() and hideColumn().

QHeaderView *QTableView::horizontalHeader() const

Returns the table view's horizontal header.

参见 setHorizontalHeader(), verticalHeader(), and QAbstractItemModel::headerData().

[virtual protected] int QTableView::horizontalOffset() const

重新实现 QAbstractItemView::horizontalOffset().

Returns the horizontal offset of the items in the table view.

Note that the table view uses the horizontal header section positions to determine the positions of columns in the view.

参见 verticalOffset().

[virtual] QModelIndex QTableView::indexAt(const QPoint &pos) const

重新实现 QAbstractItemView::indexAt().

Returns the index position of the model item corresponding to the table item at position pos in contents coordinates.

bool QTableView::isColumnHidden(int column) const

Returns true if the given column is hidden; otherwise returns false.

参见 isRowHidden().

[virtual protected] bool QTableView::isIndexHidden(const QModelIndex &index) const

重新实现 QAbstractItemView::isIndexHidden().

bool QTableView::isRowHidden(int row) const

Returns true if the given row is hidden; otherwise returns false.

参见 isColumnHidden().

[virtual protected] QModelIndex QTableView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)

重新实现 QAbstractItemView::moveCursor().

Moves the cursor in accordance with the given cursorAction, using the information provided by the modifiers.

参见 QAbstractItemView::CursorAction.

[virtual protected] void QTableView::paintEvent(QPaintEvent *event)

重新实现 QWidget::paintEvent().

Paints the table on receipt of the given paint event event.

[slot] void QTableView::resizeColumnToContents(int column)

Resizes the given column based on the size hints of the delegate used to render each item in the column.

Note: Only visible columns will be resized. Reimplement sizeHintForColumn() to resize hidden columns as well.

参见 resizeColumnsToContents(), sizeHintForColumn(), and QHeaderView::resizeContentsPrecision().

[slot] void QTableView::resizeColumnsToContents()

Resizes all columns based on the size hints of the delegate used to render each item in the columns.

参见 resizeColumnToContents(), sizeHintForColumn(), and QHeaderView::resizeContentsPrecision().

[slot] void QTableView::resizeRowToContents(int row)

Resizes the given row based on the size hints of the delegate used to render each item in the row.

参见 resizeRowsToContents(), sizeHintForRow(), and QHeaderView::resizeContentsPrecision().

[slot] void QTableView::resizeRowsToContents()

Resizes all rows based on the size hints of the delegate used to render each item in the rows.

参见 resizeRowToContents(), sizeHintForRow(), and QHeaderView::resizeContentsPrecision().

int QTableView::rowAt(int y) const

Returns the row in which the given y-coordinate, y, in contents coordinates is located.

Note: This function returns -1 if the given coordinate is not valid (has no row).

参见 columnAt().

[protected slot] void QTableView::rowCountChanged(int oldCount, int newCount)

This slot is called whenever rows are added or deleted. The previous number of rows is specified by oldCount, and the new number of rows is specified by newCount.

int QTableView::rowHeight(int row) const

Returns the height of the given row.

参见 setRowHeight(), resizeRowToContents(), and columnWidth().

[protected slot] void QTableView::rowMoved(int row, int oldIndex, int newIndex)

This slot is called to change the index of the given row in the table view. The old index is specified by oldIndex, and the new index by newIndex.

参见 columnMoved().

[protected slot] void QTableView::rowResized(int row, int oldHeight, int newHeight)

This slot is called to change the height of the given row. The old height is specified by oldHeight, and the new height by newHeight.

参见 columnResized().

int QTableView::rowSpan(int row, int column) const

Returns the row span of the table element at (row, column). The default is 1.

This function was introduced in Qt 4.2.

参见 setSpan() and columnSpan().

int QTableView::rowViewportPosition(int row) const

Returns the y-coordinate in contents coordinates of the given row.

[slot] void QTableView::selectColumn(int column)

Selects the given column in the table view if the current SelectionMode and SelectionBehavior allows columns to be selected.

参见 selectRow().

[slot] void QTableView::selectRow(int row)

Selects the given row in the table view if the current SelectionMode and SelectionBehavior allows rows to be selected.

参见 selectColumn().

[virtual protected] QModelIndexList QTableView::selectedIndexes() const

重新实现 QAbstractItemView::selectedIndexes().

[virtual protected] void QTableView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)

重新实现 QAbstractItemView::selectionChanged().

void QTableView::setColumnHidden(int column, bool hide)

If hide is true the given column will be hidden; otherwise it will be shown.

参见 isColumnHidden() and setRowHidden().

void QTableView::setColumnWidth(int column, int width)

Sets the width of the given column to be width.

This function was introduced in Qt 4.1.

参见 columnWidth().

void QTableView::setHorizontalHeader(QHeaderView *header)

Sets the widget to use for the horizontal header to header.

参见 horizontalHeader() and setVerticalHeader().

[virtual] void QTableView::setModel(QAbstractItemModel *model)

重新实现 QAbstractItemView::setModel().

[virtual] void QTableView::setRootIndex(const QModelIndex &index)

重新实现 QAbstractItemView::setRootIndex().

void QTableView::setRowHeight(int row, int height)

Sets the height of the given row to be height.

This function was introduced in Qt 4.1.

参见 rowHeight().

void QTableView::setRowHidden(int row, bool hide)

If hide is true row will be hidden, otherwise it will be shown.

参见 isRowHidden() and setColumnHidden().

[virtual protected] void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags flags)

重新实现 QAbstractItemView::setSelection().

Selects the items within the given rect and in accordance with the specified selection flags.

[virtual] void QTableView::setSelectionModel(QItemSelectionModel *selectionModel)

重新实现 QAbstractItemView::setSelectionModel().

void QTableView::setSortingEnabled(bool enable)

If enable is true, enables sorting for the table and immediately trigger a call to sortByColumn() with the current sort section and order

Note: Setter function for property sortingEnabled.

参见 isSortingEnabled().

void QTableView::setSpan(int row, int column, int rowSpanCount, int columnSpanCount)

Sets the span of the table element at (row, column) to the number of rows and columns specified by (rowSpanCount, columnSpanCount).

This function was introduced in Qt 4.2.

参见 rowSpan() and columnSpan().

void QTableView::setVerticalHeader(QHeaderView *header)

Sets the widget to use for the vertical header to header.

参见 verticalHeader() and setHorizontalHeader().

[slot] void QTableView::showColumn(int column)

Show the given column.

参见 hideColumn() and showRow().

[slot] void QTableView::showRow(int row)

Show the given row.

参见 hideRow() and showColumn().

[virtual protected] int QTableView::sizeHintForColumn(int column) const

重新实现 QAbstractItemView::sizeHintForColumn().

Returns the size hint for the given 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 will be used when resizeColumnToContents() or QHeaderView::resizeSections() is called. If a larger column width is required by either the horizontal header or the item delegate, the larger width will be used instead.

参见 QWidget::sizeHint, horizontalHeader(), and QHeaderView::resizeContentsPrecision().

[virtual protected] int QTableView::sizeHintForRow(int row) const

重新实现 QAbstractItemView::sizeHintForRow().

Returns the size hint for the given 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.

参见 QWidget::sizeHint, verticalHeader(), and QHeaderView::resizeContentsPrecision().

void QTableView::sortByColumn(int column, Qt::SortOrder order)

Sorts the model by the values in the given column in the given order.

This function was introduced in Qt 4.2.

参见 sortingEnabled.

[virtual protected] void QTableView::timerEvent(QTimerEvent *event)

重新实现 QObject::timerEvent().

[virtual protected] void QTableView::updateGeometries()

重新实现 QAbstractItemView::updateGeometries().

QHeaderView *QTableView::verticalHeader() const

Returns the table view's vertical header.

参见 setVerticalHeader(), horizontalHeader(), and QAbstractItemModel::headerData().

[virtual protected] int QTableView::verticalOffset() const

重新实现 QAbstractItemView::verticalOffset().

Returns the vertical offset of the items in the table view.

Note that the table view uses the vertical header section positions to determine the positions of rows in the view.

参见 horizontalOffset().

[virtual protected] QStyleOptionViewItem QTableView::viewOptions() const

重新实现 QAbstractItemView::viewOptions().

[virtual protected] QSize QTableView::viewportSizeHint() const

重新实现 QAbstractScrollArea::viewportSizeHint().