QDoubleSpinBox + MousePressEvent

时间:2015-09-23 19:14:51

标签: qspinbox mousepress

我正在尝试实现我自己的QDoubleSpinBox版本,使其在Maya中使用int / FloatFields,因此您可以按Ctrl +单击+拖动来更改值。

我通过从我的值设置文本来使用此系统处理QLabel但是当我尝试在QDoubleSpinBox上执行此操作时,我遇到了mousePressEvent的问题。它只适用于arrowButtons,而不是字段本身......

这是我的初始代码:

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.

在这种情况下,它没有'工作。所以我试过这个:

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)

惊喜!它不起作用......但如果我用QLineEdit替换QDoubleSpinBox它确实有效......为什么?对我而言,它的工作方式与默认情况下一样,mousePressButton不会对它起作用......

我遗失了什么特别的东西?

谢谢!

0 个答案:

没有答案