Alexis_Labuschagne | 2021-01-03 14:40:21 UTC | #1
RE Using "QDoubleValidator(double bottom , double top , int decimals , QObject * parent = nullptr)"
Source code: self.InputLineEdit.setValidator(QtGui.QDoubleValidator(0.1,99.9.0,2))
I get a comma allowed for the decimal separator where I need a point in the QLineEdit widget. Any advice how to force a point decimal separator?
martin | 2021-01-10 18:56:22 UTC | #2
Hi @Alexis_Labuschagne The decimal separator is handled through the locale which defines how numbers are displayed/handled for localization purposes.
The comma separator is probably following your system defaults, but you can override it as follows. This creates a custom locale (using United States, which uses the decimal point for decimals) and applies it to the field. Note that the comma is still allowed, since this is a decimal group operator.
Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 15,000 copies sold!
from PyQt5.QtWidgets import QApplication, QLineEdit
from PyQt5.QtGui import QDoubleValidator
from PyQt5.QtCore import QLocale
app = QApplication([])
lineedit = QLineEdit()
lineedit.show()
validator = QDoubleValidator(0.1,9990,2)
locale = QLocale(QLocale.English, QLocale.UnitedStates)
validator.setLocale(locale)
validator.setNotation(QDoubleValidator.StandardNotation)
lineedit.setValidator(validator)
lineedit.textChanged.connect(print)
lineedit.show()
app.exec_()
The line below is invalid Python, since Python syntax only allows numbers to have a single decimal point in them (and no commas).
PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Save yourself time and frustration. Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.
self.InputLineEdit.setValidator(QtGui.QDoubleValidator(0.1,99.9.0,2))
If you want to use alternative grouping in the number (e.g. like an IP address) with multiple decimal points, you can implement your own validator by subclassing QValidator
. Let me know if that's what you're looking for.