QTableView

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

Inheritance diagram of PySide2.QtWidgets.QTableView

Inherited by: QTableWidget

Synopsis

Functions

Slots

Detailed Description

../../_images/windows-tableview.png

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.

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

qtableview-resized1

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.

class QTableView([parent=None])
param parent

QWidget

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

PySide2.QtWidgets.QTableView.clearSpans()

Removes all row and column spans in the table view.

See also

setSpan()

PySide2.QtWidgets.QTableView.columnAt(x)
Parameters

xint

Return type

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

See also

rowAt()

PySide2.QtWidgets.QTableView.columnCountChanged(oldCount, newCount)
Parameters
  • oldCountint

  • newCountint

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 .

PySide2.QtWidgets.QTableView.columnMoved(column, oldIndex, newIndex)
Parameters
  • columnint

  • oldIndexint

  • newIndexint

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

PySide2.QtWidgets.QTableView.columnResized(column, oldWidth, newWidth)
Parameters
  • columnint

  • oldWidthint

  • newWidthint

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

PySide2.QtWidgets.QTableView.columnSpan(row, column)
Parameters
  • rowint

  • columnint

Return type

int

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

See also

setSpan() rowSpan()

PySide2.QtWidgets.QTableView.columnViewportPosition(column)
Parameters

columnint

Return type

int

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

PySide2.QtWidgets.QTableView.columnWidth(column)
Parameters

columnint

Return type

int

Returns the width of the given column .

PySide2.QtWidgets.QTableView.gridStyle()
Return type

PenStyle

See also

setGridStyle()

PySide2.QtWidgets.QTableView.hideColumn(column)
Parameters

columnint

Hide the given column .

PySide2.QtWidgets.QTableView.hideRow(row)
Parameters

rowint

Hide the given row .

PySide2.QtWidgets.QTableView.horizontalHeader()
Return type

QHeaderView

Returns the table view’s horizontal header.

See also

setHorizontalHeader() verticalHeader() headerData()

PySide2.QtWidgets.QTableView.isColumnHidden(column)
Parameters

columnint

Return type

bool

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

See also

isRowHidden()

PySide2.QtWidgets.QTableView.isCornerButtonEnabled()
Return type

bool

PySide2.QtWidgets.QTableView.isRowHidden(row)
Parameters

rowint

Return type

bool

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

See also

isColumnHidden()

PySide2.QtWidgets.QTableView.isSortingEnabled()
Return type

bool

PySide2.QtWidgets.QTableView.resizeColumnToContents(column)
Parameters

columnint

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.

PySide2.QtWidgets.QTableView.resizeColumnsToContents()

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

PySide2.QtWidgets.QTableView.resizeRowToContents(row)
Parameters

rowint

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

PySide2.QtWidgets.QTableView.resizeRowsToContents()

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

PySide2.QtWidgets.QTableView.rowAt(y)
Parameters

yint

Return type

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

See also

columnAt()

PySide2.QtWidgets.QTableView.rowCountChanged(oldCount, newCount)
Parameters
  • oldCountint

  • newCountint

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 .

PySide2.QtWidgets.QTableView.rowHeight(row)
Parameters

rowint

Return type

int

Returns the height of the given row .

PySide2.QtWidgets.QTableView.rowMoved(row, oldIndex, newIndex)
Parameters
  • rowint

  • oldIndexint

  • newIndexint

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

PySide2.QtWidgets.QTableView.rowResized(row, oldHeight, newHeight)
Parameters
  • rowint

  • oldHeightint

  • newHeightint

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

PySide2.QtWidgets.QTableView.rowSpan(row, column)
Parameters
  • rowint

  • columnint

Return type

int

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

PySide2.QtWidgets.QTableView.rowViewportPosition(row)
Parameters

rowint

Return type

int

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

PySide2.QtWidgets.QTableView.selectColumn(column)
Parameters

columnint

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

See also

selectRow()

PySide2.QtWidgets.QTableView.selectRow(row)
Parameters

rowint

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

See also

selectColumn()

PySide2.QtWidgets.QTableView.setColumnHidden(column, hide)
Parameters
  • columnint

  • hidebool

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

PySide2.QtWidgets.QTableView.setColumnWidth(column, width)
Parameters
  • columnint

  • widthint

Sets the width of the given column to be width .

See also

columnWidth()

PySide2.QtWidgets.QTableView.setCornerButtonEnabled(enable)
Parameters

enablebool

PySide2.QtWidgets.QTableView.setGridStyle(style)
Parameters

stylePenStyle

See also

gridStyle()

PySide2.QtWidgets.QTableView.setHorizontalHeader(header)
Parameters

headerQHeaderView

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

PySide2.QtWidgets.QTableView.setRowHeight(row, height)
Parameters
  • rowint

  • heightint

Sets the height of the given row to be height .

See also

rowHeight()

PySide2.QtWidgets.QTableView.setRowHidden(row, hide)
Parameters
  • rowint

  • hidebool

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

PySide2.QtWidgets.QTableView.setShowGrid(show)
Parameters

showbool

See also

showGrid()

PySide2.QtWidgets.QTableView.setSortingEnabled(enable)
Parameters

enablebool

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

PySide2.QtWidgets.QTableView.setSpan(row, column, rowSpan, columnSpan)
Parameters
  • rowint

  • columnint

  • rowSpanint

  • columnSpanint

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

PySide2.QtWidgets.QTableView.setVerticalHeader(header)
Parameters

headerQHeaderView

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

PySide2.QtWidgets.QTableView.setWordWrap(on)
Parameters

onbool

See also

wordWrap()

PySide2.QtWidgets.QTableView.showColumn(column)
Parameters

columnint

Show the given column .

PySide2.QtWidgets.QTableView.showGrid()
Return type

bool

See also

setShowGrid()

PySide2.QtWidgets.QTableView.showRow(row)
Parameters

rowint

Show the given row .

PySide2.QtWidgets.QTableView.sortByColumn(column)
Parameters

columnint

This is an overloaded function.

Sorts the model by the values in the given column .

PySide2.QtWidgets.QTableView.sortByColumn(column, order)
Parameters
  • columnint

  • orderSortOrder

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

See also

sortingEnabled

PySide2.QtWidgets.QTableView.verticalHeader()
Return type

QHeaderView

Returns the table view’s vertical header.

See also

setVerticalHeader() horizontalHeader() headerData()

PySide2.QtWidgets.QTableView.wordWrap()
Return type

bool

See also

setWordWrap()