事件 - 仅捕获 Qt.Key_Delete
我想更改 QTableWidget 的 Key_Delete 行为,但由于使用 Designer,我不想使用表单对 py 文件进行任何更改。因此,不要像这样重新实现 QTableWidget,而是这样的答案: 如何在 PyQt 中为 Qt-Designer 小部件实现 MousePressEvent 我做了这样的事情:
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.tableWidget.__class__.keyPressEvent = self.test
def test(self,event):
if event.key() == Qt.Key_Delete:
print "test"
return QTableWidget.keyPressEvent(self, event)
问题是我不知道如何保持其他键的原始行为Qt.Key_Delete。 我已经像这样更改了最后一行:
return QtGui.QTableWidget.keyPressEvent(self, event)
return QtGui.QTableWidget.event(self, event)
但它不起作用。
I would like to change behavior of Key_Delete for QTableWidget, but because of using Designer I don't want to make any changes in py file with form. So instead of reimplementing QTableWidget like it is this answer: How to implement MousePressEvent for a Qt-Designer Widget in PyQt I do something like this:
class MyForm(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.tableWidget.__class__.keyPressEvent = self.test
def test(self,event):
if event.key() == Qt.Key_Delete:
print "test"
return QTableWidget.keyPressEvent(self, event)
The problem is that I don't know how to keep original behavior of other keys than Qt.Key_Delete.
I have already changed last line like this:
return QtGui.QTableWidget.keyPressEvent(self, event)
return QtGui.QTableWidget.event(self, event)
but it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先:“但这不起作用”通常描述性不够。您期望什么行为?您看到的行为是什么?有任何错误消息吗?
不过,我很容易在这里发现一些错误。
您正在重写方法 QTableWidget.keyPressEvent,该方法需要 2 个参数:(QTableWidget 实例、事件)。但在上面显示的代码中,您用来覆盖它的函数只需要 1 个参数(第一个参数“self”不计算在内,因为它是自动提供的)。
由于您已设置 QTableWidget.keyPressEvent = self.test,并且您还尝试在 self.test() 内调用此函数,因此您创建了一个无限递归函数。
当你调用QTableWidget.keyPressEvent时,你传入的第一个参数(self)是一个QMainWindow对象。然而,正如我上面提到的,这个函数需要一个 QTableWidget 作为它的第一个参数。
由于您在类级别重写此方法,因此所有 QTableWidget 将被迫使用相同的 keyPressEvent 函数(这将是非常有问题的)。相反,您应该仅重写特定小部件的方法:
self.ui.tableWidget.keyPressEvent = self.test
(另请注意,tableWidget.keyPressEvent 的签名与 QTableWidget.keyPressEvent 不同)示例:
最后,另一种(可能更干净)的方法是创建 QTableWdget 的子类,并在 Designer 中将表格小部件“升级”到您的新类。
First: "but it doesn't work" is usually not descriptive enough. What behavior did you expect? What was the behavior you saw instead? Were there any error messages?
I can easily see a few mistakes in here, though.
You're overriding the method QTableWidget.keyPressEvent, which expects 2 arguments: (QTableWidget instance, event). But in the code you show above, the function you are using to override it only takes 1 argument (the first argument, 'self', does not count since it is automatically supplied).
Since you have set QTableWidget.keyPressEvent = self.test, and you are also trying to call this function from within self.test(), you have created an infinitely recursive function.
When you call QTableWidget.keyPressEvent, the first argument you have passed in (self) is a QMainWindow object. However as I mentioned above, this function expects a QTableWidget as its first argument.
Since you are overriding this method at the class level, ALL QTableWidgets will be forced to use the same keyPressEvent function (this would be very problematic). Instead, you should override just your specific widget's method:
self.ui.tableWidget.keyPressEvent = self.test
(also note that the signature for tableWidget.keyPressEvent is different from QTableWidget.keyPressEvent)Example:
Finally, another (possibly cleaner) approach would be to create a subclass of QTableWdget and, within Designer, 'promote' the table widget to your new class.