本文介绍了使setDecimals与重写的QDoubleSpinBox一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我要'.',所以更改了QDoubleSpinbox.而不是,",但现在setDecimals无法正常工作...我如何保留qdoublespinbox分别对应于setdecimals的功能并保留我的重写类(或等效的/更好的东西)?

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)?

我尝试做:

return QtWidgets.QWidget.locale().toString(_value, QLatin1Char('f'), QtWidgets.QDoubleSpinBox.decimals())

在textFromValue下,但出现错误:

under textFromValue but i got the error:

TypeError: locale(self): first argument of unbound method must have type 'QWidget'

我不明白.我也不相信pyqt5支持QLatin1Char.

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

推荐答案

我发现整个问题比我最初设想的要容易得多.原来,您只需要将语言环境设置为使用'.'的语言环境即可.而不是','.

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.

这篇关于使setDecimals与重写的QDoubleSpinBox一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:03