Skip to content Skip to sidebar Skip to footer

Make Setdecimals Work With Overriden Qdoublespinbox

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 an

Solution 1:

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.

Solution 2:

Try it:

from PyQt5 import QtCore, QtGui, QtWidgets

classDotDoubleSpinBox(QtWidgets.QDoubleSpinBox):
    def__init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setDecimals(4)         
        self.setMinimumWidth(300)
        self.setMaximum(999999999)

    defvalidate(self, text, position):   
        p = position-1if position>0else0# p <- indexif 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)

    defvalueFromText(self, text):
        #text = text.replace(",", ".")returnfloat("%.4f" % (float(text)))  

    deftextFromValue(self, value):
        _value = "%.4f" % (float(value))
        return _value


classUi_MainWindow(object):

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

    defretranslateUi(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

Post a Comment for "Make Setdecimals Work With Overriden Qdoublespinbox"