2

I would like to catch the return key in a qtablewidget to do something with the currently marked cell. That is: I want the user to press the "return/enter" key on his keyboard when any cell is highligted. Pressing that button should issue a new method. For example show a messagebox with the content of that cell.

How do I connect the event of pressing the return key to a method?

Since I am new to python I have no idea how to do that and would be grateful for any advice.

jb.
  • 23,300
  • 18
  • 98
  • 136
user3692467
  • 33
  • 1
  • 4

2 Answers2

3

Your question is a little ambiguous. What does 'catch the return key' mean? QTableWidget has several methods that return information.

If you wanted to get the current cell's text you could simply do:

my_table.currentItem().text()

UPDATE

In your comment below, you specified that you want the user to be able to press Enter or Return and then be able to process the current items information.

To do this you create a subclass of QTableWidget and override its keyPressEvent method. Some of the inspiration came from here:

class MyTableWidget(QTableWidget):
    def __init__(self, parent=None):
        super(MyTableWidget, self).__init__(parent)

    def keyPressEvent(self, event):
         key = event.key()

         if key == Qt.Key_Return or key == Qt.Key_Enter:
             # Process current item here
         else:
             super(MyTableWidget, self).keyPressEvent(event)
Community
  • 1
  • 1
BeetDemGuise
  • 954
  • 7
  • 11
  • Thanks for your answer, Darin. I'm sorry for not describing clearly what I wanted to say. I want the user to press the "return/enter" key on his keyboard when any cell is highligted. Pressing that button should issue a new method. How do I connect the event of pressing the return key to a method? – user3692467 May 30 '14 at 19:12
  • Thanks again, it now works as intended. But pressing any other key than Return throws me this error: `super(MyTableWidget, self).keyPressEvent(self, event) TypeError: QAbstractItemView.keyPressEvent(QKeyEvent): argument 1 has unexpected type 'MyTableWidget'` I think there is something wrong with the last line of code... – user3692467 Jun 01 '14 at 17:11
  • I believe I looked at outdated documentation. If you remove the `self` argument from the line `super(MyTableWidget, self).keyPressEvent(self, event)`, the code should work. I have updated my answer. – BeetDemGuise Jun 01 '14 at 21:03
2
  • here is an example of catching key presses in a QTableWidget
  • i have used it to implement things like allowing the user to select multiple rows and then hit the space bar to set or reset the check boxes in multiple table rows
class yourClass(QWidget):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)

        self.mLastKeyState = 0
    
        # construct the table
        self.mPlTable = QTableWidget()
        self.mPlTable.keyPressEvent = self.tableOnKeyPressEvent  # or keyReleaseEvent
    
    def tableOnKeyPressEvent(self, event):
        if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter
            # do what needs to be done here
            # you can return here if you do not want to pass on the return to the table

        # pass on the keyPressEvent to the table
        QTableWidget.keyPressEvent(self.mPlTable, event) 

Louie
  • 91
  • 4