问题描述
我对编程很陌生.请在这方面帮助我.提前致谢.
I am very new in programming. Please help me in this regards. Thanks in advance.
这是我的代码:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def clear_table(self):
self.tableWidget_2.clearContents()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1080)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.tableWidget_2 = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget_2.setGeometry(QtCore.QRect(10, 100, 1800, 700))
self.tableWidget_2.setObjectName("tableWidget_2")
self.tableWidget_2.setColumnCount(3)
self.tableWidget_2.setRowCount(500)
# self.tableWidget_2.cellChanged.connect(self.c_current)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(300, 20, 150, 50))
self.pushButton_2.setText("Import table")
self.pushButton_2.setObjectName("pushButton_2")
#####
###
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(10, 20, 150, 50))
self.pushButton.setText("Clear table")
self.pushButton.clicked.connect(self.clear_table)
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 18))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
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_())
输出表:
比如我想把这个数据表粘贴到qt小部件表中:
For example, I want to paste this data table into the qt widget table:
1 2 3
6 7 8
10 11 12
同样,我也想使用 CLT+C 或鼠标右键单击并复制从 qt 表中复制出整个数据.像这样:
Similarly I also want to copy out the whole data from qt table using CLT+C or mouse right button click and the copy.like this:
推荐答案
如果只需要内部复制粘贴操作,可以使用局部变量来存储项目数据(行、列和内容),然后使用事件过滤以触发这些操作.
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 ( or ) 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
class Window(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 = []
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.KeyPress:
if event == QtGui.QKeySequence.Copy:
self.copySelection()
return True
elif event == QtGui.QKeySequence.Paste:
self.pasteSelection()
return True
elif 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)
if not self.tableView.selectedIndexes():
# no selection available, both copy and paste are disabled
copyAction.setEnabled(False)
pasteAction.setEnabled(False)
if not self.clipboard:
# no clipboard contents, paste is disabled
pasteAction.setEnabled(False)
menu.exec(event.globalPos())
return True
return super(Window, self).eventFilter(source, event)
def copySelection(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()))
def pasteSelection(self):
if not self.clipboard:
return
current = self.tableView.currentIndex()
if not 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_())
请注意,我没有使用您的代码,因为这是从 pyuic
生成的输出,它应该永远被修改.这些文件仅用于导入,并且程序逻辑必须在单独的脚本中实现.阅读有关使用 Designer 的文档,了解如何正确使用 UI 文件,否则只需按照我的示例中的方法对 qwidget 进行子类化来创建接口.
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.
这篇关于如何在qt表格小部件中粘贴和复制多个单元格值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!