QTableView

Inheritance diagram of QTableView

Inherited by: QTableWidget

Synopsis

Functions

Slots

Detailed Description

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

A PySide.QtGui.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 PySide.QtGui.QTableView class is one of the Model/View Classes and is part of Qt’s model/view framework .

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

Visual Appearance

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

Rows and columns can be hidden and shown with PySide.QtGui.QTableView.hideRow() , PySide.QtGui.QTableView.hideColumn() , PySide.QtGui.QTableView.showRow() , and PySide.QtGui.QTableView.showColumn() . They can be selected with PySide.QtGui.QTableView.selectRow() and PySide.QtGui.QTableView.selectColumn() . The table will show a grid depending on the PySide.QtGui.QTableView.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 PySide.QtGui.QAbstractItemView.setIndexWidget() function, and later retrieved with PySide.QtGui.QAbstractItemView.indexWidget() .

../../_images/qtableview-resized.png

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 PySide.QtGui.QTableView.horizontalHeader() or PySide.QtGui.QTableView.verticalHeader() and set the header’s PySide.QtGui.QHeaderView.stretchLastSection() property.

To distribute the available space according to the space requirement of each column or row, call the view’s PySide.QtGui.QTableView.resizeColumnsToContents() or PySide.QtGui.QTableView.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 PySide.QtGui.QTableView.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 PySide.QtGui.QTableView.rowViewportPosition() . The PySide.QtGui.QTableView.columnAt() and PySide.QtGui.QTableView.columnViewportPosition() functions provide the equivalent conversion operations between x-coordinates and column indexes.

Styles

PySide.QtGui.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.

../../_images/windowsxp-tableview.png ../../_images/macintosh-tableview.png ../../_images/plastique-tableview.png
A Windows XP style table view. A Macintosh style table view. A Plastique style table view.

See also

PySide.QtGui.QTableWidget View Classes PySide.QtCore.QAbstractItemModel PySide.QtGui.QAbstractItemView Chart Example Pixelator Example Table Model Example

class PySide.QtGui.QTableView([parent=None])
Parameters:parentPySide.QtGui.QWidget

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

PySide.QtGui.QTableView.clearSpans()

Removes all row and column spans in the table view.

PySide.QtGui.QTableView.columnAt(x)
Parameters:xPySide.QtCore.int
Return type:PySide.QtCore.int

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

PySide.QtGui.QTableView.columnCountChanged(oldCount, newCount)
Parameters:
  • oldCountPySide.QtCore.int
  • newCountPySide.QtCore.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 .

PySide.QtGui.QTableView.columnMoved(column, oldIndex, newIndex)
Parameters:
  • columnPySide.QtCore.int
  • oldIndexPySide.QtCore.int
  • newIndexPySide.QtCore.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 .

PySide.QtGui.QTableView.columnResized(column, oldWidth, newWidth)
Parameters:
  • columnPySide.QtCore.int
  • oldWidthPySide.QtCore.int
  • newWidthPySide.QtCore.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 .

PySide.QtGui.QTableView.columnSpan(row, column)
Parameters:
  • rowPySide.QtCore.int
  • columnPySide.QtCore.int
Return type:

PySide.QtCore.int

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

PySide.QtGui.QTableView.columnViewportPosition(column)
Parameters:columnPySide.QtCore.int
Return type:PySide.QtCore.int

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

PySide.QtGui.QTableView.columnWidth(column)
Parameters:columnPySide.QtCore.int
Return type:PySide.QtCore.int

Returns the width of the given column .

PySide.QtGui.QTableView.gridStyle()
Return type:PySide.QtCore.Qt.PenStyle

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

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

PySide.QtGui.QTableView.hideColumn(column)
Parameters:columnPySide.QtCore.int

Hide the given column .

PySide.QtGui.QTableView.hideRow(row)
Parameters:rowPySide.QtCore.int

Hide the given row .

PySide.QtGui.QTableView.horizontalHeader()
Return type:PySide.QtGui.QHeaderView

Returns the table view’s horizontal header.

PySide.QtGui.QTableView.isColumnHidden(column)
Parameters:columnPySide.QtCore.int
Return type:PySide.QtCore.bool

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

PySide.QtGui.QTableView.isCornerButtonEnabled()
Return type:PySide.QtCore.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.

PySide.QtGui.QTableView.isRowHidden(row)
Parameters:rowPySide.QtCore.int
Return type:PySide.QtCore.bool

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

PySide.QtGui.QTableView.isSortingEnabled()
Return type:PySide.QtCore.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 PySide.QtGui.QTableView.setSortingEnabled() immediately triggers a call to PySide.QtGui.QTableView.sortByColumn() with the current sort section and order.

PySide.QtGui.QTableView.resizeColumnToContents(column)
Parameters:columnPySide.QtCore.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 PySide.QtGui.QTableView.sizeHintForColumn() to resize hidden columns as well.

PySide.QtGui.QTableView.resizeColumnsToContents()

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

PySide.QtGui.QTableView.resizeRowToContents(row)
Parameters:rowPySide.QtCore.int

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

PySide.QtGui.QTableView.resizeRowsToContents()

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

PySide.QtGui.QTableView.rowAt(y)
Parameters:yPySide.QtCore.int
Return type:PySide.QtCore.int

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

PySide.QtGui.QTableView.rowCountChanged(oldCount, newCount)
Parameters:
  • oldCountPySide.QtCore.int
  • newCountPySide.QtCore.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 .

PySide.QtGui.QTableView.rowHeight(row)
Parameters:rowPySide.QtCore.int
Return type:PySide.QtCore.int

Returns the height of the given row .

PySide.QtGui.QTableView.rowMoved(row, oldIndex, newIndex)
Parameters:
  • rowPySide.QtCore.int
  • oldIndexPySide.QtCore.int
  • newIndexPySide.QtCore.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 .

PySide.QtGui.QTableView.rowResized(row, oldHeight, newHeight)
Parameters:
  • rowPySide.QtCore.int
  • oldHeightPySide.QtCore.int
  • newHeightPySide.QtCore.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 .

PySide.QtGui.QTableView.rowSpan(row, column)
Parameters:
  • rowPySide.QtCore.int
  • columnPySide.QtCore.int
Return type:

PySide.QtCore.int

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

PySide.QtGui.QTableView.rowViewportPosition(row)
Parameters:rowPySide.QtCore.int
Return type:PySide.QtCore.int

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

PySide.QtGui.QTableView.selectColumn(column)
Parameters:columnPySide.QtCore.int

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

PySide.QtGui.QTableView.selectRow(row)
Parameters:rowPySide.QtCore.int

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

PySide.QtGui.QTableView.setColumnHidden(column, hide)
Parameters:
  • columnPySide.QtCore.int
  • hidePySide.QtCore.bool

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

PySide.QtGui.QTableView.setColumnWidth(column, width)
Parameters:
  • columnPySide.QtCore.int
  • widthPySide.QtCore.int

Sets the width of the given column to be width .

PySide.QtGui.QTableView.setCornerButtonEnabled(enable)
Parameters:enablePySide.QtCore.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.

PySide.QtGui.QTableView.setGridStyle(style)
Parameters:stylePySide.QtCore.Qt.PenStyle

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

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

PySide.QtGui.QTableView.setHorizontalHeader(header)
Parameters:headerPySide.QtGui.QHeaderView

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

PySide.QtGui.QTableView.setRowHeight(row, height)
Parameters:
  • rowPySide.QtCore.int
  • heightPySide.QtCore.int

Sets the height of the given row to be height .

PySide.QtGui.QTableView.setRowHidden(row, hide)
Parameters:
  • rowPySide.QtCore.int
  • hidePySide.QtCore.bool

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

PySide.QtGui.QTableView.setShowGrid(show)
Parameters:showPySide.QtCore.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.

PySide.QtGui.QTableView.setSortingEnabled(enable)
Parameters:enablePySide.QtCore.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 PySide.QtGui.QTableView.setSortingEnabled() immediately triggers a call to PySide.QtGui.QTableView.sortByColumn() with the current sort section and order.

PySide.QtGui.QTableView.setSpan(row, column, rowSpan, columnSpan)
Parameters:
  • rowPySide.QtCore.int
  • columnPySide.QtCore.int
  • rowSpanPySide.QtCore.int
  • columnSpanPySide.QtCore.int

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

PySide.QtGui.QTableView.setVerticalHeader(header)
Parameters:headerPySide.QtGui.QHeaderView

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

PySide.QtGui.QTableView.setWordWrap(on)
Parameters:onPySide.QtCore.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 PySide.QtGui.QAbstractItemView.textElideMode() .

PySide.QtGui.QTableView.showColumn(column)
Parameters:columnPySide.QtCore.int

Show the given column .

PySide.QtGui.QTableView.showGrid()
Return type:PySide.QtCore.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.

PySide.QtGui.QTableView.showRow(row)
Parameters:rowPySide.QtCore.int

Show the given row .

PySide.QtGui.QTableView.sortByColumn(column, order)
Parameters:
  • columnPySide.QtCore.int
  • orderPySide.QtCore.Qt.SortOrder
PySide.QtGui.QTableView.verticalHeader()
Return type:PySide.QtGui.QHeaderView

Returns the table view’s vertical header.

PySide.QtGui.QTableView.visualIndex(index)
Parameters:indexPySide.QtCore.QModelIndex
Return type:PySide.QtCore.int
PySide.QtGui.QTableView.wordWrap()
Return type:PySide.QtCore.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 PySide.QtGui.QAbstractItemView.textElideMode() .