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:

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)[source]#

Bases: QTableWidget

QTableWidget with a context menu displaying up to 5 actions:

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.

../../../_images/TableWidget.png
Parameters:
  • parent – Parent QWidget

  • cut (bool) – Enable cut action

  • paste (bool) – Enable paste action

mousePressEvent(self, e: QMouseEvent | None)[source]#
enablePaste()[source]#

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()[source]#

Enable cut action.

Warning

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

setSelectionMode(mode)[source]#

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)[source]#

Bases: QTableView

QTableView with a context menu displaying up to 5 actions:

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)[source]#
setModel(model)[source]#

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

Parameters:

modelqt.QAbstractItemModel object

enablePaste()[source]#

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()[source]#

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)[source]#
setSelectionMode(mode)[source]#

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

Parameters:

mode

Returns:

Actions#

class CopySelectedCellsAction(table)[source]#

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:

tableQTableView to which this action belongs.

class CopyAllCellsAction(table)[source]#

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:

tableQTableView to which this action belongs.

class CutSelectedCellsAction(table)[source]#

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:

tableQTableView to which this action belongs.

class CutAllCellsAction(table)[source]#

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:

tableQTableView to which this action belongs.

class PasteCellsAction(table)[source]#

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:

tableQTableView to which this action belongs.