iv'e changed QDoubleSpinbox because i wanted '.' instead of ',' but now setDecimals doesn't work... How do i retain the functionality of qdoublespinbox respective to setdecimals and retain my overriden class(or something equivalent/better)?
I tried doing:
return QtWidgets.QWidget.locale().toString(_value, QLatin1Char('f'), QtWidgets.QDoubleSpinBox.decimals())
under textFromValue but i got the error:
TypeError: locale(self): first argument of unbound method must have type 'QWidget'
which i don't understand. Also i do not believe pyqt5 supports QLatin1Char.
from PyQt5 import QtCore, QtGui, QtWidgets
class DotDoubleSpinBox(QtWidgets.QDoubleSpinBox):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setDecimals(4)
self.setMinimumWidth(300)
self.setMaximum(9999999999)
def validate(self, text, position):
if "." in text:
state = QtGui.QValidator.Acceptable
elif "," in text:
state = QtGui.QValidator.Invalid
else:
state = QtGui.QValidator.Intermediate
return (state, text, position)
def valueFromText(self, text):
text = text.replace(",", ".")
return float(text)
def textFromValue(self, value):
_value = str(value)
_value = _value.replace(",", ".")
return _value
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.doubleSpinBox = DotDoubleSpinBox(self.centralwidget)
self.doubleSpinBox.setGeometry(QtCore.QRect(260, 110, 80, 32))
self.doubleSpinBox.setObjectName("doubleSpinBox")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 30))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())