Skip to content

Instantly share code, notes, and snippets.

@justinfx
Created March 9, 2012 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinfx/2007194 to your computer and use it in GitHub Desktop.
Save justinfx/2007194 to your computer and use it in GitHub Desktop.
Example of how to set up a widget that replicates a Maya attrFieldSliderGrp
class AttrFieldGroup(QtGui.QWidget):
"""
AttrFieldGroup
Resembles a attrFieldSliderGrp widget in the Maya UI.
This is a basic example. You could go further in setting
up a common setRange() to configure both the text input and
the slider together.
"""
# custom signal for when any value changes
valueChanged = QtCore.pyqtSignal(float)
def __init__(self, parent=None):
super(AttrFieldGroup, self).__init__(parent)
self.layout = QtGui.QHBoxLayout(self)
# You could create a self.setTextLabel() method
self.text = QtGui.QLabel("Label:")
self.input = QtGui.QLineEdit()
self.input.setFixedWidth(50)
# you could configure the range on this validator
self.inputValidator = QtGui.QDoubleValidator()
self.inputValidator.setDecimals(3)
self.input.setValidator(self.inputValidator)
self.slider = QtGui.QSlider()
self.slider.setOrientation(QtCore.Qt.Horizontal)
# range could be set from a self.setRange() method
self.slider.setRange(0, 100)
self.layout.addWidget(self.text)
self.layout.addWidget(self.input)
self.layout.addWidget(self.slider)
self.slider.sliderMoved.connect(self.sliderMoved)
self.input.editingFinished.connect(self.fieldEdited)
self.setValue(0)
def value(self):
return float(self.input.text())
def setValue(self, v):
self.input.setText("%0.3f" % float(v))
self.fieldEdited()
def sliderMoved(self, val):
self.input.setText('%0.3f' % (val / 100.0))
self._emitValueChanged()
def fieldEdited(self):
val = float(self.input.text())*100
self.slider.setValue(int(val))
self._emitValueChanged()
def _emitValueChanged(self):
self.valueChanged.emit(float(self.input.text()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment