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

Time:02-22

i'm confused about eventFilter. I want fully disable QTabWidget being scrolled when mouse is hovering and wheel scrolling on it.

For example, i made a eventFilter that does scrolling QScrollArea when mouse is hovering and wheeling at QTabWidget. It works however QTabWidget will scrolling and change value too.

I tried with return False, True, or event.ignore() it still did not work that make QTabWidget stop changing value.

Here is my eventFilter code:

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)

And this is my full code as example:

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

CodePudding user response:

The wheel event is managed by the tab bar, not the tab widget, so you need to install the filter on its tabBar():

    self.genreTabData.tabBar().installEventFilter(self)
  • Related