Python Qt: How to catch “return” in qtablewidget

不羁的心 提交于 2019-11-28 06:30:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!