I am trying to implement my own version of the QDoubleSpinBox to have it act like in Maya with int/FloatFields so you can Ctrl+click+drag to change the value.
I have this system working on a QLabel by setting the text from my value but then when I try to do it on a QDoubleSpinBox I have a problem with the mousePressEvent. It only works on the arrowButtons, not it the field itself...
here is my initial code :
class MayaLikeDoubleField(QtGui.QDoubleSpinBox):
def __init__(self, parent):
super(MayaLikeDoubleField, self).__init__()
self.offset = 0
def mousePressEvent(self, event):
self.offset = event.x()
def mouseMoveEvent(self, event):
modifiers = QtGui.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ControlModifier:
QtGui.QWidget.mouseMoveEvent(self, event)
relPos = event.x()-self.offset
print relPos*0.01
# instead of printing I would set the value.
In this case, it doesn' work. So I have tried this :
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.layout = QtGui.QHBoxLayout(self)
self.sbox = QtGui.QDoubleSpinBox()
self.layout.addWidget(self.line)
self.sbox.installEventFilter(self)
def mousePressEvent(self, event):
print "Main Widget Mouse Press"
super(Widget, self).mousePressEvent(event)
def eventFilter(self, obj, event):
modifiers = QtGui.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.ControlModifier:
if self.layout.indexOf(obj) != -1:
if event.type() == event.MouseButtonPress:
print "Widget click"
if event.type() == event.MouseMove:
print "Widget Move"
return super(Widget, self).eventFilter(obj, event)
And surprise ! it doesn't work ... but if i replace the QDoubleSpinBox by a QLineEdit it does work ... why ? for me it works the same way as by default the mousePressButton doesn't work on it...
Is there anything special that I am missing ?
Thanks !