如何在PyQt5中的QTableWidget中复制和粘贴多个单元格?

问题描述 投票:0回答:5

我有一个 Qtablewidget,如下所示。我想从表中复制多个单元格并粘贴到同一个表中的其他行中。

到目前为止,我可以在单个单元上执行相同的操作,但是有没有办法可以同时执行多个单元??

另外,如果可能的话,复制多行并将其粘贴到下面的同一个表中?

我尝试在 SO 中查看一些答案,但没有在 PyQt5 中实现它。

提前致谢。

示例代码(QtDesigner 提供):

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1048, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(40, 40, 861, 511))
        self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.AnyKeyPressed|QtWidgets.QAbstractItemView.DoubleClicked|QtWidgets.QAbstractItemView.EditKeyPressed)
        self.tableWidget.setRowCount(5)
        self.tableWidget.setColumnCount(5)
        self.tableWidget.setObjectName("tableWidget")
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setHorizontalHeaderItem(4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(0, 0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(0, 1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(0, 2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(0, 3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(0, 4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(1, 0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(1, 1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(1, 2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(1, 3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(1, 4, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(2, 0, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(2, 1, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(2, 2, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(2, 3, item)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget.setItem(2, 4, item)
        self.tableWidget.horizontalHeader().setVisible(True)
        self.tableWidget.verticalHeader().setVisible(False)
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        item = self.tableWidget.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "Name"))
        item = self.tableWidget.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "ID"))
        item = self.tableWidget.horizontalHeaderItem(2)
        item.setText(_translate("MainWindow", "City"))
        item = self.tableWidget.horizontalHeaderItem(3)
        item.setText(_translate("MainWindow", "Number"))
        item = self.tableWidget.horizontalHeaderItem(4)
        item.setText(_translate("MainWindow", "Occupation"))
        __sortingEnabled = self.tableWidget.isSortingEnabled()
        self.tableWidget.setSortingEnabled(False)
        item = self.tableWidget.item(0, 0)
        item.setText(_translate("MainWindow", "Mark"))
        item = self.tableWidget.item(0, 1)
        item.setText(_translate("MainWindow", "1"))
        item = self.tableWidget.item(0, 2)
        item.setText(_translate("MainWindow", "Newyork"))
        item = self.tableWidget.item(0, 3)
        item.setText(_translate("MainWindow", "4796423643344"))
        item = self.tableWidget.item(0, 4)
        item.setText(_translate("MainWindow", "Teacher"))
        item = self.tableWidget.item(1, 0)
        item.setText(_translate("MainWindow", "Taylor"))
        item = self.tableWidget.item(1, 1)
        item.setText(_translate("MainWindow", "2"))
        item = self.tableWidget.item(1, 2)
        item.setText(_translate("MainWindow", "Chicago"))
        item = self.tableWidget.item(1, 3)
        item.setText(_translate("MainWindow", "43683284"))
        item = self.tableWidget.item(1, 4)
        item.setText(_translate("MainWindow", "Nurse"))
        self.tableWidget.setSortingEnabled(__sortingEnabled)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

派生类:

from PyQt5 import QtWidgets

from demo import Ui_MainWindow

class demo_code(QtWidgets.QMainWindow, Ui_MainWindow):                    
    def __init__(self):
        super(demo_code, self).__init__()
        self.setupUi(self) 



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = demo_code()    
    window.show()
    sys.exit(app.exec_())
python pyqt pyqt5 qt-designer qtablewidget
5个回答
6
投票

这是一个子类化 QTableWidget 的解决方案。您必须重新实现

keyPressEvent
,以捕获复制和粘贴按键序列。复制时,使用
QTableWidget.selectedIndexes()
保存当前选定的项目。粘贴时,为列表中的每个单元格设置一个新的 QTableWidgetItem,将索引转换为新的突出显示索引。它将粘贴所有突出显示的单元格,它们不需要位于同一行。它假设具有最小索引的单元格将被放置在粘贴时突出显示的单元格中。

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class Table(QTableWidget):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setRowCount(10)
        self.setColumnCount(10)
        # etc.

    def keyPressEvent(self, event):
        super().keyPressEvent(event)
        if event.key() == Qt.Key_C and (event.modifiers() & Qt.ControlModifier):
            self.copied_cells = sorted(self.selectedIndexes())
        elif event.key() == Qt.Key_V and (event.modifiers() & Qt.ControlModifier):
            r = self.currentRow() - self.copied_cells[0].row()
            c = self.currentColumn() - self.copied_cells[0].column()
            for cell in self.copied_cells:
                self.setItem(cell.row() + r, cell.column() + c, QTableWidgetItem(cell.data()))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = Table()
    gui.show()
    sys.exit(app.exec_())

3
投票

这是为 PyQt6 编写的解决方案,支持复制到剪贴板,以便您可以将 QTableWidget 中的多个单元格选择粘贴到流行的电子表格程序中,例如 Google Sheets、Apple Numbers 或 Microsoft Excel。这些程序需要制表符 (' ') 分隔新列和换行符 (' ') 分隔新行

from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *

import sys

class TableWithCopy(QTableWidget):
    """
    this class extends QTableWidget
    * supports copying multiple cell's text onto the clipboard
    * formatted specifically to work with multiple-cell paste into programs
      like google sheets, excel, or numbers
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def keyPressEvent(self, event):
        super().keyPressEvent(event)
        if event.key() == Qt.Key.Key_C and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):
            copied_cells = sorted(self.selectedIndexes())

            copy_text = ''
            max_column = copied_cells[-1].column()
            for c in copied_cells:
                copy_text += self.item(c.row(), c.column()).text()
                if c.column() == max_column:
                    copy_text += '\n'
                else:
                    copy_text += '\t'
                    
            QApplication.clipboard().setText(copy_text)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = TableWithCopy()
    gui.setColumnCount(10)
    gui.setRowCount(10)
    gui.show()
    sys.exit(app.exec())

0
投票

要使用粘贴功能稍微扩展 boochbrain 答案(在我的用例中,我需要数据覆盖所有行和列,才能粘贴它):

from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *

import sys

class TableWithCopy(QTableWidget):
    """
    this class extends QTableWidget
    * supports copying multiple cell's text onto the clipboard
    * formatted specifically to work with multiple-cell paste into programs
      like google sheets, excel, or numbers
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def keyPressEvent(self, event):
        super().keyPressEvent(event)
        if event.key() == Qt.Key.Key_C and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):
            copied_cells = sorted(self.selectedIndexes())

            copy_text = ''
            max_column = copied_cells[-1].column()
            for c in copied_cells:
                copy_text += self.item(c.row(), c.column()).text()
                if c.column() == max_column:
                    copy_text += '\n'
                else:
                    copy_text += '\t'
                    
            QApplication.clipboard().setText(copy_text)

        if event.key() == Qt.Key_V and (event.modifiers() & Qt.KeyboardModifier.ControlModifier):
            rows = QApplication.clipboard().text().split('\n')[:-1]
            if len(rows) == 0:
                return
            if len(rows) != self.rowCount() or len(rows[0].split('\t')) != self.columnCount():
                QMessageBox.information(None, 'Error', 'The pasted data does not contain the correct data')
                return
            self.clear()
            self.setRowCount(len(rows))
            self.setColumnCount(len(rows[0].split('\t')))
            for i, row in enumerate(rows):
                row = row.split('\t')
                item1 = QTableWidgetItem(row[0])
                item2 = QTableWidgetItem(row[1])
                self.setItem(i, 0, item1)
                self.setItem(i, 1, item2)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = TableWithCopy()
    gui.setColumnCount(10)
    gui.setRowCount(10)
    gui.show()
    sys.exit(app.exec())

0
投票
def copy_selected_cells(self):
    copied_cells = sorted(self.ui.tableWidget.selectedIndexes())
    copy_text = ''
    max_column = copied_cells[-1].column()
    max_row = copied_cells[-1].row()
    for c in copied_cells:
        copy_text += self.ui.tableWidget.item(c.row(), c.column()).text()
        if c.column() == max_column:
            if c.row() != max_row:
                copy_text += '\n'
        else:
            copy_text += '\t'
    QApplication.clipboard().setText(copy_text)
def paste_to_cells(self):
    selection = self.ui.tableWidget.selectedIndexes()
    if selection:
        row_anchor = selection[0].row()
        column_anchor = selection[0].column()
        clipboard = QApplication.clipboard()
        rows = clipboard.text().split('\n')
        for indx_row, row in enumerate(rows):
            values = row.split('\t')
            for indx_col, value in enumerate(values):
                if row_anchor + indx_row < self.ui.tableWidget.rowCount() and column_anchor + indx_col < self.ui.tableWidget.columnCount():
                    item = QTableWidgetItem(value)
                    self.ui.tableWidget.setItem(row_anchor + indx_row, column_anchor + indx_col, item)

我使用上面的代码实现了复制粘贴方法。 如果 c.row() != max_row: 复制文本+=' ' 我修复了复制方法的一个类似错误。


0
投票

这是我在Qtablewidget中复制、粘贴和剪切的解决方案。

复制:

def copy_selected_cells(self):
    copied_cells = sorted(self.tableWidget.selectedIndexes())
    copy_text = ''
    max_column = copied_cells[-1].column()
    max_row = copied_cells[-1].row()
    for c in copied_cells:
        copy_text += self.tableWidget.item(c.row(), c.column()).text()
        if c.column() == max_column:
            if c.row() != max_row:
                copy_text += '\n'
        else:
            copy_text += '\t'
    QApplication.clipboard().setText(copy_text)

粘贴:

def paste_to_cells(self):
    selection = self.tableWidget.selectedIndexes()
    if selection:
        row_anchor = selection[0].row()
        column_anchor = selection[0].column()
        clipboard = QApplication.clipboard()
        rows = clipboard.text().split('\n')
        print(len(rows))
        current_row = self.tableWidget.currentRow()
        for n in range(len(rows)):
            self.tableWidget.insertRow(current_row + n)
        self.tableWidget.clearSelection()
        self.tableWidget.selectRow(current_row)
        for indx_row, row in enumerate(rows):
            values = row.split('\t')
            for indx_col, value in enumerate(values):
                if row_anchor + indx_row < self.tableWidget.rowCount() and column_anchor + indx_col < self.tableWidget.columnCount():
                    item = QTableWidgetItem(value)
                    self.tableWidget.setItem(row_anchor + indx_row, column_anchor + indx_col, item)

切:

    def cut_selected_cells(self):
        copied_cells = sorted(self.tableWidget.selectedIndexes())
        copy_text = ''
        max_column = copied_cells[-1].column()
        max_row = copied_cells[-1].row()
        current_row = self.tableWidget.currentRow()
        for c in copied_cells:
            copy_text += self.tableWidget.item(c.row(), c.column()).text()
            if c.column() == max_column:
                if c.row() != max_row:
                    copy_text += '\n'
            else:
                copy_text += '\t'
        index_list = []
        for model_index in self.tableWidget.selectionModel().selectedRows():
            index = QPersistentModelIndex(model_index)
            index_list.append(index)
        for index in index_list:
            self.tableWidget.removeRow(index.row())
        QApplication.clipboard().setText(copy_text)
        self.tableWidget.selectRow(current_row)
© www.soinside.com 2019 - 2024. All rights reserved.