QTableView 类

QTableView 类提供表格视图的默认模型/视图实现。 更多...

头: #include <QTableView>
qmake: QT += widgets
继承: QAbstractItemView
继承者:

QTableWidget

特性

公共函数

QTableView (QWidget * parent = nullptr)
virtual ~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
QHeaderView * verticalHeader () const
bool wordWrap () const

重实现公共函数

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

公共槽

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 )
void sortByColumn (int column , Qt::SortOrder order )

重实现保护函数

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

保护槽

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 )

详细描述

QTableView 实现从模型显示项的表格视图。此类用于提供先前由 QTable 类提供的标准表格,但使用由 Qt 模型/视图体系结构提供的更灵活方式。

QTableView 类是一种 模型/视图类 且属于 Qt 的 模型/视图框架 .

QTableView 实现的接口定义通过 QAbstractItemView 类以允许它显示提供数据,通过模型派生自 QAbstractItemModel 类。

可以通过采用鼠标点击单元格或通过使用方向键来导航表格单元格。因为 QTableView 启用 tabKeyNavigation 默认情况下,也可以命中 Tab 和 Backtab 键以从一单元格移至另一单元格。

视觉外观

表格拥有的垂直 Header 头部可以获得使用 verticalHeader () 函数,和水平 Header 头部获得透过 horizontalHeader () 函数。可以找到表格中每行的高度通过使用 rowHeight ();同样,可以找到列的宽度使用 columnWidth ()。由于这两者是纯 Widget,可以隐藏它们使用 hide () 函数。

行和列可以被隐藏和展示采用 hideRow (), hideColumn (), showRow (),和 showColumn ()。可以选择它们采用 selectRow () 和 selectColumn ()。表格将展示栅格从属 showGrid 特性。

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 () 或 verticalHeader () and set the header's stretchLastSection 特性。

To distribute the available space according to the space requirement of each column or row, call the view's resizeColumnsToContents () 或 resizeRowsToContents () 函数。

坐标系

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 ()。 columnAt () 和 columnViewportPosition () functions provide the equivalent conversion operations between x-coordinates and column indexes.

另请参阅 QTableWidget , 视图类 , QAbstractItemModel , QAbstractItemView , 图表范例 , 像素器范例 ,和 表格模型范例 .

特性文档编制

cornerButtonEnabled : bool

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

若此特性为 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.

此特性是 true 在默认情况下。

该特性在 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

若此特性为 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

此特性保持是否启用排序

若此特性为 true , sorting is enabled for the table. If this property is false , sorting is not enabled. The default value is false.

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

该特性在 Qt 4.2 引入。

访问函数:

bool isSortingEnabled () const
void setSortingEnabled (bool enable )

另请参阅 sortByColumn ().

wordWrap : bool

此特性保持项文本的自动换行策略

若此特性为 true then the item text is wrapped where necessary at word-breaks; otherwise it is not wrapped at all. This property is true 在默认情况下。

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 .

该特性在 Qt 4.3 引入。

访问函数:

bool wordWrap () const
void setWordWrap (bool on )

成员函数文档编制

QTableView:: QTableView ( QWidget * parent = nullptr)

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

另请参阅 QAbstractItemModel .

[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 )

调用此槽改变宽度为给定 column 。旧宽度的指定通过 oldWidth ,和新宽度通过 newWidth .

另请参阅 rowResized ().

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

隐藏给定 column .

另请参阅 showColumn () 和 hideRow ().

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

隐藏给定 row .

另请参阅 showRow () 和 hideColumn ().

