简体   繁体   English

使用QTabWidget默认使用Ctrl + Tab阻止选项卡循环

[英]Prevent tab cycling with Ctrl+Tab by default with QTabWidget

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. 默认情况下,我可以按Ctrl + Tab / Ctrl + Shift + Tab在选项卡之间循环。 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()

You can always install an eventFilter (similar to KeyPressEater here) 您始终可以安装eventFilter (类似于KeyPressEater

Here I did it: 在这里我做到了:

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_()

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. 我懒得为alt + tab或alt + shift + tab键找到合适的QtCore.Qt常量,所以我先听了,然后用python告诉我的替换。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM