I am trying to find a way to put negative numbers in a QDoubleSpinBox
. I read the documentation but haven't found the solution yet. Here is the class I made to customize my QDoubleSpinBox
:
class ComboBoxDelegate(QStyledItemDelegate):
"""A delegate that allows the user to change integer values from the model
using a spin box widget. """
def __init__(self, parent=None):
super().__init__(parent)
def createEditor(self, parent, option, index : QModelIndex):
if index.column() == 0 :
editor = QLineEdit(parent)
return editor
if index.column() == 1 :
combo = QComboBox(parent)
combo.addItems(["len","diam","conc"])
editor = combo
return editor
if index.column() > 1 :
editor = CustomDoubleSpinbox(parent)
editor.setDecimals(3)
return editor
def setEditorData(self, editor , index):
value = index.model().data(index, Qt.EditRole)
if type(editor) == QComboBox :
editor.setCurrentText(str(value))
if type(editor) == CustomDoubleSpinbox :
editor.setValue(value)
if type(editor) == QLineEdit :
editor.setText(value)
def setModelData(self, editor , model, index : QModelIndex):
if type(editor) == QComboBox :
value = editor.currentText()
if type(editor) == CustomDoubleSpinbox :
editor.interpretText()
value = editor.value()
if type(editor) == QLineEdit :
value = editor.text()
model.setData(index, value, Qt.EditRole)
def updateEditorGeometry(self, editor, option, index):
editor.setGeometry(option.rect)
class CustomDoubleSpinbox(QDoubleSpinBox):
def validate(self, text: str, pos: int) -> object:
text = text.replace(".", ",")
return QDoubleSpinBox.validate(self, text, pos)
def valueFromText(self, text: str) -> float:
text = text.replace(",", ".")
return float(text)
EDIT
h4z3 found the solution in the documentation : QDoubleSpinBox for python. By setting a negative minimum with the setMinimum
method, it works.