|  | Home · All Classes · Modules

# QTableView Class Reference
[QtGui module]

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

Inherits QAbstractItemView.

Inherited by QTableWidget.

### Methods

- __init__ (self, QWidget parent = None)
- clearSpans (self)
- int columnAt (self, int x)
- columnCountChanged (self, int oldCount, int newCount)
- columnMoved (self, int column, int oldIndex, int newIndex)
- columnResized (self, int column, int oldWidth, int newWidth)
- int columnSpan (self, int row, int column)
- int columnViewportPosition (self, int column)
- int columnWidth (self, int column)
- currentChanged (self, QModelIndex current, QModelIndex previous)
- Qt.PenStyle gridStyle (self)
- hideColumn (self, int column)
- hideRow (self, int row)
- QHeaderView horizontalHeader (self)
- int horizontalOffset (self)
- horizontalScrollbarAction (self, int action)
- QModelIndex indexAt (self, QPoint p)
- bool isColumnHidden (self, int column)
- bool isCornerButtonEnabled (self)
- bool isIndexHidden (self, QModelIndex index)
- bool isRowHidden (self, int row)
- bool isSortingEnabled (self)
- QModelIndex moveCursor (self, QAbstractItemView.CursorAction cursorAction, Qt.KeyboardModifiers modifiers)
- paintEvent (self, QPaintEvent e)
- resizeColumnsToContents (self)
- resizeColumnToContents (self, int column)
- resizeRowsToContents (self)
- resizeRowToContents (self, int row)
- int rowAt (self, int y)
- rowCountChanged (self, int oldCount, int newCount)
- int rowHeight (self, int row)
- rowMoved (self, int row, int oldIndex, int newIndex)
- rowResized (self, int row, int oldHeight, int newHeight)
- int rowSpan (self, int row, int column)
- int rowViewportPosition (self, int row)
- scrollContentsBy (self, int dx, int dy)
- scrollTo (self, QModelIndex index, QAbstractItemView.ScrollHint hint = QAbstractItemView.EnsureVisible)
- selectColumn (self, int column)
- list-of-QModelIndex selectedIndexes (self)
- selectionChanged (self, QItemSelection selected, QItemSelection deselected)
- selectRow (self, int row)
- setColumnHidden (self, int column, bool hide)
- setColumnWidth (self, int column, int width)
- setCornerButtonEnabled (self, bool enable)
- setGridStyle (self, Qt.PenStyle style)
- setHorizontalHeader (self, QHeaderView header)
- setModel (self, QAbstractItemModel model)
- setRootIndex (self, QModelIndex index)
- setRowHeight (self, int row, int height)
- setRowHidden (self, int row, bool hide)
- setSelection (self, QRect rect, QItemSelectionModel.SelectionFlags command)
- setSelectionModel (self, QItemSelectionModel selectionModel)
- setShowGrid (self, bool show)
- setSortingEnabled (self, bool enable)
- setSpan (self, int row, int column, int rowSpan, int columnSpan)
- setVerticalHeader (self, QHeaderView header)
- setWordWrap (self, bool on)
- showColumn (self, int column)
- bool showGrid (self)
- showRow (self, int row)
- int sizeHintForColumn (self, int column)
- int sizeHintForRow (self, int row)
- sortByColumn (self, int column)
- sortByColumn (self, int column, Qt.SortOrder order)
- timerEvent (self, QTimerEvent event)
- updateGeometries (self)
- QHeaderView verticalHeader (self)
- int verticalOffset (self)
- verticalScrollbarAction (self, int action)
- QStyleOptionViewItem viewOptions (self)
- QRect visualRect (self, QModelIndex index)
- QRegion visualRegionForSelection (self, QItemSelection selection)
- bool wordWrap (self)

## Detailed Description

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.

A Windows XP style table view. | A Macintosh style table view. | A Plastique style table view.

## Method Documentation

### QTableView.__init__ (self, QWidget parent = None)

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

See also QAbstractItemModel.

### QTableView.clearSpans (self)

Removes all row and column spans in the table view.

This function was introduced in Qt 4.4.

See also setSpan().

### int QTableView.columnAt (self, int x)

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

See also 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, and the 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.

See also 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)`.

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.

See also rowResized().

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

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

This function was introduced in Qt 4.2.

See also 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.

See also setColumnWidth(), resizeColumnToContents(), and rowHeight().

### QTableView.currentChanged (self, QModelIndex current, QModelIndex previous)

Reimplemented from 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)`.

