MCPcopy Index your code
hub / github.com/PyQt5/PyQt / TableView

Class TableView

QTableView/CopyContent.py:23–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21
22
23class TableView(QTableView):
24
25 def __init__(self, parent=None):
26 super(TableView, self).__init__(parent)
27 self.resize(800, 600)
28 self.setContextMenuPolicy(Qt.ActionsContextMenu) # 右键菜单
29 self.setEditTriggers(self.NoEditTriggers) # 禁止编辑
30 self.doubleClicked.connect(self.onDoubleClick)
31 self.addAction(QAction("复制", self, triggered=self.copyData))
32 self.myModel = QStandardItemModel() # model
33 self.initHeader() # 初始化表头
34 self.setModel(self.myModel)
35 self.initData() # 初始化模拟数据
36
37 def onDoubleClick(self, index):
38 print(index.row(), index.column(), index.data())
39
40 def keyPressEvent(self, event):
41 super(TableView, self).keyPressEvent(event)
42 # Ctrl + C
43 if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_C:
44 self.copyData()
45
46 def copyData(self):
47 count = len(self.selectedIndexes())
48 if count == 0:
49 return
50 if count == 1: # 只复制了一个
51 QApplication.clipboard().setText(
52 self.selectedIndexes()[0].data()) # 复制到剪贴板中
53 QMessageBox.information(self, "提示", "已复制一个数据")
54 return
55 rows = set()
56 cols = set()
57 for index in self.selectedIndexes(): # 得到所有选择的
58 rows.add(index.row())
59 cols.add(index.column())
60 # print(index.row(),index.column(),index.data())
61 if len(rows) == 1: # 一行
62 QApplication.clipboard().setText("\t".join(
63 [index.data() for index in self.selectedIndexes()])) # 复制
64 QMessageBox.information(self, "提示", "已复制一行数据")
65 return
66 if len(cols) == 1: # 一列
67 QApplication.clipboard().setText("\r\n".join(
68 [index.data() for index in self.selectedIndexes()])) # 复制
69 QMessageBox.information(self, "提示", "已复制一列数据")
70 return
71 mirow, marow = min(rows), max(rows) # 最(少/多)行
72 micol, macol = min(cols), max(cols) # 最(少/多)列
73 print(mirow, marow, micol, macol)
74 arrays = [
75 [
76 "" for _ in range(macol - micol + 1)
77 ] for _ in range(marow - mirow + 1)
78 ] # 创建二维数组(并排除前面的空行和空列)
79 print(arrays)
80 # 填充数据

Callers 1

CopyContent.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected