QTableView Class Reference

[ QtGui module]

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

继承 QAbstractItemView .

Inherited by QTableWidget .

方法


详细描述

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.

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

QTableView 实现的接口定义通过 QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel 类。

导航

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.

视觉外观

表格拥有的垂直 Header 头部可以获得使用 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 () 函数。

行和列可以被隐藏和展示采用 hideRow (), hideColumn (), showRow (),和 showColumn (). They can be selected with selectRow () 和 selectColumn ()。 table will show a grid depending on the 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 () 和 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 () or 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.

样式

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

Screenshot of a Windows XP style table view Screenshot of a Macintosh style table view Screenshot of a Plastique style table view
A Windows XP style table view. A Macintosh style table view. A Plastique style table view.

方法文档编制

QTableView.__init__ ( self , QWidget   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

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

另请参阅 QAbstractItemModel .

QTableView.clearSpans ( self )

Removes all row and column spans in the table view.

该函数在 Qt 4.4 引入。

另请参阅 setSpan ().

int QTableView.columnAt ( self , int  x )

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

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

另请参阅 rowAt ().

QTableView.columnCountChanged ( self , int  oldCount , int  newCount )

This method is also a Qt slot with the C++ signature void columnCountChanged(int,int) .

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

QTableView.columnMoved ( self , int  column , int  oldIndex , int  newIndex )

This method is also a Qt slot with the C++ signature void columnMoved(int,int,int) .

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

QTableView.columnResized ( self , int  column , int  oldWidth , int  newWidth )

This method is also a Qt slot with the C++ signature void columnResized(int,int,int) .

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

另请参阅 rowResized ().

int QTableView.columnSpan ( self , int  row , int  column )

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

该函数在 Qt 4.2 引入。

另请参阅 setSpan () and rowSpan ().

int QTableView.columnViewportPosition ( self , int  column )

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

int QTableView.columnWidth ( self , int  column )

Returns the width of the given column .

另请参阅 setColumnWidth (), resizeColumnToContents (), and rowHeight ().

QTableView.currentChanged ( self , QModelIndex   current , QModelIndex   上一 )

重实现自 QAbstractItemView.currentChanged ().

Qt.PenStyle QTableView.gridStyle ( self )

QTableView.hideColumn ( self , int  column )

This method is also a Qt slot with the C++ signature void hideColumn(int) .

隐藏给定 column .

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

QTableView.hideRow ( self , int  row )

This method is also a Qt slot with the C++ signature void hideRow(int) .

隐藏给定 row .

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

QHeaderView QTableView.horizontalHeader ( self )

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

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

int QTableView.horizontalOffset ( self )

重实现自 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 ().

QTableView.horizontalScrollbarAction ( self , int  action )

QModelIndex QTableView.indexAt ( self , QPoint   p )

重实现自 QAbstractItemView.indexAt ().

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

bool QTableView.isColumnHidden ( self , int  column )

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

另请参阅 isRowHidden ().

bool QTableView.isCornerButtonEnabled ( self )

bool QTableView.isIndexHidden ( self , QModelIndex   index )

重实现自 QAbstractItemView.isIndexHidden ().

bool QTableView.isRowHidden ( self , int  row )

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

另请参阅 isColumnHidden ().

bool QTableView.isSortingEnabled ( self )

QModelIndex QTableView.moveCursor ( self , QAbstractItemView.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 .

QTableView.paintEvent ( self , QPaintEvent   e )

重实现自 QWidget.paintEvent ().

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

QTableView.resizeColumnsToContents ( self )

This method is also a Qt slot with the C++ signature void resizeColumnsToContents() .

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

QTableView.resizeColumnToContents ( self , int  column )

This method is also a Qt slot with the C++ signature void resizeColumnToContents(int) .

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.

QTableView.resizeRowsToContents ( self )

This method is also a Qt slot with the C++ signature void resizeRowsToContents() .

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

QTableView.resizeRowToContents ( self , int  row )

This method is also a Qt slot with the C++ signature void resizeRowToContents(int) .

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

int QTableView.rowAt ( self , int  y )

返回行在给定 y 坐标 y , in contents coordinates is located.

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

另请参阅 columnAt ().

QTableView.rowCountChanged ( self , int  oldCount , int  newCount )

This method is also a Qt slot with the C++ signature void rowCountChanged(int,int) .

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

int QTableView.rowHeight ( self , int  row )

返回高度为给定 row .

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

QTableView.rowMoved ( self , int  row , int  oldIndex , int  newIndex )

This method is also a Qt slot with the C++ signature void rowMoved(int,int,int) .

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

QTableView.rowResized ( self , int  row , int  oldHeight , int  newHeight )

This method is also a Qt slot with the C++ signature void rowResized(int,int,int) .

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

另请参阅 columnResized ().

int QTableView.rowSpan ( self , int  row , int  column )

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

该函数在 Qt 4.2 引入。

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

int QTableView.rowViewportPosition ( self , int  row )

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

QTableView.scrollContentsBy ( self , int  dx , int  dy )

QTableView.scrollTo ( self , QModelIndex   index , QAbstractItemView.ScrollHint   hint  = QAbstractItemView.EnsureVisible)

QTableView.selectColumn ( self , int  column )

This method is also a Qt slot with the C++ signature void selectColumn(int) .

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

另请参阅 selectRow ().

list-of-QModelIndex QTableView.selectedIndexes ( self )

重实现自 QAbstractItemView.selectedIndexes ().

QTableView.selectionChanged ( self , QItemSelection   selected , QItemSelection   deselected )

重实现自 QAbstractItemView.selectionChanged ().

QTableView.selectRow ( self , int  row )

This method is also a Qt slot with the C++ signature void selectRow(int) .

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

另请参阅 selectColumn ().

QTableView.setColumnHidden ( self , int  column , bool  hide )

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

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

QTableView.setColumnWidth ( self , int  column , int  width )

设置宽度为给定 column to be width .

该函数在 Qt 4.1 引入。

另请参阅 columnWidth ().

QTableView.setCornerButtonEnabled ( self , bool  enable )

QTableView.setGridStyle ( self , Qt.PenStyle   style )

QTableView.setHorizontalHeader ( self , QHeaderView   header )

header argument has it's ownership transferred to Qt.

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

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

QTableView.setModel ( self , QAbstractItemModel   model )

重实现自 QAbstractItemView.setModel ().

QTableView.setRootIndex ( self , QModelIndex   index )

重实现自 QAbstractItemView.setRootIndex ().

QTableView.setRowHeight ( self , int  row , int  height )

Sets the height of the given row to be height .

该函数在 Qt 4.1 引入。

另请参阅 rowHeight ().

QTableView.setRowHidden ( self , int  row , bool  hide )

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

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

QTableView.setSelection ( self , QRect   rect , QItemSelectionModel.SelectionFlags   command )

重实现自 QAbstractItemView.setSelection ().

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

QTableView.setSelectionModel ( self , QItemSelectionModel   selectionModel )

重实现自 QAbstractItemView.setSelectionModel ().

QTableView.setShowGrid ( self , bool  show )

QTableView.setSortingEnabled ( self , bool  enable )

QTableView.setSpan ( self , int  row , int  column , int  rowSpan , int  columnSpan )

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

QTableView.setVerticalHeader ( self , QHeaderView   header )

header argument has it's ownership transferred to Qt.

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

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

QTableView.setWordWrap ( self , bool  on )

QTableView.showColumn ( self , int  column )

This method is also a Qt slot with the C++ signature void showColumn(int) .

Show the given column .

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

bool QTableView.showGrid ( self )

QTableView.showRow ( self , int  row )

This method is also a Qt slot with the C++ signature void showRow(int) .

Show the given row .

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

int QTableView.sizeHintForColumn ( self , int  column )

重实现自 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 and horizontalHeader ().

int QTableView.sizeHintForRow ( self , int  row )

重实现自 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 () 是 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 代替。

另请参阅 QWidget.sizeHint and verticalHeader ().

QTableView.sortByColumn ( self , int  column )

This method is also a Qt slot with the C++ signature void sortByColumn(int) .

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

该函数在 Qt 4.2 引入。

另请参阅 sortingEnabled .

QTableView.sortByColumn ( self , int  column , Qt.SortOrder   order )

QTableView.timerEvent ( self , QTimerEvent   event )

重实现自 QObject.timerEvent ().

QTableView.updateGeometries ( self )

重实现自 QAbstractItemView.updateGeometries ().

QHeaderView QTableView.verticalHeader ( self )

Returns the table view's vertical header.

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

int QTableView.verticalOffset ( self )

重实现自 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 ().

QTableView.verticalScrollbarAction ( self , int  action )

QStyleOptionViewItem QTableView.viewOptions ( self )

重实现自 QAbstractItemView.viewOptions ().

QRect QTableView.visualRect ( self , QModelIndex   index )

QRegion QTableView.visualRegionForSelection ( self , QItemSelection   selection )

bool QTableView.wordWrap ( self )