[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.

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

另请参阅 resizeColumnsToContents (), sizeHintForColumn (),和 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 (),和 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 (),和 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 (),和 QHeaderView::resizeContentsPrecision ().

[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 .

[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 ().

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

选择给定 column in the table view if the current SelectionMode and SelectionBehavior allows columns to be selected.

另请参阅 selectRow ().

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

选择给定 row in the table view if the current SelectionMode and SelectionBehavior allows rows to be selected.

另请参阅 selectColumn ().

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

Show the given column .

另请参阅 hideColumn () 和 showRow ().

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

Show the given row .

另请参阅 hideRow () 和 showColumn ().

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

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

column may be -1, in which case no sort indicator will be shown and the model will return to its natural, unsorted order. Note that not all models support this and may even crash in this case.

该函数在 Qt 4.2 引入。

另请参阅 sortingEnabled .

[virtual] QTableView:: ~QTableView ()

Destroys the table view.

void QTableView:: clearSpans ()

Removes all row and column spans in the table view.

该函数在 Qt 4.4 引入。

另请参阅 setSpan ().

int QTableView:: columnAt ( int x ) const

Returns the column in which the given x-coordinate, x ,在内容坐标中定位。

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

另请参阅 rowAt ().

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

Returns the column span of the table element at ( row , column )。默认为 1。

该函数在 Qt 4.2 引入。

另请参阅 setSpan () 和 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 (),和 rowHeight ().

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

重实现: QAbstractItemView::currentChanged (const QModelIndex ¤t, const QModelIndex &previous).

QHeaderView *QTableView:: horizontalHeader () const

返回表格视图的水平 Header 头。

另请参阅 setHorizontalHeader (), verticalHeader (),和 QAbstractItemModel::headerData ().

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

重实现: QAbstractItemView::horizontalOffset () const.

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 ().

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

重实现: QAbstractItemView::indexAt (const QPoint &point) const.

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

返回 true 若给定 column 被隐藏;否则返回 false .

另请参阅 isRowHidden ().

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

重实现: QAbstractItemView::isIndexHidden (const QModelIndex &index) const.

bool QTableView:: isRowHidden ( int row ) const

返回 true 若给定 row 被隐藏;否则返回 false .

另请参阅 isColumnHidden ().

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

重实现: QAbstractItemView::moveCursor (QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers).

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

另请参阅 QAbstractItemView::CursorAction .

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

重实现: QAbstractScrollArea::paintEvent (QPaintEvent *event).

描绘表格当收到给定描绘事件 event .

int QTableView:: rowAt ( int y ) const

返回行在给定 y 坐标 y ,在内容坐标中定位。

注意: 此函数返回 -1 若给定坐标无效 (没有行)。

另请参阅 columnAt ().

int QTableView:: rowHeight ( int row ) const

返回高度为给定 row .

另请参阅 setRowHeight (), resizeRowToContents (),和 columnWidth ().

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

Returns the row span of the table element at ( row , column )。默认为 1。

该函数在 Qt 4.2 引入。

另请参阅 setSpan () 和 columnSpan ().

int QTableView:: rowViewportPosition ( int row ) const

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

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

重实现: QAbstractItemView::selectedIndexes () const.

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

重实现: QAbstractItemView::selectionChanged (const QItemSelection &selected, const QItemSelection &deselected).

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

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

另请参阅 isColumnHidden () 和 setRowHidden ().

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

设置宽度为给定 column width .

该函数在 Qt 4.1 引入。

另请参阅 columnWidth ().

void QTableView:: setHorizontalHeader ( QHeaderView * header )

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

另请参阅 horizontalHeader () 和 setVerticalHeader ().

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

重实现: QAbstractItemView::setModel (QAbstractItemModel *model).

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

重实现: QAbstractItemView::setRootIndex (const QModelIndex &index).

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

Sets the height of the given row height .

该函数在 Qt 4.1 引入。

另请参阅 rowHeight ().

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

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

另请参阅 isRowHidden () 和 setColumnHidden ().

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

重实现: QAbstractItemView::setSelection (const QRect &rect, QItemSelectionModel::SelectionFlags flags).

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

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

重实现: QAbstractItemView::setSelectionModel (QItemSelectionModel *selectionModel).

void QTableView:: setSortingEnabled ( bool enable )

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

注意: Setter 函数对于特性 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 ).

该函数在 Qt 4.2 引入。

另请参阅 rowSpan () 和 columnSpan ().

void QTableView:: setVerticalHeader ( QHeaderView * header )

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

另请参阅 verticalHeader () 和 setHorizontalHeader ().

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

重实现: QAbstractItemView::sizeHintForColumn (int column) const.

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 () 或 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 (),和 QHeaderView::resizeContentsPrecision ().

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

重实现: QAbstractItemView::sizeHintForRow (int row) const.

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 (),和 QHeaderView::resizeContentsPrecision ().

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

重实现: QAbstractItemView::timerEvent (QTimerEvent *event).

[override virtual protected] void QTableView:: updateGeometries ()

重实现: QAbstractItemView::updateGeometries ().

QHeaderView *QTableView:: verticalHeader () const

Returns the table view's vertical header.

另请参阅 setVerticalHeader (), horizontalHeader (),和 QAbstractItemModel::headerData ().

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

重实现: QAbstractItemView::verticalOffset () const.

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 ().

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

重实现: QAbstractItemView::viewOptions () const.

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

重实现: QAbstractItemView::viewportSizeHint () const.