-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyTableView.py
120 lines (106 loc) · 4.28 KB
/
MyTableView.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# MyTableView.py
from PyQt5.QtWidgets import QApplication, QMenu
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItem, QCursor
from PyQt5 import QtWidgets
class MyTableView(QtWidgets.QTableView):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setContextMenuPolicy(Qt.CustomContextMenu)
# 创建QMenu信号事件
self.customContextMenuRequested.connect(self.showMenu)
self.contextMenu = QMenu(self)
self.CP = self.contextMenu.addAction('复制')
self.JQ = self.contextMenu.addAction('剪切')
self.NT = self.contextMenu.addAction('粘贴')
self.CP.triggered.connect(self.copy)
self.JQ.triggered.connect(self.cut)
self.NT.triggered.connect(self.paste)
def del_tb_text(self):
try:
indexes = self.selectedIndexes()
for index in indexes:
row, column = index.row(), index.column()
model = self.model()
item = QStandardItem()
model.setItem(row, column, item)
self.setModel(model)
except BaseException as e:
print(e)
return
def paste_tb_text(self):
try:
indexes = self.selectedIndexes()
for index in indexes:
index = index
break
r, c = index.row(), index.column()
text = QApplication.clipboard().text()
ls = text.split('\n')
ls1 = []
for row in ls:
ls1.append(row.split('\t'))
model = self.model()
rows = len(ls)
columns = len(ls1[0])
for row in range(rows):
for column in range(columns):
item = QStandardItem()
item.setText((str(ls1[row][column])))
model.setItem(row + r, column + c, item)
except Exception as e:
print(e)
return
def selected_tb_text(self):
try:
indexes = self.selectedIndexes() # 获取表格对象中被选中的数据索引列表
indexes_dict = {}
for index in indexes: # 遍历每个单元格
row, column = index.row(), index.column() # 获取单元格的行号,列号
if row in indexes_dict.keys():
indexes_dict[row].append(column)
else:
indexes_dict[row] = [column]
# 将数据表数据用制表符(\t)和换行符(\n)连接,使其可以复制到excel文件中
text = ''
for row, columns in indexes_dict.items():
row_data = ''
for column in columns:
data = self.model().item(row, column).text()
if row_data:
row_data = row_data + '\t' + data
else:
row_data = data
if text:
text = text + '\n' + row_data
else:
text = row_data
return text
except BaseException as e:
print(e)
return ''
def copy(self):
text = self.selected_tb_text() # 获取当前表格选中的数据
if text:
clipboard = QApplication.clipboard()
clipboard.setText(text)
# pyperclip.copy(text) # 复制数据到粘贴板
def cut(self):
self.copy()
self.del_tb_text()
def paste(self):
self.paste_tb_text()
def showMenu(self, pos):
# pos 鼠标位置
# 菜单显示前,将它移动到鼠标点击的位置
self.contextMenu.exec_(QCursor.pos()) # 在鼠标位置显示
def keyPressEvent(self, event): # 重写键盘监听事件
# 监听 CTRL+C 组合键,实现复制数据到粘贴板
if (event.key() == Qt.Key_C) and QApplication.keyboardModifiers() == Qt.ControlModifier:
self.copy() # 获取当前表格选中的数据
elif (event.key() == Qt.Key_X) and QApplication.keyboardModifiers() == Qt.ControlModifier:
self.cut()
elif (event.key() == Qt.Key_V) and QApplication.keyboardModifiers() == Qt.ControlModifier:
self.paste()
else:
super().keyPressEvent(event)