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

Inherited by: QTableWidget
Synopsis¶
Functions¶
def
clearSpans
()def
columnAt
(x)def
columnSpan
(row, column)def
columnViewportPosition
(column)def
columnWidth
(column)def
gridStyle
()def
horizontalHeader
()def
isColumnHidden
(column)def
isCornerButtonEnabled
()def
isRowHidden
(row)def
isSortingEnabled
()def
rowAt
(y)def
rowHeight
(row)def
rowSpan
(row, column)def
rowViewportPosition
(row)def
setColumnHidden
(column, hide)def
setColumnWidth
(column, width)def
setCornerButtonEnabled
(enable)def
setGridStyle
(style)def
setHorizontalHeader
(header)def
setRowHeight
(row, height)def
setRowHidden
(row, hide)def
setSortingEnabled
(enable)def
setSpan
(row, column, rowSpan, columnSpan)def
setVerticalHeader
(header)def
setWordWrap
(on)def
showGrid
()def
verticalHeader
()def
wordWrap
()
Slots¶
def
columnCountChanged
(oldCount, newCount)def
columnMoved
(column, oldIndex, newIndex)def
columnResized
(column, oldWidth, newWidth)def
hideColumn
(column)def
hideRow
(row)def
resizeColumnToContents
(column)def
resizeColumnsToContents
()def
resizeRowToContents
(row)def
resizeRowsToContents
()def
rowCountChanged
(oldCount, newCount)def
rowMoved
(row, oldIndex, newIndex)def
rowResized
(row, oldHeight, newHeight)def
selectColumn
(column)def
selectRow
(row)def
setShowGrid
(show)def
showColumn
(column)def
showRow
(row)def
sortByColumn
(column)def
sortByColumn
(column, order)
Detailed Description¶
![]()
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 theQAbstractItemView
class to allow it to display data provided by models derived from theQAbstractItemModel
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 thehorizontalHeader()
function. The height of each row in the table can be found by usingrowHeight()
; similarly, the width of columns can be found usingcolumnWidth()
. Since both of these are plain widgets, you can hide either of them using theirhide()
functions.Rows and columns can be hidden and shown with
hideRow()
,hideColumn()
,showRow()
, andshowColumn()
. They can be selected withselectRow()
andselectColumn()
. The table will show a grid depending on theshowGrid
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 thesetIndexWidget()
function, and later retrieved withindexWidget()
.
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()
orverticalHeader()
and set the header’sstretchLastSection
property.To distribute the available space according to the space requirement of each column or row, call the view’s
resizeColumnsToContents()
orresizeRowsToContents()
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 withrowViewportPosition()
. ThecolumnAt()
andcolumnViewportPosition()
functions provide the equivalent conversion operations between x-coordinates and column indexes.See also
QTableWidget
View ClassesQAbstractItemModel
QAbstractItemView
Chart Example Pixelator Example Table Model Example
-
class
PySide2.QtWidgets.
QTableView
([parent=None])¶ - param parent
Constructs a table view with a
parent
to represent the data.See also
QAbstractItemModel
-
PySide2.QtWidgets.QTableView.
clearSpans
()¶ Removes all row and column spans in the table view.
See also
-
PySide2.QtWidgets.QTableView.
columnAt
(x)¶ - Parameters
x – int
- 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
-
PySide2.QtWidgets.QTableView.
columnCountChanged
(oldCount, newCount)¶ - Parameters
oldCount – int
newCount – 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 bynewCount
.
-
PySide2.QtWidgets.QTableView.
columnMoved
(column, oldIndex, newIndex)¶ - Parameters
column – int
oldIndex – int
newIndex – int
This slot is called to change the index of the given
column
in the table view. The old index is specified byoldIndex
, and the new index bynewIndex
.See also
-
PySide2.QtWidgets.QTableView.
columnResized
(column, oldWidth, newWidth)¶ - Parameters
column – int
oldWidth – int
newWidth – int
This slot is called to change the width of the given
column
. The old width is specified byoldWidth
, and the new width bynewWidth
.See also
-
PySide2.QtWidgets.QTableView.
columnSpan
(row, column)¶ - Parameters
row – int
column – int
- Return type
int
Returns the column span of the table element at (
row
,column
). The default is 1.
-
PySide2.QtWidgets.QTableView.
columnViewportPosition
(column)¶ - Parameters
column – int
- Return type
int
Returns the x-coordinate in contents coordinates of the given
column
.
-
PySide2.QtWidgets.QTableView.
columnWidth
(column)¶ - Parameters
column – int
- Return type
int
Returns the width of the given
column
.
-
PySide2.QtWidgets.QTableView.
gridStyle
()¶ - Return type
This property holds the pen style used to draw the grid..
This property holds the style used when drawing the grid (see
showGrid
).
-
PySide2.QtWidgets.QTableView.
hideColumn
(column)¶ - Parameters
column – int
Hide the given
column
.See also
-
PySide2.QtWidgets.QTableView.
hideRow
(row)¶ - Parameters
row – int
Hide the given
row
.See also
-
PySide2.QtWidgets.QTableView.
horizontalHeader
()¶ - Return type
Returns the table view’s horizontal header.
See also
setHorizontalHeader()
verticalHeader()
headerData()
-
PySide2.QtWidgets.QTableView.
isColumnHidden
(column)¶ - Parameters
column – int
- Return type
bool
Returns
true
if the givencolumn
is hidden; otherwise returnsfalse
.See also
-
PySide2.QtWidgets.QTableView.
isCornerButtonEnabled
()¶ - Return type
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.
-
PySide2.QtWidgets.QTableView.
isRowHidden
(row)¶ - Parameters
row – int
- Return type
bool
Returns
true
if the givenrow
is hidden; otherwise returnsfalse
.See also
-
PySide2.QtWidgets.QTableView.
isSortingEnabled
()¶ - Return type
bool
This property holds whether sorting is enabled.
If this property is
true
, sorting is enabled for the table. If this property isfalse
, sorting is not enabled. The default value is false.Note
. Setting the property to true with
setSortingEnabled()
immediately triggers a call tosortByColumn()
with the current sort section and order.See also
-
PySide2.QtWidgets.QTableView.
resizeColumnToContents
(column)¶ - Parameters
column – 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.See also
resizeColumnsToContents()
sizeHintForColumn()
resizeContentsPrecision()
-
PySide2.QtWidgets.QTableView.
resizeColumnsToContents
()¶ Resizes all columns based on the size hints of the delegate used to render each item in the columns.
See also
resizeColumnToContents()
sizeHintForColumn()
resizeContentsPrecision()
-
PySide2.QtWidgets.QTableView.
resizeRowToContents
(row)¶ - Parameters
row – int
Resizes the given
row
based on the size hints of the delegate used to render each item in the row.See also
resizeRowsToContents()
sizeHintForRow()
resizeContentsPrecision()
-
PySide2.QtWidgets.QTableView.
resizeRowsToContents
()¶ Resizes all rows based on the size hints of the delegate used to render each item in the rows.
See also
resizeRowToContents()
sizeHintForRow()
resizeContentsPrecision()
-
PySide2.QtWidgets.QTableView.
rowAt
(y)¶ - Parameters
y – int
- 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
-
PySide2.QtWidgets.QTableView.
rowCountChanged
(oldCount, newCount)¶ - Parameters
oldCount – int
newCount – 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 bynewCount
.
-
PySide2.QtWidgets.QTableView.
rowHeight
(row)¶ - Parameters
row – int
- Return type
int
Returns the height of the given
row
.
-
PySide2.QtWidgets.QTableView.
rowMoved
(row, oldIndex, newIndex)¶ - Parameters
row – int
oldIndex – int
newIndex – int
This slot is called to change the index of the given
row
in the table view. The old index is specified byoldIndex
, and the new index bynewIndex
.See also
-
PySide2.QtWidgets.QTableView.
rowResized
(row, oldHeight, newHeight)¶ - Parameters
row – int
oldHeight – int
newHeight – int
This slot is called to change the height of the given
row
. The old height is specified byoldHeight
, and the new height bynewHeight
.See also
-
PySide2.QtWidgets.QTableView.
rowSpan
(row, column)¶ - Parameters
row – int
column – int
- Return type
int
Returns the row span of the table element at (
row
,column
). The default is 1.See also
-
PySide2.QtWidgets.QTableView.
rowViewportPosition
(row)¶ - Parameters
row – int
- Return type
int
Returns the y-coordinate in contents coordinates of the given
row
.
-
PySide2.QtWidgets.QTableView.
selectColumn
(column)¶ - Parameters
column – int
Selects the given
column
in the table view if the currentSelectionMode
andSelectionBehavior
allows columns to be selected.See also
-
PySide2.QtWidgets.QTableView.
selectRow
(row)¶ - Parameters
row – int
Selects the given
row
in the table view if the currentSelectionMode
andSelectionBehavior
allows rows to be selected.See also
-
PySide2.QtWidgets.QTableView.
setColumnHidden
(column, hide)¶ - Parameters
column – int
hide – bool
If
hide
is true the givencolumn
will be hidden; otherwise it will be shown.See also
-
PySide2.QtWidgets.QTableView.
setColumnWidth
(column, width)¶ - Parameters
column – int
width – int
Sets the width of the given
column
to bewidth
.See also
-
PySide2.QtWidgets.QTableView.
setCornerButtonEnabled
(enable)¶ - Parameters
enable – 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.
-
PySide2.QtWidgets.QTableView.
setGridStyle
(style)¶ - Parameters
style –
PenStyle
This property holds the pen style used to draw the grid..
This property holds the style used when drawing the grid (see
showGrid
).
-
PySide2.QtWidgets.QTableView.
setHorizontalHeader
(header)¶ - Parameters
header –
PySide2.QtWidgets.QHeaderView
Sets the widget to use for the horizontal header to
header
.See also
-
PySide2.QtWidgets.QTableView.
setRowHeight
(row, height)¶ - Parameters
row – int
height – int
Sets the height of the given
row
to beheight
.See also
-
PySide2.QtWidgets.QTableView.
setRowHidden
(row, hide)¶ - Parameters
row – int
hide – bool
If
hide
is truerow
will be hidden, otherwise it will be shown.See also
-
PySide2.QtWidgets.QTableView.
setShowGrid
(show)¶ - Parameters
show – bool
This property holds whether the grid is shown.
If this property is
true
a grid is drawn for the table; if the property isfalse
, no grid is drawn. The default value is true.
-
PySide2.QtWidgets.QTableView.
setSortingEnabled
(enable)¶ - Parameters
enable – bool
This property holds whether sorting is enabled.
If this property is
true
, sorting is enabled for the table. If this property isfalse
, sorting is not enabled. The default value is false.Note
. Setting the property to true with
setSortingEnabled()
immediately triggers a call tosortByColumn()
with the current sort section and order.See also
-
PySide2.QtWidgets.QTableView.
setSpan
(row, column, rowSpan, columnSpan)¶ - Parameters
row – int
column – int
rowSpan – int
columnSpan – int
Sets the span of the table element at (
row
,column
) to the number of rows and columns specified by (rowSpanCount
,columnSpanCount
).See also
-
PySide2.QtWidgets.QTableView.
setVerticalHeader
(header)¶ - Parameters
header –
PySide2.QtWidgets.QHeaderView
Sets the widget to use for the vertical header to
header
.See also
-
PySide2.QtWidgets.QTableView.
setWordWrap
(on)¶ - Parameters
on – 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 istrue
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
textElideMode
.
-
PySide2.QtWidgets.QTableView.
showColumn
(column)¶ - Parameters
column – int
Show the given
column
.See also
-
PySide2.QtWidgets.QTableView.
showGrid
()¶ - Return type
bool
This property holds whether the grid is shown.
If this property is
true
a grid is drawn for the table; if the property isfalse
, no grid is drawn. The default value is true.
-
PySide2.QtWidgets.QTableView.
showRow
(row)¶ - Parameters
row – int
Show the given
row
.See also
-
PySide2.QtWidgets.QTableView.
sortByColumn
(column)¶ - Parameters
column – int
Note
This function is deprecated.
This is an overloaded function.
This function is deprecated. Use
sortByColumn
(int column,SortOrder
order) instead. Sorts the model by the values in the givencolumn
.
-
PySide2.QtWidgets.QTableView.
sortByColumn
(column, order) - Parameters
column – int
order –
SortOrder
Sorts the model by the values in the given
column
andorder
.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.See also
sortingEnabled
-
PySide2.QtWidgets.QTableView.
verticalHeader
()¶ - Return type
Returns the table view’s vertical header.
See also
setVerticalHeader()
horizontalHeader()
headerData()
-
PySide2.QtWidgets.QTableView.
wordWrap
()¶ - Return type
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 istrue
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
textElideMode
.
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.