Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt How to disable mouse scrolling of QComboBox?

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?

like image 330
Alberto Toglia Avatar asked Jul 13 '10 21:07

Alberto Toglia


3 Answers

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.

like image 53
Davor Lucic Avatar answered Sep 20 '22 16:09

Davor Lucic


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.

like image 40
Markus Dutschke Avatar answered Sep 17 '22 16:09

Markus Dutschke


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.

like image 39
Ralph Tandetzky Avatar answered Sep 17 '22 16:09

Ralph Tandetzky