I'm using
QDoubleValidatorwith aQLineEditin PyQt6, but the validator allows a comma as the decimal separator instead of a point (dot). How can I force the decimal separator to be a point?
When you use QDoubleValidator in PyQt6, the character it treats as the decimal separator — a comma (,) or a point (.) — is determined by the locale the validator uses. A locale defines how numbers, dates, and currencies are formatted for a particular language and region. In many European countries, for example, the comma is the standard decimal separator (3,14), while in the United States and United Kingdom, the point is used (3.14).
By default, QDoubleValidator picks up your system's locale settings. If your operating system is configured for a locale that uses commas, the validator will accept commas as the decimal separator. To override this and force a point separator, you can create a QLocale and apply it directly to the validator.
Setting a Custom Locale on the Validator
The QLocale class lets you specify a language and country combination that determines number formatting rules. To get a point as the decimal separator, use a locale like English, UnitedStates:
from PyQt6.QtCore import QLocale
locale = QLocale(QLocale.English, QLocale.UnitedStates)
validator.setLocale(locale)
Once this locale is set on the validator, the point will be accepted as the decimal separator in your QLineEdit.
One thing to be aware of: even with this locale, the comma may still be accepted as input because the comma serves as the thousands grouping separator in English locales (e.g., 1,000.50). This is expected behavior and generally won't cause problems.
Setting Standard Notation
You can also control whether the validator accepts scientific notation (like 1.5e10) by setting the notation mode. If you only want standard decimal numbers, use:
validator.setNotation(QDoubleValidator.StandardNotation)
This prevents users from entering numbers in scientific notation format.
Constructor Arguments
When creating a QDoubleValidator, the three main arguments are:
- bottom — the minimum acceptable value
- top — the maximum acceptable value
- decimals — the maximum number of digits after the decimal point
For example:
validator = QDoubleValidator(0.1, 9990, 2)
This creates a validator that accepts numbers between 0.1 and 9990, with up to 2 decimal places.
Be careful with Python syntax here — something like 99.9.0 is not a valid Python number and will raise a SyntaxError.
Complete Working Example
Here's a full example that creates a QLineEdit with a QDoubleValidator configured to use a point as the decimal separator:
from PyQt6.QtWidgets import QApplication, QLineEdit
from PyQt6.QtGui import QDoubleValidator
from PyQt6.QtCore import QLocale
app = QApplication([])
lineedit = QLineEdit()
lineedit.setPlaceholderText("Enter a number (e.g. 3.14)")
# Create a validator: min 0.1, max 9990, up to 2 decimal places.
validator = QDoubleValidator(0.1, 9990, 2)
# Create a locale that uses a point for the decimal separator.
locale = QLocale(QLocale.English, QLocale.UnitedStates)
validator.setLocale(locale)
# Only allow standard notation (no scientific notation).
validator.setNotation(QDoubleValidator.StandardNotation)
# Apply the validator to the line edit.
lineedit.setValidator(validator)
lineedit.show()
app.exec()
Run this, and you'll have a QLineEdit that only accepts numbers formatted with a point as the decimal separator. Try typing 3.14 — it will be accepted. The comma is still permitted as a grouping separator (e.g., 1,000), but it won't function as a decimal point.
If you're looking for more ways to validate user input, you might also be interested in input validation in Tkinter for a comparison of how other Python GUI frameworks handle similar problems. For a broader overview of the widgets available in PyQt6, including QLineEdit, see the basic widgets tutorial.
Summary
The decimal separator used by QDoubleValidator comes from the locale. To change it:
- Create a
QLocalewith the language and country that matches your desired formatting. - Call
setLocale()on the validator to apply it. - Optionally, call
setNotation(QDoubleValidator.StandardNotation)to restrict input to standard decimal notation.
This gives you full control over how numbers are entered in your QLineEdit fields, regardless of the user's system settings. To learn more about checking whether a QLineEdit is empty or contains valid data, see how to check if a QLineEdit is empty.
PyQt6 Crash Course by Martin Fitzpatrick — The important parts of PyQt6 in bite-size chunks