开发者

Event - catch only Qt.Key_Delete

开发者 https://www.devze.com 2023-02-27 10:36 出处:网络
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

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 开发者_Python百科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.


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.

  1. 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).

  2. 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.

  3. 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.

  4. 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:

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
win = QtGui.QMainWindow()
win.show()

table = QtGui.QTableWidget()
win.setCentralWidget(table)
def test(event):
    if event.key() == QtCore.Qt.Key_Delete:
        print "delete"
    return QtGui.QTableWidget.keyPressEvent(table, event)
table.keyPressEvent = test

Finally, another (possibly cleaner) approach would be to create a subclass of QTableWdget and, within Designer, 'promote' the table widget to your new class.

0

精彩评论

暂无评论...
验证码 换一张
取 消