PySide6.QtWidgets.QTableWidget¶
- class QTableWidget¶
- The - QTableWidgetclass provides an item-based table view with a default model. More…- Synopsis¶- Properties¶- columnCountᅟ- The number of columns in the table
- rowCountᅟ- The number of rows in the table
 - Methods¶- def - __init__()
- def - cellWidget()
- def - column()
- def - columnCount()
- def - currentColumn()
- def - currentItem()
- def - currentRow()
- def - editItem()
- def - findItems()
- def - indexFromItem()
- def - item()
- def - itemAt()
- def - itemFromIndex()
- def - itemPrototype()
- def - items()
- def - row()
- def - rowCount()
- def - selectedItems()
- def - selectedRanges()
- def - setCellWidget()
- def - setColumnCount()
- def - setCurrentCell()
- def - setCurrentItem()
- def - setItem()
- def - setRowCount()
- def - sortItems()
- def - takeItem()
- def - visualColumn()
- def - visualItemRect()
- def - visualRow()
 - Virtual methods¶- def - dropMimeData()
- def - mimeData()
- def - mimeTypes()
 - Slots¶- def - clear()
- def - clearContents()
- def - insertColumn()
- def - insertRow()
- def - removeColumn()
- def - removeRow()
- def - scrollToItem()
 - Signals¶- def - cellActivated()
- def - cellChanged()
- def - cellClicked()
- def - cellEntered()
- def - cellPressed()
- def - itemActivated()
- def - itemChanged()
- def - itemClicked()
- def - itemEntered()
- def - itemPressed()
 - Note - This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE - Detailed Description¶- Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors.   - Table widgets provide standard table display facilities for applications. The items in a - QTableWidgetare provided by- QTableWidgetItem.- If you want a table that uses your own data model you should use - QTableViewrather than this class.- Table widgets can be constructed with the required numbers of rows and columns: - tableWidget = QTableWidget(12, 3, self) - Alternatively, tables can be constructed without a given size and resized later: - tableWidget = QTableWidget(self) tableWidget.setRowCount(10) tableWidget.setColumnCount(5) - Items are created outside the table (with no parent widget) and inserted into the table with - setItem():- newItem = 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()and- setVerticalHeaderLabels()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:- cubesHeaderItem = 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 with- columnCount(). The table can be cleared with the- clear()function.- Note - Properties can be used directly when - from __feature__ import true_propertyis used or via accessor functions otherwise.- property columnCountᅟ: 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. - Access functions:
 - property rowCountᅟ: 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. - Access functions:
 - Creates a new table view with the given - parent.- __init__(rows, columns[, parent=None])
- Parameters:
- rows – int 
- columns – int 
- parent – - QWidget
 
 
 - Creates a new table view with the given - rowsand- columns, and with the given- parent.- cellActivated(row, column)¶
- Parameters:
- row – int 
- column – int 
 
 
 - This signal is emitted when the cell specified by - rowand- columnhas been activated- cellChanged(row, column)¶
- Parameters:
- row – int 
- column – int 
 
 
 - This signal is emitted whenever the data of the item in the cell specified by - rowand- columnhas changed.- cellClicked(row, column)¶
- Parameters:
- row – int 
- column – int 
 
 
 - This signal is emitted whenever a cell in the table is clicked. The - rowand- columnspecified is the cell that was clicked.- cellDoubleClicked(row, column)¶
- Parameters:
- row – int 
- column – int 
 
 
 - This signal is emitted whenever a cell in the table is double clicked. The - rowand- columnspecified is the cell that was double clicked.- cellEntered(row, column)¶
- Parameters:
- row – int 
- column – int 
 
 
 - This signal is emitted when the mouse cursor enters a cell. The cell is specified by - rowand- column.- This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item. - cellPressed(row, column)¶
