[英]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.