Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

pyqt4 - Python Qt: How to catch "return" in qtablewidget

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...