Skip to content Skip to sidebar Skip to footer

How To Paste And Copy Out Multiple Cells Values In Qt Table Widget

I am very new in programming. Please help me in this regards. Thanks in advance. Here is my code: from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def

Solution 1:

If you only need internal copy and paste operations, you can use a local variable to store the item data (row, column and content), then use an event filter to trigger those operations.

In the following example I created two simple functions that can be called when using the keyboard shortcuts (ctrl+c or ctrl+v) or through a context menu.

When one or more items are selected, they can be copied. If the "clipboard" has contents and one index is selected (the "current index"), those contents will be pasted relative to it.

from PyQt5 import QtCore, QtGui, QtWidgets

classWindow(QtWidgets.QWidget):
    def__init__(self):
        super(Window, self).__init__()
        self.model = QtGui.QStandardItemModel(10, 10)
        self.tableView = QtWidgets.QTableView()
        self.tableView.setModel(self.model)

        self.tableView.installEventFilter(self)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.tableView)

        self.clipboard = []

    defeventFilter(self, source, event):
        if event.type() == QtCore.QEvent.KeyPress:
            if event == QtGui.QKeySequence.Copy:
                self.copySelection()
                returnTrueelif event == QtGui.QKeySequence.Paste:
                self.pasteSelection()
                returnTrueelif event.type() == QtCore.QEvent.ContextMenu:
            # a context menu for the copy/paste operations
            menu = QtWidgets.QMenu()
            copyAction = menu.addAction('Copy')
            copyAction.triggered.connect(self.copySelection)
            pasteAction = menu.addAction('Paste')
            pasteAction.triggered.connect(self.pasteSelection)
            ifnot self.tableView.selectedIndexes():
                # no selection available, both copy and paste are disabled
                copyAction.setEnabled(False)
                pasteAction.setEnabled(False)
            ifnot self.clipboard:
                # no clipboard contents, paste is disabled
                pasteAction.setEnabled(False)
            menu.exec(event.globalPos())
            returnTruereturnsuper(Window, self).eventFilter(source, event)

    defcopySelection(self):
        # clear the current contents of the clipboard
        self.clipboard.clear()
        selected = self.tableView.selectedIndexes()
        rows = []
        columns = []
        # cycle all selected items to get the minimum row and column, so that the# reference will always be [0, 0]for index in selected:
            rows.append(index.row())
            columns.append(index.column())
        minRow = min(rows)
        minCol = min(columns)
        for index in selected:
            # append the data of each selected index
            self.clipboard.append((index.row() - minRow, index.column() - minCol, index.data()))

    defpasteSelection(self):
        ifnot self.clipboard:
            return
        current = self.tableView.currentIndex()
        ifnot current.isValid():
            # in the rare case that there is no current index, use the first row# and column as target
            current = self.model.index(0, 0)

        firstRow = current.row()
        firstColumn = current.column()

        # optional: get the selection model so that pasted indexes will be# automatically selected at the end
        selection = self.tableView.selectionModel()
        for row, column, data in self.clipboard:
            # get the index, with rows and columns relative to the current
            index = self.model.index(firstRow + row, firstColumn + column)
            # set the data for the index
            self.model.setData(index, data, QtCore.Qt.DisplayRole)
            # add the index to the selection
            selection.select(index, selection.Select)

        # apply the selection model
        self.tableView.setSelectionModel(selection)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

Note that I didn't use your code, as that is the output generated from pyuic, which should never be modified. Those files are only meant to be imported, and the program logic must be implemented on a separate script. Read the documentation about using Designer to know how to correctly use UI files, otherwise just create the interface by subclassing the qwidget as done in my example.

Post a Comment for "How To Paste And Copy Out Multiple Cells Values In Qt Table Widget"