问题
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.
回答1:
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)
来源:https://stackoverflow.com/questions/23961355/python-qt-how-to-catch-return-in-qtablewidget