1

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_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
n00p
  • 269
  • 4
  • 11

2 Answers2

2

I figured out the whole issue was a lot easier than i first assumed. Turns out you just have to set the locale to a locale that uses '.' instead of ','.

self.doubleSpinBox.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates))

That will do it. Irregardless, thank you to the answerer who almost had a perfect answer.

n00p
  • 269
  • 4
  • 11
0

Try it:

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

    def validate(self, text, position):   
        p = position-1 if position>0 else 0     # p <- index

        if text == "" or ((text[p] > "9" or text[p] < "0") and text[p] != "."):
            state = QtGui.QValidator.Invalid
        elif "." == text[p]: 
            if ("." in text[0:p]) or ("." in text[p:]):
                state = QtGui.QValidator.Invalid
            else:
                state = QtGui.QValidator.Acceptable
                self.setValue(float(text))
        elif "," == text[p]: 
            state = QtGui.QValidator.Invalid
        else:
            state = QtGui.QValidator.Intermediate
            self.setValue(float(text))

        return (state, text, position)

    def valueFromText(self, text):
        #text = text.replace(",", ".")
        return float("%.4f" % (float(text)))  

    def textFromValue(self, value):
        _value = "%.4f" % (float(value))
        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_())

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33
  • Thank you, that works almost perfectly. The only problem is when one tries to delete from the back or move the cursor forwards from the back it acts "weird"(you will see) I was thinking maybe i can just change locale of the boxes and then that locale will use . not , ... ? Might be a lot easier change. – n00p Aug 14 '18 at 14:03
  • Thank you. But it seemed the locale way was the easiest :) – n00p Aug 14 '18 at 14:14