Hide the given column.

See also showColumn() and hideRow().

### QTableView.hideRow (self, int row)

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

Hide the given row.

See also showRow() and hideColumn().

### QHeaderView QTableView.horizontalHeader (self)

Returns the table view's horizontal header.

See also setHorizontalHeader(), verticalHeader(), and QAbstractItemModel.headerData().

### int QTableView.horizontalOffset (self)

Reimplemented from 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.

See also verticalOffset().

### QTableView.horizontalScrollbarAction (self, int action)

### QModelIndex QTableView.indexAt (self, QPoint p)

Reimplemented from 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.

See also isRowHidden().

### bool QTableView.isCornerButtonEnabled (self)

### bool QTableView.isIndexHidden (self, QModelIndex index)

Reimplemented from QAbstractItemView.isIndexHidden().

### bool QTableView.isRowHidden (self, int row)

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

See also isColumnHidden().

### bool QTableView.isSortingEnabled (self)

### QModelIndex QTableView.moveCursor (self, QAbstractItemView.CursorAction cursorAction, Qt.KeyboardModifiers modifiers)

Reimplemented from QAbstractItemView.moveCursor().

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

See also QAbstractItemView.CursorAction.

### QTableView.paintEvent (self, QPaintEvent e)

Reimplemented from QWidget.paintEvent().

Paints the table on receipt of the given paint event 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.

Note: 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)

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

See also 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, and the new number of rows is specified by newCount.

### int QTableView.rowHeight (self, int row)

Returns the height of the given row.

See also 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.

See also 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, and the new height by newHeight.

See also columnResized().

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

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

This function was introduced in Qt 4.2.

See also 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)`.

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

See also selectRow().

### list-of-QModelIndex QTableView.selectedIndexes (self)

Reimplemented from QAbstractItemView.selectedIndexes().

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

Reimplemented from QAbstractItemView.selectionChanged().

### QTableView.selectRow (self, int row)

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

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

See also selectColumn().

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

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

See also isColumnHidden() and setRowHidden().

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

Sets the width of the given column to be width.

This function was introduced in Qt 4.1.

See also columnWidth().

### QTableView.setCornerButtonEnabled (self, bool enable)

### QTableView.setGridStyle (self, Qt.PenStyle style)

### QTableView.setHorizontalHeader (self, QHeaderView header)

The header argument has it's ownership transferred to Qt.

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

See also horizontalHeader() and setVerticalHeader().

### QTableView.setModel (self, QAbstractItemModel model)

Reimplemented from QAbstractItemView.setModel().

### QTableView.setRootIndex (self, QModelIndex index)

Reimplemented from QAbstractItemView.setRootIndex().

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

Sets the height of the given row to be height.

This function was introduced in Qt 4.1.

See also rowHeight().

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

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

See also isRowHidden() and setColumnHidden().

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

Reimplemented from QAbstractItemView.setSelection().

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

### QTableView.setSelectionModel (self, QItemSelectionModel selectionModel)

Reimplemented from 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).

This function was introduced in Qt 4.2.

See also rowSpan() and columnSpan().

### QTableView.setVerticalHeader (self, QHeaderView header)

The header argument has it's ownership transferred to Qt.

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

See also verticalHeader() and 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.

See also hideColumn() and 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.

See also hideRow() and showColumn().

### int QTableView.sizeHintForColumn (self, int column)

Reimplemented from 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.

See also QWidget.sizeHint and horizontalHeader().

### int QTableView.sizeHintForRow (self, int row)

Reimplemented from 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.

See also 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 in the given order.

This function was introduced in Qt 4.2.

See also sortingEnabled.

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

### QTableView.timerEvent (self, QTimerEvent event)

Reimplemented from QObject.timerEvent().

### QTableView.updateGeometries (self)

Reimplemented from QAbstractItemView.updateGeometries().

### QHeaderView QTableView.verticalHeader (self)

Returns the table view's vertical header.

See also setVerticalHeader(), horizontalHeader(), and QAbstractItemModel.headerData().

### int QTableView.verticalOffset (self)

Reimplemented from 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.

See also horizontalOffset().

### QTableView.verticalScrollbarAction (self, int action)

### QStyleOptionViewItem QTableView.viewOptions (self)

Reimplemented from QAbstractItemView.viewOptions().

### QRect QTableView.visualRect (self, QModelIndex index)

### QRegion QTableView.visualRegionForSelection (self, QItemSelection selection)

### bool QTableView.wordWrap (self)