- Parameters:
- row – int 
- column – int 
 
 
 - This signal is emitted whenever a cell in the table is pressed. The - rowand- columnspecified is the cell that was pressed.- Returns the widget displayed in the cell in the given - rowand- column.- 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.- clearContents()¶
 - Removes all items not in the headers from the view. This will also remove all selections. The table dimensions stay the same. - closePersistentEditor(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - Closes the persistent editor for - item.- column(item)¶
- Parameters:
- item – - QTableWidgetItem
- Return type:
- int 
 
 - Returns the column for the - item.- columnCount()¶
- Return type:
- int 
 
 - Returns the number of columns. - See also - Getter of property - columnCountᅟ.- currentCellChanged(currentRow, currentColumn, previousRow, previousColumn)¶
- Parameters:
- currentRow – int 
- currentColumn – int 
- previousRow – int 
- previousColumn – int 
 
 
 - This signal is emitted whenever the current cell changes. The cell specified by - previousRowand- previousColumnis the cell that previously had the focus, the cell specified by- currentRowand- currentColumnis the new current cell.- currentColumn()¶
- Return type:
- int 
 
 - Returns the column of the current item. - See also - currentItem()¶
- Return type:
 
 - Returns the current item. - See also - currentItemChanged(current, previous)¶
- Parameters:
- current – - QTableWidgetItem
- previous – - QTableWidgetItem
 
 
 - This signal is emitted whenever the current item changes. The - previousitem is the item that previously had the focus,- currentis the new current item.- currentRow()¶
- Return type:
- int 
 
 - Returns the row of the current item. - See also - dropMimeData(row, column, data, action)¶
- Parameters:
- row – int 
- column – int 
- data – - QMimeData
- action – - DropAction
 
- Return type:
- bool 
 
 - Handles the - datasupplied by a drag and drop operation that ended with the given- actionin the given- rowand- column. Returns- trueif the data and action can be handled by the model; otherwise returns- false.- See also - editItem(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - Starts editing the - itemif it is editable.- findItems(text, flags)¶
- Parameters:
- text – str 
- flags – Combination of - MatchFlag
 
- Return type:
- .list of QTableWidgetItem 
 
 - Finds items that matches the - textusing the given- flags.- horizontalHeaderItem(column)¶
- Parameters:
- column – int 
- Return type:
 
 - Returns the horizontal header item for column, - column, if one has been set; otherwise returns- None.- See also - indexFromItem(item)¶
- Parameters:
- item – - QTableWidgetItem
- Return type:
 
 - Returns the QModelIndex associated with the given - item.- Note - In Qt versions prior to 5.10, this function took a non- - const- item.- insertColumn(column)¶
- Parameters:
- column – int 
 
 - Inserts an empty column into the table at - column.- insertRow(row)¶
- Parameters:
- row – int 
 
 - Inserts an empty row into the table at - row.- isPersistentEditorOpen(item)¶
- Parameters:
- item – - QTableWidgetItem
- Return type:
- bool 
 
 - Returns whether a persistent editor is open for item - item.- item(row, column)¶
- Parameters:
- row – int 
- column – int 
 
- Return type:
 
 - Returns the item for the given - rowand- columnif one has been set; otherwise returns- None.- See also - itemActivated(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - This signal is emitted when the specified - itemhas been activated- Returns a pointer to the item at the given - point, or returns- Noneif- pointis not covered by an item in the table widget.- See also - 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 returns- Noneif the specified point is not covered by an item in the table widget.- See also - itemChanged(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - This signal is emitted whenever the data of - itemhas changed.- itemClicked(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - This signal is emitted whenever an item in the table is clicked. The - itemspecified is the item that was clicked.- itemDoubleClicked(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - This signal is emitted whenever an item in the table is double clicked. The - itemspecified is the item that was double clicked.- itemEntered(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - This signal is emitted when the mouse cursor enters an item. The - itemis the item entered.- This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item. - itemFromIndex(index)¶
- Parameters:
- index – - QModelIndex
- Return type:
 
 - Returns a pointer to the - QTableWidgetItemassociated with the given- index.- itemPressed(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - This signal is emitted whenever an item in the table is pressed. The - itemspecified is the item that was pressed.- itemPrototype()¶
- Return type:
 
 - Returns the item prototype used by the table. - See also - itemSelectionChanged()¶
 - This signal is emitted whenever the selection changes. - See also - items(data)¶
- Parameters:
- data – - QMimeData
- Return type:
- .list of QTableWidgetItem 
 
 - Returns a list of pointers to the items contained in the - dataobject. If the object was not created by a- QTreeWidgetin the same process, the list is empty.- Returns an object that contains a serialized description of the specified - items. The format used to describe the items is obtained from the- mimeTypes()function.- If the list of items is empty, - Noneis returned rather than a serialized empty list.- 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 - openPersistentEditor(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - Opens an editor for the give - item. The editor remains open after editing.- removeCellWidget(row, column)¶
- Parameters:
- row – int 
- column – int 
 
 
 - Removes the widget set on the cell indicated by - rowand- column.- removeColumn(column)¶
- Parameters:
- column – int 
 
 - Removes the column - columnand all its items from the table.- removeRow(row)¶
- Parameters:
- row – int 
 
 - Removes the row - rowand all its items from the table.- row(item)¶
- Parameters:
- item – - QTableWidgetItem
- Return type:
- int 
 
 - Returns the row for the - item.- rowCount()¶
- Return type:
- int 
 
 - Returns the number of rows. - See also - Getter of property - rowCountᅟ.- scrollToItem(item[, hint=QAbstractItemView.ScrollHint.EnsureVisible])¶
- Parameters:
- item – - QTableWidgetItem
- hint – - ScrollHint
 
 
 - Scrolls the view if necessary to ensure that the - itemis visible. The- hintparameter specifies more precisely where the- itemshould be located after the operation.- selectedItems()¶
- Return type:
- .list of QTableWidgetItem 
 
 - 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()- selectedRanges()¶
- Return type:
- .list of QTableWidgetSelectionRange 
 
 - Returns a list of all selected ranges. - See also - Warning - This section contains snippets that were automatically translated from C++ to Python and may contain errors. - Sets the given - widgetto be displayed in the cell in the given- rowand- column, 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 - QLineEditobject will be deleted.- setCellWidget(row, column, QLineEdit()) ... setCellWidget(row, column, QTextEdit()) - See also - setColumnCount(columns)¶
- Parameters:
- columns – int 
 
 - Sets the number of columns in this table’s model to - columns. If this is less than- columnCount(), the data in the unwanted columns is discarded.- See also - Setter of property - columnCountᅟ.- 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 - setCurrentCell(row, column, command)
- Parameters:
- row – int 
- column – int 
- command – Combination of - SelectionFlag
 
 
 - Sets the current cell to be the cell at position ( - row,- column), using the given- command.- See also - setCurrentItem(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - Sets the current item to - item.- Unless the selection mode is - NoSelection, the item is also selected.- See also - setCurrentItem(item, command)
- Parameters:
- item – - QTableWidgetItem
- command – Combination of - SelectionFlag
 
 
 - Sets the current item to be - item, using the given- command.- See also - setHorizontalHeaderItem(column, item)¶
- Parameters:
- column – int 
- item – - QTableWidgetItem
 
 
 - Sets the horizontal header item for column - columnto- item. If necessary, the column count is increased to fit the item. The previous header item (if there was one) is deleted.- See also - setHorizontalHeaderLabels(labels)¶
- Parameters:
- labels – list of strings 
 
 - Sets the horizontal header labels using - labels.- setItem(row, column, item)¶
- Parameters:
- row – int 
- column – int 
- item – - QTableWidgetItem
 
 
 - Sets the item for the given - rowand- columnto- item.- The table takes ownership of the item. - Note that if sorting is enabled (see - sortingEnabled) and- columnis the current sort column, the- rowwill be moved to the sorted position determined by- item.- If you want to set several items of a particular row (say, by calling setItem() 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 - rowargument for all items in the same row (i.e. setItem() will not move the row).- See also - setItemPrototype(item)¶
- Parameters:
- item – - QTableWidgetItem
 
 - 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 - QTableWidgetItemsubclass and want to make sure that- QTableWidgetcreates instances of your subclass.- The table takes ownership of the prototype. - See also - setRangeSelected(range, select)¶
- Parameters:
- range – - QTableWidgetSelectionRange
- select – bool 
 
 
 - Selects or deselects the - rangedepending on- select.- setRowCount(rows)¶
- Parameters:
- rows – int 
 
 - Sets the number of rows in this table’s model to - rows. If this is less than- rowCount(), the data in the unwanted rows is discarded.- See also - Setter of property - rowCountᅟ.- setVerticalHeaderItem(row, item)¶
- Parameters:
- row – int 
- item – - QTableWidgetItem
 
 
 - Sets the vertical header item for row - rowto- item.- See also - setVerticalHeaderLabels(labels)¶
- Parameters:
- labels – list of strings 
 
 - Sets the vertical header labels using - labels.- Sorts all the rows in the table widget based on - columnand- order.- supportedDropActions()¶
- Return type:
- Combination of - DropAction
 
 - Returns the drop actions supported by this view. - See also - DropActions- takeHorizontalHeaderItem(column)¶
- Parameters:
- column – int 
- Return type:
 
 - Removes the horizontal header item at - columnfrom the header without deleting it.- takeItem(row, column)¶
- Parameters:
- row – int 
- column – int 
 
- Return type:
 
 - Removes the item at - rowand- columnfrom the table without deleting it.- takeVerticalHeaderItem(row)¶
- Parameters:
- row – int 
- Return type:
 
 - Removes the vertical header item at - rowfrom the header without deleting it.- verticalHeaderItem(row)¶
- Parameters:
- row – int 
- Return type:
 
 - Returns the vertical header item for row - row.- See also - visualColumn(logicalColumn)¶
- Parameters:
- logicalColumn – int 
- Return type:
- int 
 
 - Returns the visual column of the given - logicalColumn.- visualItemRect(item)¶
- Parameters:
- item – - QTableWidgetItem
- Return type:
 
 - Returns the rectangle on the viewport occupied by the item at - item.- visualRow(logicalRow)¶
- Parameters:
- logicalRow – int 
- Return type:
- int 
 
 - Returns the visual row of the given - logicalRow.