Skip to main content
`Ctrl`+`K`

# `TableWidget`: Table widget

This module provides table widgets handling cut, copy and paste for multiple cell selections. These actions can be triggered using keyboard shortcuts or through a context menu (right-click).

`TableView` is a subclass of `QTableView`. The added features are made available to users after a model is added to the widget, using `TableView.setModel()`.

`TableWidget` is a subclass of `qt.QTableWidget`, a table view with a built-in standard data model. The added features are available as soon as the widget is initialized.

The cut, copy and paste actions are implemented as QActions:

> - `CopySelectedCellsAction` (Ctrl+C)

> - `CopyAllCellsAction`

> - `CutSelectedCellsAction` (Ctrl+X)

> - `CutAllCellsAction`

> - `PasteCellsAction` (Ctrl+V)

The copy actions are enabled by default. The cut and paste actions must be explicitly enabled, by passing parameters `cut=True, paste=True` when creating the widgets, or later by calling their `enableCut()` and `enablePaste()` methods.

## Widget

class TableWidget(parent=None, cut=False, paste=False)

  Bases: `QTableWidget`

  `QTableWidget` with a context menu displaying up to 5 actions:

  > - `CopySelectedCellsAction`

  > - `CopyAllCellsAction`

  > - `CutSelectedCellsAction`

  > - `CutAllCellsAction`

  > - `PasteCellsAction`

  These actions interact with the clipboard and can be used to copy data to or from an external application, or another widget.

  The cut and paste actions are disabled by default, due to the risk of overwriting data (no Undo action is available). Use `enablePaste()` and `enableCut()` to activate them.

  Parameters:

    - parent – Parent QWidget

    - cut (bool) – Enable cut action

    - paste (bool) – Enable paste action

  mousePressEvent(self, e: QMouseEvent | None)
  enablePaste()

    Enable paste action, to paste data from the clipboard into the table.

    Warning

    This action can cause data to be overwritten. There is currently no Undo action to retrieve lost data.

  enableCut()

    Enable cut action.

    Warning

    This action can cause data to be deleted. There is currently no Undo action to retrieve lost data.

  setSelectionMode(mode)

    Overloaded from QTableWidget to disable cut/copy selection actions in case mode is NoSelection

    Parameters:

      mode

    Returns:

## View

class TableView(parent=None, cut=False, paste=False)

  Bases: `QTableView`

  `QTableView` with a context menu displaying up to 5 actions:

  > - `CopySelectedCellsAction`

  > - `CopyAllCellsAction`

  > - `CutSelectedCellsAction`

  > - `CutAllCellsAction`

  > - `PasteCellsAction`

  These actions interact with the clipboard and can be used to copy data to or from an external application, or another widget.

  The cut and paste actions are disabled by default, due to the risk of overwriting data (no Undo action is available). Use `enablePaste()` and `enableCut()` to activate them.

  Note

  These actions will be available only after a model is associated with this view, using `setModel()`.

  Parameters:

    - parent – Parent QWidget

    - cut (bool) – Enable cut action

    - paste (bool) – Enable paste action

  mousePressEvent(self, e: QMouseEvent | None)
  setModel(model)

    Set the data model for the table view, activate the actions and the context menu.

    Parameters:

      model – `qt.QAbstractItemModel` object

  enablePaste()

    Enable paste action, to paste data from the clipboard into the table.

    Warning

    This action can cause data to be overwritten. There is currently no Undo action to retrieve lost data.

  enableCut()

    Enable cut action.

    Warning

    This action can cause data to be deleted. There is currently no Undo action to retrieve lost data.

  addAction(self, action: QAction | None)
  setSelectionMode(mode)

    Overloaded from QTableView to disable cut/copy selection actions in case mode is NoSelection

    Parameters:

      mode

    Returns:

## Actions

class CopySelectedCellsAction(table)

  Bases: `QAction`

  QAction to copy text from selected cells in a `QTableWidget` into the clipboard.

  If multiple cells are selected, the copied text will be a concatenation of the texts in all selected cells, tabulated with tabulation and newline characters.

  If the cells are sparsely selected, the structure is preserved by representing the unselected cells as empty strings in between two tabulation characters. Beware of pasting this data in another table widget, because depending on how the paste is implemented, the empty cells may cause data in the target table to be deleted, even though you didn’t necessarily select the corresponding cell in the origin table.

  Parameters:

    table – `QTableView` to which this action belongs.

class CopyAllCellsAction(table)

  Bases: `QAction`

  QAction to copy text from all cells in a `QTableWidget` into the clipboard.

  The copied text will be a concatenation of the texts in all cells, tabulated with tabulation and newline characters.

  Parameters:

    table – `QTableView` to which this action belongs.

class CutSelectedCellsAction(table)

  Bases: `CopySelectedCellsAction`

  QAction to cut text from selected cells in a `QTableWidget` into the clipboard.

  The text is deleted from the original table widget (use `CopySelectedCellsAction` to preserve the original data).

  If multiple cells are selected, the cut text will be a concatenation of the texts in all selected cells, tabulated with tabulation and newline characters.

  If the cells are sparsely selected, the structure is preserved by representing the unselected cells as empty strings in between two tabulation characters. Beware of pasting this data in another table widget, because depending on how the paste is implemented, the empty cells may cause data in the target table to be deleted, even though you didn’t necessarily select the corresponding cell in the origin table.

  Parameters:

    table – `QTableView` to which this action belongs.

class CutAllCellsAction(table)

  Bases: `CopyAllCellsAction`

  QAction to cut text from all cells in a `QTableWidget` into the clipboard.

  The text is deleted from the original table widget (use `CopyAllCellsAction` to preserve the original data).

  The cut text will be a concatenation of the texts in all cells, tabulated with tabulation and newline characters.

  Parameters:

    table – `QTableView` to which this action belongs.

class PasteCellsAction(table)

  Bases: `QAction`

  QAction to paste text from the clipboard into the table.

  If the text contains tabulations and newlines, they are interpreted as column and row separators. In such a case, the text is split into multiple texts to be pasted into multiple cells.

  If a cell content is an empty string in the original text, it is ignored: the destination cell’s text will not be deleted.

  Parameters:

    table – `QTableView` to which this action belongs.