Qt How to disable mouse scrolling of QComboBox?

14,262

Solution 1

As I found this question, when I tried to figure out the solution to (basically) the same issue: In my case I wanted to have a QComboBox in a QScrollArea in pyside (python QT lib).

Here my redefined QComboBox class:

#this combo box scrolls only if opend before.
#if the mouse is over the combobox and the mousewheel is turned,
# the mousewheel event of the scrollWidget is triggered
class MyQComboBox(QtGui.QComboBox):
    def __init__(self, scrollWidget=None, *args, **kwargs):
        super(MyQComboBox, self).__init__(*args, **kwargs)  
        self.scrollWidget=scrollWidget
        self.setFocusPolicy(QtCore.Qt.StrongFocus)

    def wheelEvent(self, *args, **kwargs):
        if self.hasFocus():
            return QtGui.QComboBox.wheelEvent(self, *args, **kwargs)
        else:
            return self.scrollWidget.wheelEvent(*args, **kwargs)

which is callable in this way:

self.scrollArea = QtGui.QScrollArea(self)
self.frmScroll = QtGui.QFrame(self.scrollArea)
cmbOption = MyQComboBox(self.frmScroll)

It is basically emkey08's answer in the link Ralph Tandetzky pointed out, but this time in python.

Solution 2

The same can happen to you in a QSpinBox or QDoubleSpinBox. On QSpinBox inside a QScrollArea: How to prevent Spin Box from stealing focus when scrolling? you can find a really good and well explained solution to the problem with code snippets.

Solution 3

You should be able to disable mouse wheel scroll by installing eventFilter on your QComboBox and ignore the events generated by mouse wheel, or subclass QComboBox and redefine wheelEvent to do nothing.

Share:
14,262
Alberto Toglia
Author by

Alberto Toglia

Updated on June 07, 2022

Comments

  • Alberto Toglia
    Alberto Toglia almost 2 years

    I have some embedded QComboBox in a QTableView. To make them show by default I made those indexes "persistent editor". But now every time I do a mouse scroll on top them they break my current table selection.

    So basically how can I disable mouse scrolling of QComboBox?

  • Alberto Toglia
    Alberto Toglia almost 14 years
    I also changed the combobox's focus policy to click. This helped too. Thanks!
  • 18C
    18C over 6 years
    But the QComboBox is STILL FOCUSED if I wheel over it. Why? How to prevent it? I want not to disable focus but only focus on wheel.
  • ElDog
    ElDog over 6 years
    This worked for me with a QSpinBox (replace QtGui.QComboBox with Qwidgets.QSpinBox), thanks!
  • Pat Corwin
    Pat Corwin almost 4 years
    For me an even better way is to call the event's ignore(). This will pass the event to the parent so you don't have to explicitly designate who receives the scroll. ``` def wheelEvent(self, event): if self.hasFocus(): return QtGui.QComboBox.wheelEvent(self, event) else: event.ignore()