简体   繁体   English

如何使用Tab键从QTableWidget移至下一个小部件

[英]How to move from a QTableWidget to the next widget using the tab-key

I want to know how to move from a table-widget to the next widget when I press the tab key in PyQt. 我想知道当我在PyQt中按下Tab键时如何从表格小工具移至下一个小部件。

The current widget sequence is composed of line-edit > combo-box > table-widget > push-button. 当前的窗口小部件序列由行编辑>组合框>表小部件>按钮组成。 In the line-edit, when you press the tab key in the beginning, the combo-box > table widget is moved to in sequence. 在行编辑中,当您一开始按Tab键时,组合框>表格窗口小部件将依次移动到。 However, in the table-widget, repeatedly moving to the end, it moves to the 0,0 row and column of the table-widget again, not to the next widget (the push-button). 但是,在表小部件中,反复移动到末尾,它将再次移动到表小部件的0,0行和列,而不是下一个窗口小部件(按钮)。

How do I fix this? 我该如何解决?

There are two main ways to fix this. 有两种主要方法可以解决此问题。

The first, and simplest, way is to use setTabKeyNavigation to completely disable moving between cells using the tab-key. 第一种也是最简单的方法是使用setTabKeyNavigation完全禁止使用Tab键在单元格之间移动。 The arrow keys must then be used to navigate between cells in the table. 然后必须使用箭头键在表中的单元格之间导航。

The second way is to modify the keypress event handing for the table, so that Tab in the last cell and Shift Tab in the first cell moves the focus to the next/previous widget. 第二种方法是修改表的keypress事件处理,以便最后一个单元格中的Tab和第一个单元格中的Shift Tab将焦点移到下一个/上一个小部件。

The demo script below demonstrates both these approaches: 下面的演示脚本演示了这两种方法:

import sys
from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.edit = QtWidgets.QLineEdit()
        self.combo = QtWidgets.QComboBox()
        self.table = QtWidgets.QTableWidget(3, 3)
        self.button = QtWidgets.QPushButton('Disable Table Tabbing', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtWidgets.QGridLayout(self)
        layout.addWidget(self.edit, 0, 0)
        layout.addWidget(self.combo, 0, 1)
        layout.addWidget(self.table, 1, 0, 1, 2)
        layout.addWidget(self.button, 2, 0, 1, 2)
        self.table.installEventFilter(self)
        self.edit.setFocus()

    def handleButton(self):
        if self.table.tabKeyNavigation():
            self.button.setText('Enable Table Tabbing')
            self.table.setTabKeyNavigation(False)
        else:
            self.button.setText('Disable Table Tabbing')
            self.table.setTabKeyNavigation(True)

    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.KeyPress and
            source is self.table and source.isEnabled() and
            source.tabKeyNavigation()):
            index = self.table.currentIndex()
            if event.key() == QtCore.Qt.Key_Backtab:
                if index.row() == index.column() == 0:
                    QtWidgets.QAbstractScrollArea.focusNextPrevChild(
                        self.table, False)
                    return True
            elif event.key() == QtCore.Qt.Key_Tab:
                model = self.table.model()
                if (index.row() == model.rowCount() - 1 and
                    index.column() == model.columnCount() - 1):
                    QtWidgets.QAbstractScrollArea.focusNextPrevChild(
                        self.table, True)
                    return True
        return super(Window, self).eventFilter(source, event)

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 100, 400, 250)
    window.show()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Tab键发送到python子进程的stdin - how to send tab-key to python subprocess's stdin 按 T​​ab 键时移动到下一个选项卡(并专注于相应的小部件) - Move to next tab (and focus on the corresponding widget) when pressing the Tab key 使用 spinbox 小部件 (PyQt5) 从 QTableWidget 移动项目和读取值 - Move items and read values from QTableWidget with spinbox widget (PyQt5) 让Enter键的行为类似于QTableWidget中的Tab - Have Enter key behave like Tab in QTableWidget 如何通过单击 QPushButton 移动到下一个选项卡(或导航到另一个选项卡)? - How to move to next tab (or navigate to another tab) on the click of a QPushButton? 如何使用QThread从慢速Sql语句填充自定义QTableWidget? - How to populate a custom QTableWidget from a slow Sql statement using QThread? Python 传统知识。 使用 Tab 键时设置小部件焦点 - Python tk. Setting widget focus when using tab key 在Tkinter中按下Tab键后,如何捕获文本小部件的值? - How to Capture the Value of a Text Widget after the Tab Key is Pressed in Tkinter? 如何通过单击QtableWidget中的Delete键删除行? - How to remove a row by hitting delete key in QtableWidget? 如何将值从“QTableWidget”打印到另一个“QTableWidget”? - How can I print values from "QTableWidget" to another "QTableWidget"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM