2

I have QSpinBox's Signal valueChanged connected to a QWidget's function like:

class MyWidget(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        #just an example
        mySpinBox = QtGui.QSpinBox()
        mySpinBox.valueChanged.connect(self.foo)

   def foo(self):
       if value was changed by Mouse Wheel:
            #do this
       else:
            #do nothing 
tobilocker
  • 891
  • 8
  • 27

2 Answers2

1

Derive QSpinBox and override the wheelevent

http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#wheelEvent

You can define your own signal which will be emitted by the wheelevent to obtain the behavior you want to have.

For an example / tutorial, see: http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/

Gombat
  • 1,994
  • 16
  • 21
0

Based on Gombat's answer and this question (because how it is explained in provided tutorial it wil raise an error) i did this in the widget

class MyWidget(QtGui.QWidget):
    mySignal1 = QtCore.pyqtSignal() #these have to be here or... 
    mySignal2 = QtCore.pyqtSignal() #...there will be an attribute error

    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        #just an example
        self.mySpinBox1 = QtGui.QSpinBox()
        self.mySpinBox2 = QtGui.QSpinBox()
        self.mySpinBox1.installEventFilter(self)
        self.mySpinBox2.installEventFilter(self)
        self.mySignal1.connect(self.foo)
        self.mySignal2.connect(self.bar)

   def eventFilter(self, source, event):
       if source is self.spinBox1:
           if event.type() == QtCore.QEvent.Wheel:
                self.spinBox1.wheelEvent(event)
                self.mySignal1.emit()
           return True
       else:
           return False
    elif source is self.spinBox2:
        if event.type() == QtCore.QEvent.Wheel:
            self.spinBox2.wheelEvent(event)
            self.mySignal2.emit()
            return True
        else:
            return False
    else:
        return QtGui.QWidget.eventFilter(self, source, event)

    def foo(self):
        #right here, when a mouse wheel event occured in spinbox1

    def bar(self):
        #right here, when a mouse wheel event occured in spinbox2

hope this helps. thanks (untested, because it is just an example, there might be errors)

Community
  • 1
  • 1
tobilocker
  • 891
  • 8
  • 27