QTableWidget¶
The
QTableWidget
class provides an item-based table view with a default model. More…
Synopsis¶
Functions¶
def
cellWidget
(row, column)def
closePersistentEditor
(item)def
column
(item)def
columnCount
()def
currentColumn
()def
currentItem
()def
currentRow
()def
editItem
(item)def
findItems
(text, flags)def
horizontalHeaderItem
(column)def
indexFromItem
(item)def
indexFromItem
(item)def
isItemSelected
(item)def
isPersistentEditorOpen
(item)def
item
(row, column)def
itemAt
(p)def
itemAt
(x, y)def
itemFromIndex
(index)def
itemPrototype
()def
items
(data)def
openPersistentEditor
(item)def
removeCellWidget
(row, column)def
row
(item)def
rowCount
()def
selectedItems
()def
selectedRanges
()def
setCellWidget
(row, column, widget)def
setColumnCount
(columns)def
setCurrentCell
(row, column)def
setCurrentCell
(row, column, command)def
setCurrentItem
(item)def
setCurrentItem
(item, command)def
setHorizontalHeaderItem
(column, item)def
setHorizontalHeaderLabels
(labels)def
setItem
(row, column, item)def
setItemPrototype
(item)def
setItemSelected
(item, select)def
setRangeSelected
(range, select)def
setRowCount
(rows)def
setVerticalHeaderItem
(row, item)def
setVerticalHeaderLabels
(labels)def
sortItems
(column[, order=Qt.AscendingOrder])def
takeHorizontalHeaderItem
(column)def
takeItem
(row, column)def
takeVerticalHeaderItem
(row)def
verticalHeaderItem
(row)def
visualColumn
(logicalColumn)def
visualItemRect
(item)def
visualRow
(logicalRow)
Virtual functions¶
def
dropMimeData
(row, column, data, action)def
mimeData
(items)def
mimeTypes
()def
supportedDropActions
()
Slots¶
def
clear
()def
clearContents
()def
insertColumn
(column)def
insertRow
(row)def
removeColumn
(column)def
removeRow
(row)def
scrollToItem
(item[, hint=EnsureVisible])
Signals¶
def
cellActivated
(row, column)def
cellChanged
(row, column)def
cellClicked
(row, column)def
cellDoubleClicked
(row, column)def
cellEntered
(row, column)def
cellPressed
(row, column)def
currentCellChanged
(currentRow, currentColumn, previousRow, previousColumn)def
currentItemChanged
(current, previous)def
itemActivated
(item)def
itemChanged
(item)def
itemClicked
(item)def
itemDoubleClicked
(item)def
itemEntered
(item)def
itemPressed
(item)def
itemSelectionChanged
()
Detailed Description¶
Table widgets provide standard table display facilities for applications. The items in a
QTableWidget
are provided byQTableWidgetItem
.If you want a table that uses your own data model you should use
QTableView
rather than this class.Table widgets can be constructed with the required numbers of rows and columns:
tableWidget = new QTableWidget(12, 3, this);Alternatively, tables can be constructed without a given size and resized later:
tableWidget = new QTableWidget(this); tableWidget->setRowCount(10); tableWidget->setColumnCount(5);Items are created outside the table (with no parent widget) and inserted into the table with
setItem()
:QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg( (row+1)*(column+1))); tableWidget->setItem(row, column, newItem);If you want to enable sorting in your table widget, do so after you have populated it with items, otherwise sorting may interfere with the insertion order (see
setItem()
for details).Tables can be given both horizontal and vertical headers. The simplest way to create the headers is to supply a list of strings to the
setHorizontalHeaderLabels()
andsetVerticalHeaderLabels()
functions. These will provide simple textual headers for the table’s columns and rows. More sophisticated headers can be created from existing table items that are usually constructed outside the table. For example, we can construct a table item with an icon and aligned text, and use it as the header for a particular column:QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(tr("Cubes")); cubesHeaderItem->setIcon(QIcon(QPixmap(":/Images/cubed.png"))); cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);The number of rows in the table can be found with
rowCount()
, and the number of columns withcolumnCount()
. The table can be cleared with theclear()
function.
- class PySide2.QtWidgets.QTableWidget([parent=None])¶
PySide2.QtWidgets.QTableWidget(rows, columns[, parent=None])
- param parent:
- param columns:
int
- param rows:
int
Creates a new table view with the given
parent
.Creates a new table view with the given
rows
andcolumns
, and with the givenparent
.
- PySide2.QtWidgets.QTableWidget.cellActivated(row, column)¶
- Parameters:
row – int
column – int
- PySide2.QtWidgets.QTableWidget.cellChanged(row, column)¶
- Parameters:
row – int
column – int
- PySide2.QtWidgets.QTableWidget.cellClicked(row, column)¶
- Parameters:
row – int
column – int
- PySide2.QtWidgets.QTableWidget.cellDoubleClicked(row, column)¶
- Parameters:
row – int
column – int
- PySide2.QtWidgets.QTableWidget.cellEntered(row, column)¶
- Parameters:
row – int
column – int
- PySide2.QtWidgets.QTableWidget.cellPressed(row, column)¶
- Parameters:
row – int
column – int
- PySide2.QtWidgets.QTableWidget.cellWidget(row, column)¶
- Parameters:
row – int
column – int
- Return type:
Returns the widget displayed in the cell in the given
row
andcolumn
.Note
The table takes ownership of the widget.
See also
- PySide2.QtWidgets.QTableWidget.clear()¶
Removes all items in the view. This will also remove all selections and headers. If you don’t want to remove the headers, use
clearContents()
. The table dimensions stay the same.
- PySide2.QtWidgets.QTableWidget.clearContents()¶
Removes all items not in the headers from the view. This will also remove all selections. The table dimensions stay the same.
- PySide2.QtWidgets.QTableWidget.closePersistentEditor(item)¶
- Parameters:
Closes the persistent editor for
item
.
- PySide2.QtWidgets.QTableWidget.column(item)¶
- Parameters:
- Return type:
int
Returns the column for the
item
.
- PySide2.QtWidgets.QTableWidget.columnCount()¶
- Return type:
int
This property holds the number of columns in the table.
By default, for a table constructed without row and column counts, this property contains a value of 0.
- PySide2.QtWidgets.QTableWidget.currentCellChanged(currentRow, currentColumn, previousRow, previousColumn)¶
- Parameters:
currentRow – int
currentColumn – int
previousRow – int
previousColumn – int
- PySide2.QtWidgets.QTableWidget.currentColumn()¶
- Return type:
int
Returns the column of the current item.
See also
- PySide2.QtWidgets.QTableWidget.currentItem()¶
- Return type:
Returns the current item.
See also
- PySide2.QtWidgets.QTableWidget.currentItemChanged(current, previous)¶
- Parameters:
current –
PySide2.QtWidgets.QTableWidgetItem
previous –
PySide2.QtWidgets.QTableWidgetItem
- PySide2.QtWidgets.QTableWidget.currentRow()¶
- Return type:
int
Returns the row of the current item.
See also
- PySide2.QtWidgets.QTableWidget.dropMimeData(row, column, data, action)¶
- Parameters:
row – int
column – int
data –
PySide2.QtCore.QMimeData
action –
DropAction
- Return type:
bool
Handles the
data
supplied by a drag and drop operation that ended with the givenaction
in the givenrow
andcolumn
. Returnstrue
if the data and action can be handled by the model; otherwise returnsfalse
.See also
- PySide2.QtWidgets.QTableWidget.editItem(item)¶
- Parameters:
Starts editing the
item
if it is editable.
- PySide2.QtWidgets.QTableWidget.findItems(text, flags)¶
- Parameters:
text – str
flags –
MatchFlags
- Return type:
Finds items that matches the
text
using the givenflags
.
- PySide2.QtWidgets.QTableWidget.horizontalHeaderItem(column)¶
- Parameters:
column – int
- Return type:
Returns the horizontal header item for column,
column
, if one has been set; otherwise returnsNone
.See also
- PySide2.QtWidgets.QTableWidget.indexFromItem(item)¶
- Parameters:
- Return type:
Returns the
QModelIndex
associated with the givenitem
.Note
In Qt versions prior to 5.10, this function took a non-
const
item
.
- PySide2.QtWidgets.QTableWidget.indexFromItem(item)
- Parameters:
- Return type:
Returns the
QModelIndex
associated with the givenitem
.Note
In Qt versions prior to 5.10, this function took a non-
const
item
.
- PySide2.QtWidgets.QTableWidget.insertColumn(column)¶
- Parameters:
column – int
Inserts an empty column into the table at
column
.
- PySide2.QtWidgets.QTableWidget.insertRow(row)¶
- Parameters:
row – int
Inserts an empty row into the table at
row
.
- PySide2.QtWidgets.QTableWidget.isItemSelected(item)¶
- Parameters:
- Return type:
bool
Note
This function is deprecated.
Returns
true
if theitem
is selected, otherwise returnsfalse
.This function is deprecated. Use
isSelected()
instead.
- PySide2.QtWidgets.QTableWidget.isPersistentEditorOpen(item)¶
- Parameters:
- Return type:
bool
Returns whether a persistent editor is open for item
item
.
- PySide2.QtWidgets.QTableWidget.item(row, column)¶
- Parameters:
row – int
column – int
- Return type:
Returns the item for the given
row
andcolumn
if one has been set; otherwise returnsNone
.See also
- PySide2.QtWidgets.QTableWidget.itemActivated(item)¶
- Parameters:
- PySide2.QtWidgets.QTableWidget.itemAt(p)¶
- Parameters:
- Return type:
Returns a pointer to the item at the given
point
, or returnsNone
ifpoint
is not covered by an item in the table widget.See also
- PySide2.QtWidgets.QTableWidget.itemAt(x, y)
- Parameters:
x – int
y – int
- Return type:
Returns the item at the position equivalent to
QPoint
(ax
,ay
) in the table widget’s coordinate system, or returnsNone
if the specified point is not covered by an item in the table widget.See also
- PySide2.QtWidgets.QTableWidget.itemChanged(item)¶
- Parameters:
- PySide2.QtWidgets.QTableWidget.itemClicked(item)¶
- Parameters:
- PySide2.QtWidgets.QTableWidget.itemDoubleClicked(item)¶
- Parameters:
- PySide2.QtWidgets.QTableWidget.itemEntered(item)¶
- Parameters:
- PySide2.QtWidgets.QTableWidget.itemFromIndex(index)¶
- Parameters:
index –
PySide2.QtCore.QModelIndex
- Return type:
Returns a pointer to the
QTableWidgetItem
associated with the givenindex
.
- PySide2.QtWidgets.QTableWidget.itemPressed(item)¶
- Parameters:
- PySide2.QtWidgets.QTableWidget.itemPrototype()¶
- Return type:
Returns the item prototype used by the table.
See also
- PySide2.QtWidgets.QTableWidget.itemSelectionChanged()¶
- PySide2.QtWidgets.QTableWidget.items(data)¶
- Parameters:
data –
PySide2.QtCore.QMimeData
- Return type:
Returns a list of pointers to the items contained in the
data
object. If the object was not created by aQTreeWidget
in the same process, the list is empty.
- PySide2.QtWidgets.QTableWidget.mimeData(items)¶
- Parameters:
items –
- Return type:
Returns an object that contains a serialized description of the specified
items
. The format used to describe the items is obtained from themimeTypes()
function.If the list of items is empty,
None
is returned rather than a serialized empty list.
- PySide2.QtWidgets.QTableWidget.mimeTypes()¶
- Return type:
list of strings
Returns a list of MIME types that can be used to describe a list of tablewidget items.
See also
- PySide2.QtWidgets.QTableWidget.openPersistentEditor(item)¶
- Parameters:
Opens an editor for the give
item
. The editor remains open after editing.
- PySide2.QtWidgets.QTableWidget.removeCellWidget(row, column)¶
- Parameters:
row – int
column – int
Removes the widget set on the cell indicated by
row
andcolumn
.
- PySide2.QtWidgets.QTableWidget.removeColumn(column)¶
- Parameters:
column – int
Removes the column
column
and all its items from the table.
- PySide2.QtWidgets.QTableWidget.removeRow(row)¶
- Parameters:
row – int
Removes the row
row
and all its items from the table.
- PySide2.QtWidgets.QTableWidget.row(item)¶
- Parameters:
- Return type:
int
Returns the row for the
item
.
- PySide2.QtWidgets.QTableWidget.rowCount()¶
- Return type:
int
This property holds the number of rows in the table.
By default, for a table constructed without row and column counts, this property contains a value of 0.
- PySide2.QtWidgets.QTableWidget.scrollToItem(item[, hint=EnsureVisible])¶
- Parameters:
hint –
ScrollHint
Scrolls the view if necessary to ensure that the
item
is visible. Thehint
parameter specifies more precisely where theitem
should be located after the operation.
- PySide2.QtWidgets.QTableWidget.selectedItems()¶
- Return type:
Returns a list of all selected items.
This function returns a list of pointers to the contents of the selected cells. Use the
selectedIndexes()
function to retrieve the complete selection including empty cells.See also
selectedIndexes()
- PySide2.QtWidgets.QTableWidget.selectedRanges()¶
- Return type:
Returns a list of all selected ranges.
See also
- PySide2.QtWidgets.QTableWidget.setCellWidget(row, column, widget)¶
- Parameters:
row – int
column – int
widget –
PySide2.QtWidgets.QWidget
Sets the given
widget
to be displayed in the cell in the givenrow
andcolumn
, passing the ownership of the widget to the table.If cell widget A is replaced with cell widget B, cell widget A will be deleted. For example, in the code snippet below, the
QLineEdit
object will be deleted.setCellWidget(index, QLineEdit()) ... setCellWidget(index, QTextEdit())
See also
- PySide2.QtWidgets.QTableWidget.setColumnCount(columns)¶
- Parameters:
columns – int
This property holds the number of columns in the table.
By default, for a table constructed without row and column counts, this property contains a value of 0.
- PySide2.QtWidgets.QTableWidget.setCurrentCell(row, column)¶
- Parameters:
row – int
column – int
Sets the current cell to be the cell at position (
row
,column
).Depending on the current
selection mode
, the cell may also be selected.See also
- PySide2.QtWidgets.QTableWidget.setCurrentCell(row, column, command)
- Parameters:
row – int
column – int
command –
SelectionFlags
Sets the current cell to be the cell at position (
row
,column
), using the givencommand
.See also
- PySide2.QtWidgets.QTableWidget.setCurrentItem(item)¶
- Parameters:
Sets the current item to
item
.Unless the selection mode is
NoSelection
, the item is also selected.See also
- PySide2.QtWidgets.QTableWidget.setCurrentItem(item, command)
- Parameters:
command –
SelectionFlags
Sets the current item to be
item
, using the givencommand
.See also
- PySide2.QtWidgets.QTableWidget.setHorizontalHeaderItem(column, item)¶
- Parameters:
column – int
Sets the horizontal header item for column
column
toitem
. If necessary, the column count is increased to fit the item. The previous header item (if there was one) is deleted.See also
- PySide2.QtWidgets.QTableWidget.setHorizontalHeaderLabels(labels)¶
- Parameters:
labels – list of strings
Sets the horizontal header labels using
labels
.
- PySide2.QtWidgets.QTableWidget.setItem(row, column, item)¶
- Parameters:
row – int
column – int
Sets the item for the given
row
andcolumn
toitem
.The table takes ownership of the item.
Note that if sorting is enabled (see
sortingEnabled
) andcolumn
is the current sort column, therow
will be moved to the sorted position determined byitem
.If you want to set several items of a particular row (say, by calling in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same
row
argument for all items in the same row (i.e. will not move the row).See also
- PySide2.QtWidgets.QTableWidget.setItemPrototype(item)¶
- Parameters:
Sets the item prototype for the table to the specified
item
.The table widget will use the item prototype clone function when it needs to create a new table item. For example when the user is editing in an empty cell. This is useful when you have a
QTableWidgetItem
subclass and want to make sure thatQTableWidget
creates instances of your subclass.The table takes ownership of the prototype.
See also
- PySide2.QtWidgets.QTableWidget.setItemSelected(item, select)¶
- Parameters:
select – bool
Note
This function is deprecated.
Selects or deselects
item
depending onselect
.This function is deprecated. Use
setSelected()
instead.See also
- PySide2.QtWidgets.QTableWidget.setRangeSelected(range, select)¶
- Parameters:
select – bool
Selects or deselects the
range
depending onselect
.
- PySide2.QtWidgets.QTableWidget.setRowCount(rows)¶
- Parameters:
rows – int
This property holds the number of rows in the table.
By default, for a table constructed without row and column counts, this property contains a value of 0.
- PySide2.QtWidgets.QTableWidget.setVerticalHeaderItem(row, item)¶
- Parameters:
row – int
Sets the vertical header item for row
row
toitem
.See also
- PySide2.QtWidgets.QTableWidget.setVerticalHeaderLabels(labels)¶
- Parameters:
labels – list of strings
Sets the vertical header labels using
labels
.
- PySide2.QtWidgets.QTableWidget.sortItems(column[, order=Qt.AscendingOrder])¶
- Parameters:
column – int
order –
SortOrder
Sorts all the rows in the table widget based on
column
andorder
.
- PySide2.QtWidgets.QTableWidget.supportedDropActions()¶
- Return type:
DropActions
Returns the drop actions supported by this view.
See also
DropActions
- PySide2.QtWidgets.QTableWidget.takeHorizontalHeaderItem(column)¶
- Parameters:
column – int
- Return type:
Removes the horizontal header item at
column
from the header without deleting it.
- PySide2.QtWidgets.QTableWidget.takeItem(row, column)¶
- Parameters:
row – int
column – int
- Return type:
Removes the item at
row
andcolumn
from the table without deleting it.
- PySide2.QtWidgets.QTableWidget.takeVerticalHeaderItem(row)¶
- Parameters:
row – int
- Return type:
Removes the vertical header item at
row
from the header without deleting it.
- PySide2.QtWidgets.QTableWidget.verticalHeaderItem(row)¶
- Parameters:
row – int
- Return type:
Returns the vertical header item for row
row
.See also
- PySide2.QtWidgets.QTableWidget.visualColumn(logicalColumn)¶
- Parameters:
logicalColumn – int
- Return type:
int
Returns the visual column of the given
logicalColumn
.
- PySide2.QtWidgets.QTableWidget.visualItemRect(item)¶
- Parameters:
- Return type:
Returns the rectangle on the viewport occupied by the item at
item
.
- PySide2.QtWidgets.QTableWidget.visualRow(logicalRow)¶
- Parameters:
logicalRow – int
- Return type:
int
Returns the visual row of the given
logicalRow
.
© 2022 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.