停止悬停时更改 QTabWidget 选项卡值并用滚轮滚动

Stopping QTabWidget tab value being changed when hovered and scrolling it with wheel

我对 eventFilter 感到困惑。我想完全禁用 QTabWidget 当鼠标悬停并在其上滚动滚轮时滚动。

例如,我制作了一个事件过滤器,当鼠标在 QTabWidget 处悬停和滚动时,它会滚动 QScrollArea。它可以工作,但是 QTabWidget 也会滚动并更改值。

我尝试使用 return False、True 或 event.ignore() 它仍然无法使 QTabWidget 停止更改值。

这是我的事件过滤器代码:

def eventFilter(self, obj, event):
    # If mouse hovering genreTabData and wheel did scrolling
    if obj is self.genreTabData and event.type() == QtCore.QEvent.Wheel:
        self.scrollArea.horizontalScrollBar().setValue(self.scrollArea.horizontalScrollBar().value() + -event.angleDelta().y())
        # Stop QTabWidget being change value after above code
        # Tried return False, True, and event.ignore() but did not work
    return super(mainForm, self).eventFilter(obj, event)

这是我的完整代码示例:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QEvent
import sys

class mainForm(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.genreTabData.installEventFilter(self)

    def setupUi(self, Form):
        Form.resize(400, 101)
        self.scrollArea = QtWidgets.QScrollArea(Form)
        self.scrollArea.setGeometry(QtCore.QRect(10, 10, 381, 81))
        self.scrollArea.setStyleSheet("QScrollBar:horizontal {height:12px;}\nQScrollBar:page:horizontal {border: 0px solid #999999;background-color: rgb(34, 35, 52);width:15px;margin: 0px 0px 0px 3px;}\nQScrollBar::handle:page:horizontal {min-height: 0px;border: 0px solid red;border-radius: 5px;background-color: rgb(92, 95, 141);}\nQScrollBar::add-line:horizontal {height: 0px;subcontrol-position: bottom;subcontrol-origin: margin;}\nQScrollBar::sub-line:horizontal {height: 0 px;subcontrol-position: top;subcontrol-origin: margin;}")
        self.scrollArea.setWidgetResizable(True)
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 584, 67))
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)

        self.genreTabData = QtWidgets.QTabWidget(self.scrollAreaWidgetContents)
        self.genreTabData.setMinimumSize(QtCore.QSize(0, 40))
        self.genreTabData.setStyleSheet("QTabWidget::tab-bar {border: 1px solid gray;}\nQTabBar::tab {height: 20px; width: 100px; background: #EFF4F8; color: black; padding: 10px; border-radius: 15px; margin-left:10px; margin-right:10px;}\nQTabBar::tab:selected {background: #c3daf5;}\nQTabWidget::pane {border: none;}")
        self.genreTabData.setUsesScrollButtons(False)

        tabs = ["Test tab", "Test tab", "Test tab", "Test tab"]
        for tab in tabs:
            tabWidget = QtWidgets.QWidget()
            self.genreTabData.addTab(tabWidget, tab)

        self.horizontalLayout.addWidget(self.genreTabData)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)

    def eventFilter(self, obj, event):
        if obj is self.genreTabData and event.type() == QtCore.QEvent.Wheel:
            self.scrollArea.horizontalScrollBar().setValue(self.scrollArea.horizontalScrollBar().value() + -event.angleDelta().y())
            return True
        return super(mainForm, self).eventFilter(obj, event)


app = QtWidgets.QApplication(sys.argv)
w = mainForm()
w.show()
sys.exit(app.exec_())

滚轮事件由选项卡栏管理,而不是选项卡小部件,因此您需要在其上安装过滤器 tabBar():

    self.genreTabData.tabBar().installEventFilter(self)