(self)
| 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 | # 填充数据 |
| 81 | for index in self.selectedIndexes(): # 遍历所有选择的 |
| 82 | arrays[index.row() - mirow][index.column() - micol] = index.data() |
| 83 | print(arrays) |
| 84 | data = "" # 最后的结果 |
| 85 | for row in arrays: |
| 86 | data += "\t".join(row) + "\r\n" |
| 87 | print(data) |
| 88 | QApplication.clipboard().setText(data) # 复制到剪贴板中 |
| 89 | QMessageBox.information(self, "提示", "已复制") |
| 90 | |
| 91 | def initHeader(self): |
| 92 | for i in range(5): |
no test coverage detected