使用 QTabWidget 默认使用 Ctrl+Tab 防止选项卡循环 [英] Prevent tab cycling with Ctrl+Tab by default with QTabWidget
问题描述
我有以下示例代码,可制作三选项卡布局(第三个选项卡上有按钮).默认情况下,我可以 Ctrl+Tab/Ctrl+Shift+Tab在选项卡之间循环.如何禁用此功能?在我的非示例代码中,这不是我们想要的行为.
I have the following example code that makes a three tab layout (with buttons on the third tab). By default, I can Ctrl+Tab/Ctrl+Shift+Tab to cycle between the tabs. How do I disable this functionality? In my non-example code, this is not desired behaviour.
from PyQt4 import QtGui
import sys
def main():
app = QtGui.QApplication(sys.argv)
tabs = QtGui.QTabWidget()
push_button1 = QtGui.QPushButton("QPushButton 1")
push_button2 = QtGui.QPushButton("QPushButton 2")
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()
tab3 = QtGui.QWidget()
vBoxlayout = QtGui.QVBoxLayout()
vBoxlayout.addWidget(push_button1)
vBoxlayout.addWidget(push_button2)
tabs.resize(250, 150)
tabs.move(300, 300)
tab3.setLayout(vBoxlayout)
tabs.addTab(tab1, "Tab 1")
tabs.addTab(tab2, "Tab 2")
tabs.addTab(tab3, "Tab 3")
tabs.setWindowTitle('PyQt QTabWidget Add Tabs and Widgets Inside Tab')
tabs.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
推荐答案
您始终可以安装 eventFilter
(类似于 KeyPressEater 在这里)
You can always install an eventFilter
(similar to KeyPressEater here)
我做到了:
from PySide import QtGui, QtCore
class AltTabPressEater(QtCore.QObject):
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress and (event.key() == 16777217 or event.key() == 16777218):
return True # eat alt+tab or alt+shift+tab key
else:
# standard event processing
return QtCore.QObject.eventFilter(self, obj, event)
app = QtGui.QApplication([])
tabs = QtGui.QTabWidget()
filter = AltTabPressEater()
tabs.installEventFilter(filter)
push_button1 = QtGui.QPushButton("QPushButton 1")
push_button2 = QtGui.QPushButton("QPushButton 2")
tab1 = QtGui.QWidget()
tab2 = QtGui.QWidget()
tab3 = QtGui.QWidget()
vBoxlayout = QtGui.QVBoxLayout()
vBoxlayout.addWidget(push_button1)
vBoxlayout.addWidget(push_button2)
tabs.resize(250, 150)
tabs.move(300, 300)
tab3.setLayout(vBoxlayout)
tabs.addTab(tab1, "Tab 1")
tabs.addTab(tab2, "Tab 2")
tabs.addTab(tab3, "Tab 3")
tabs.show()
app.exec_()
我懒得为 alt+tab 或 alt+shift+tab 键找到正确的 QtCore.Qt
常量,所以我只是先听听,然后用 python 告诉我的内容替换.
I was too lazy to find the right QtCore.Qt
constants for the alt+tab or alt+shift+tab keys, so I just listened first and then replaced by what python told me.
这篇关于使用 QTabWidget 默认使用 Ctrl+Tab 防止选项卡循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!