python - 停止焦点陷入 QTableView
问题描述
MRE:
from PyQt5.QtCore import QRect, Qt, QAbstractTableModel
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView, QWidget, QVBoxLayout, QLineEdit, QListWidget
import sys, types
class MyTableModel( QAbstractTableModel ):
def __init__( self ):
super(MyTableModel, self).__init__()
data = [
[4, 9, 2],
[1, 0, 0],
[3, 5, 0],
]
self._data = data
def data(self, index, role):
if role == Qt.DisplayRole:
return self._data[index.row()][index.column()]
def rowCount(self, index):
return len(self._data)
def columnCount(self, index):
return len(self._data[0])
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(600, 700 )
self.centralwidget = QWidget(MainWindow)
self.verticalLayoutWidget = QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry( QRect(20, 20, 500, 500))
self.verticalLayout = QVBoxLayout(self.verticalLayoutWidget)
self.comps = []
self.title_line_edit = QLineEdit(self.verticalLayoutWidget)
self.comps.append( self.title_line_edit )
self.verticalLayout.addWidget(self.title_line_edit)
self.tags_list = QListWidget(self.verticalLayoutWidget)
self.comps.append( self.tags_list )
self.verticalLayout.addWidget(self.tags_list)
# UNCOMMENT THESE LINES:
# self.table_view = QTableView(self.verticalLayoutWidget)
# self.comps.append( self.table_view )
# self.table_view.setGeometry(QRect(20, 20, 200, 200))
# self.verticalLayout.addWidget(self.table_view)
# self.table_view.setModel( MyTableModel() )
self.title_line_edit2 = QLineEdit(self.verticalLayoutWidget)
self.comps.append( self.title_line_edit2 )
self.verticalLayout.addWidget(self.title_line_edit2)
MainWindow.setCentralWidget(self.centralwidget)
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def focus_in_event( self, event ):
self.original_stylesheet = self.styleSheet()
self.setStyleSheet( 'border:1px solid rgb(0, 255, 0);' )
def focus_out_event( self, event ):
self.setStyleSheet( self.original_stylesheet )
for comp in self.ui.comps:
comp.focusInEvent = types.MethodType( focus_in_event, comp )
comp.focusOutEvent = types.MethodType( focus_out_event, comp )
app = QApplication(sys.argv)
application = MyWindow()
application.show()
sys.exit(app.exec())
照原样,焦点选项卡很好地穿过组件。
但是,当您取消注释以添加QTableView
, 进入此组件时,意味着您无法再次退出。我尝试了 Shift-Tab(哪些选项卡在反向循环方向,但仍在表格单元格内),我尝试了 Ctrl-Tab/Ctrl-Shift-Tab(这似乎移动了当前单元格)。
我根本没有兴趣使用 Tab 来浏览这些单元格(箭头导航键做同样的事情),只是希望这种“诱捕”不会发生,所以我可以 Tab/Shift-Tab 以循环方式浏览所有组件像之前一样。
我尝试了几件事,将这些行添加到末尾MyWindow.__init__
:
# seemingly no effect:
def focus_next_prev_child( self, next ):
return False
self.ui.table_view.focusNextPrevChild = types.MethodType( focus_next_prev_child, comp )
# just omits the table view from the focus cycle:
self.ui.table_view.setFocusPolicy( Qt.NoFocus )
我还尝试拦截到达表格视图的击键(再次,将这些行添加到MyWindow.__init__
:
def keypress_event( self, key_event ):
modifs = key_event.modifiers()
key = key_event.key()
def unmodified_not_keypad():
print( f'unmodified, key: {hex( key )} text: {key_event.text()}' )
# strangely, if you press Shift-Tab, this gets to here (i.e. unmodified)
if key == Qt.Key_Tab or key == Qt.Key_Backtab:
# what might I do here?
# how might I find the next component in the cycle (forwards or backwards)?
return True
switcher = {
0x00000000: unmodified_not_keypad,
}
def print_invalid():
print( f'invalid, hex( modifs): {hex(modifs)}' )
# if any called function returns True this means skip the default action...
if switcher.get( int( modifs ), print_invalid )():
return
# execute default action:
QTableView.keyPressEvent( self, key_event )
self.ui.table_view.keyPressEvent = types.MethodType( keypress_event, self.ui.table_view )
这确实拦截了 Tab 和 Shift-Tab,并阻止了默认操作,但我不知道如何在焦点循环中找到正确的组件。此外,破解坚果似乎是一把大锤:大概有一种更优雅的方式来获得我想要的行为......
解决方案
如果您根本不想浏览单元格,请添加self.table_view.setTabKeyNavigation(False)
https://doc.qt.io/qt-5/qabstractitemview.html#tabKeyNavigation-prop
推荐阅读
- java - 这个带有黄瓜代码的硒的输出令人困惑
- android - 如何过滤 Kotlin 中的两个列表?
- java - 如何在我的 citrus HTTP 请求中发送图像?
- powershell - 取消执行的powershell脚本后如何取消NSIS安装程序?
- python - Raspberry Pi Python 继续循环移动函数,直到特定命令停止它
- php - Codeigniter 分页将我引导至“about:blank#blocked”
- java - 原因:java.net.URISyntaxException:索引 111 处的查询中存在非法字符:由于 URI 中的 {} 出现错误
- python - 从文件创建环境而无需其他依赖项
- reactjs - 我在哪里可以找到 FontAwesome 免费包中图标的对象名称?
- php - 索引页面不显示 db 的对象或有时显示未定义的变量