Python Qt:如何捕捉“返回"在 qtablewidget 中 [英] Python Qt: How to catch "return" in qtablewidget

查看:30
本文介绍了Python Qt:如何捕捉“返回"在 qtablewidget 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 qtablewidget 中捕获返回键,以对当前标记的单元格执行某些操作.也就是说:当任何单元格被高亮显示时,我希望用户按下键盘上的返回/输入"键.按下那个按钮应该会发出一个新的方法.例如,显示包含该单元格内容的消息框.

如何将按下返回键的事件连接到方法?

由于我是 Python 新手,我不知道该怎么做,如果您有任何建议,我将不胜感激.

解决方案

你的问题有点模棱两可.抓住返回键"是什么意思?QTableWidget 有几个返回信息的方法.

如果您想获取当前单元格的文本,您可以简单地执行以下操作:

my_table.currentItem().text()

更新

在您下面的评论中,您指定您希望用户能够按 EnterReturn 然后能够处理当前项目信息.>

为此,您需要创建 QTableWidget 的子类并覆盖其 keyPressEvent 方法.部分灵感来自此处:

class MyTableWidget(QTableWidget):def __init__(self, parent=None):super(MyTableWidget, self).__init__(parent)def keyPressEvent(self, event):键 = event.key()如果 key == Qt.Key_Return 或 key == Qt.Key_Enter:# 在这里处理当前项目别的:super(MyTableWidget, self).keyPressEvent(event)

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.

解决方案

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)

这篇关于Python Qt:如何捕捉“返回"在 qtablewidget 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