事件过滤器不断被随机触发
Event Filter keeps getting triggered randomly
我使用事件过滤器将项目复制并粘贴到我的 table 小部件。复制粘贴功能正常
我目前遇到的问题是事件过滤器不断被未以任何方式连接到它的函数触发。这最终会引发错误。
事件过滤器块是:
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.KeyPress and
event.matches(QtGui.QKeySequence.Copy)):
self.copySelection()
return True
return super(MatplotlibWidget, self).eventFilter(source, event)
def copySelection(self):
if self.tabWidget.currentIndex() == 0:
self.rand_table_widget = self.tableWidget
elif self.tabWidget.currentIndex() == 1:
self.rand_table_widget = self.tableWidget_2
elif self.tabWidget.currentIndex() == 2:
self.rand_table_widget = self.tableWidget_3
elif self.tabWidget.currentIndex() == 3:
self.rand_table_widget = self.tableWidget_4
self.copied_cells = self.rand_table_widget.selectedIndexes()
if self.copied_cells:
rows = sorted(index.row() for index in self.copied_cells)
columns = sorted(index.column() for index in self.copied_cells)
rowcount = rows[-1] - rows[0] + 1
colcount = columns[-1] - columns[0] + 1
table = [[''] * colcount for _ in range(rowcount)]
for index in self.copied_cells:
row = index.row() - rows[0]
column = index.column() - columns[0]
table[row][column] = index.data()
stream = io.StringIO()
csv.writer(stream).writerows(table)
QtWidgets.qApp.clipboard().setText(stream.getvalue())
def keyPressEvent(self, event):
if self.tabWidget.currentIndex() == 0:
self.rand_table_widget = self.tableWidget
elif self.tabWidget.currentIndex() == 1:
self.rand_table_widget = self.tableWidget_2
elif self.tabWidget.currentIndex() == 2:
self.rand_table_widget = self.tableWidget_3
elif self.tabWidget.currentIndex() == 3:
self.rand_table_widget = self.tableWidget_4
super().keyPressEvent(event)
if event.key() == Qt.Key_C and (event.modifiers() & Qt.ControlModifier):
self.copied_cells = sorted(self.rand_table_widget.selectedIndexes())
elif event.key() == Qt.Key_V and (event.modifiers() & Qt.ControlModifier):
r = self.rand_table_widget.currentRow() - self.copied_cells[0].row()
c = self.rand_table_widget.currentColumn() - self.copied_cells[0].column()
for cell in self.copied_cells:
self.rand_table_widget.setItem(cell.row() + r, cell.column() + c, QTableWidgetItem(cell.data()))
self.tableWidget.installEventFilter(self)
self.tableWidget_2.installEventFilter(self)
self.tableWidget_3.installEventFilter(self)
self.tableWidget_4.installEventFilter(self)
self.tableWidget_6.installEventFilter(self)
我得到的错误是:
line 297, in eventFilter
return super(MatplotlibWidget, self).eventFilter(source, event)
TypeError: super(type, obj): obj must be an instance or subtype of type
问题是什么?
而不是 return super(MatplotlibWidget, self).eventFilter(source, event)
你可以尝试更短的版本 return super().eventFilter(source, event)
或只是 return False
(但是不好的做法)。
我建议将 eventFilter
从 widget 移开以分离 class,这样它就可以重用并满足“单一责任”原则。
我使用事件过滤器将项目复制并粘贴到我的 table 小部件。复制粘贴功能正常
我目前遇到的问题是事件过滤器不断被未以任何方式连接到它的函数触发。这最终会引发错误。
事件过滤器块是:
def eventFilter(self, source, event):
if (event.type() == QtCore.QEvent.KeyPress and
event.matches(QtGui.QKeySequence.Copy)):
self.copySelection()
return True
return super(MatplotlibWidget, self).eventFilter(source, event)
def copySelection(self):
if self.tabWidget.currentIndex() == 0:
self.rand_table_widget = self.tableWidget
elif self.tabWidget.currentIndex() == 1:
self.rand_table_widget = self.tableWidget_2
elif self.tabWidget.currentIndex() == 2:
self.rand_table_widget = self.tableWidget_3
elif self.tabWidget.currentIndex() == 3:
self.rand_table_widget = self.tableWidget_4
self.copied_cells = self.rand_table_widget.selectedIndexes()
if self.copied_cells:
rows = sorted(index.row() for index in self.copied_cells)
columns = sorted(index.column() for index in self.copied_cells)
rowcount = rows[-1] - rows[0] + 1
colcount = columns[-1] - columns[0] + 1
table = [[''] * colcount for _ in range(rowcount)]
for index in self.copied_cells:
row = index.row() - rows[0]
column = index.column() - columns[0]
table[row][column] = index.data()
stream = io.StringIO()
csv.writer(stream).writerows(table)
QtWidgets.qApp.clipboard().setText(stream.getvalue())
def keyPressEvent(self, event):
if self.tabWidget.currentIndex() == 0:
self.rand_table_widget = self.tableWidget
elif self.tabWidget.currentIndex() == 1:
self.rand_table_widget = self.tableWidget_2
elif self.tabWidget.currentIndex() == 2:
self.rand_table_widget = self.tableWidget_3
elif self.tabWidget.currentIndex() == 3:
self.rand_table_widget = self.tableWidget_4
super().keyPressEvent(event)
if event.key() == Qt.Key_C and (event.modifiers() & Qt.ControlModifier):
self.copied_cells = sorted(self.rand_table_widget.selectedIndexes())
elif event.key() == Qt.Key_V and (event.modifiers() & Qt.ControlModifier):
r = self.rand_table_widget.currentRow() - self.copied_cells[0].row()
c = self.rand_table_widget.currentColumn() - self.copied_cells[0].column()
for cell in self.copied_cells:
self.rand_table_widget.setItem(cell.row() + r, cell.column() + c, QTableWidgetItem(cell.data()))
self.tableWidget.installEventFilter(self)
self.tableWidget_2.installEventFilter(self)
self.tableWidget_3.installEventFilter(self)
self.tableWidget_4.installEventFilter(self)
self.tableWidget_6.installEventFilter(self)
我得到的错误是:
line 297, in eventFilter
return super(MatplotlibWidget, self).eventFilter(source, event)
TypeError: super(type, obj): obj must be an instance or subtype of type
问题是什么?
而不是 return super(MatplotlibWidget, self).eventFilter(source, event)
你可以尝试更短的版本 return super().eventFilter(source, event)
或只是 return False
(但是不好的做法)。
我建议将 eventFilter
从 widget 移开以分离 class,这样它就可以重用并满足“单一责任”原则。