Important: Please read the Qt Code of Conduct -

QDoubleSpinBox + mousePressEvent

  • Hi Qt comunity !

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

  • For the time being, I will re create the QspinBox with a QLineEdit so I will get working. But I don't give up on finding why it doesn't work.

  • Lifetime Qt Champion


    You should try to put your filter on the QLineEdit from the QDoubleSpinBox. You can get it using QDoubleSpinBox::lineEdit method

    Hope it helps

  • @SGaist
    It doesn't work, here is the raised error :
    'builtin_function_or_method' object has no attribute 'installEventFilter'

    but was a nice try.

  • Lifetime Qt Champion

    Didn't you forgot the parenthesis ?

  • @SGaist
    nope i didn't but I forgot to save changes ^^ but stil, it doesn't work. no error raised this time and still not displaying the click print.

  • from my second example, I need to access the focusOutEvent of my QLineEdit, how can I reach that ? I don't want to subclass QLineEdit ...

  • Lifetime Qt Champion

    Again, the filter is your friend, QFocusEvent gives you that information.

  • @SGaist
    I have tried to add a focusOut event in my loop but it doesn't detect it ...

    if event.type() == event.FocusOut:
        print "Widget focusOut"

Log in to reply