Skip to content
Advertisement

PyQt5 QDoubleValidator don’t allow to write dot separators : x.y

Hello everyone, I’m trying to develop a GUI to modify and make computation on Pandas DataFrames with the PyQt5 module.

I could actually display my DataFrame, and Edit specific column or not. It’s displayed in a QTableWidget.

I tried to implement a QItemDelagate with the QDoubleValidator to write only specifics numbers in cols.

This is my function :

class FloatDelegate(QItemDelegate):
    def __init__(self, parent=None):
        super().__init__()

    def createEditor(self, parent, option, index):
        editor = QLineEdit(parent)
        editor.setValidator(QDoubleValidator(0.0000, 1.0000, 4))
        return editor

.....

 #data check float
 dataCheckDelege = FloatDelegate(self)
 self.setItemDelegateForColumn(3, dataCheckDelege)

I can only write numbers betwenn 0 & 1, that’s good for that, i could write flaot with the ” , ” separator like “0,5”.

But i couldn’t use ” . ” SEPARATOR, i couldn’t write “0.5”, and this is how iI need to write my dattas.

How can I deal with that?

Advertisement

Answer

This is most likely due to the locale of your validator. Validators use their locale to determine how numbers should be interpreted. If you don’t explicitly set the locale of the validator is uses whatever the locale of your system is. If that happens to be set to a locale that uses a comma as a decimal point, your validator will do so as well. To get around this you could set the locale of your validator to one that uses a dot as the decimal point, e.g. QLocale("en_US"):

def createEditor(self, parent, option, index):
    editor = QLineEdit(parent)
    validator = QDoubleValidator(0.0, 1.0, 4)
    validator.setLocale(QtCore.QLocale("en_US")
    editor.setValidator(validator)
    return editor
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement