Python Forum

Full Version: How to accept only float number in QLineEdit input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, I'm a beginner in python coding and in GUI developing.
I'm trying to develop a little GUI that takes only two float numbers in input and then add this two numbers with a function and give me the result in another QLineEdit (in Read Only mode).

The GUI work well if I insert int and float number but it breaks down if I insert characters or words or ' 1,2,35' values for example.

I'm searching the way to insert only positive numbers (float), for example '1,23' or '1.23' or '123'.

Can you help me, please?
Go to the Documentation > https://doc.qt.io/qt-5/qlineedit.html

And look up inputMask
use QValidator

self.onlyInt = QIntValidator()
self.LineEdit.setValidator(self.onlyInt)
@micro that validates if its an Integer the OP specifically stated Float (aka Real number not Integer) further Mask is fairly simple to use and might even be what that functions uses to some degree.
This work for me. Try if it works for your code:

validator = QRegExpValidator(QRegExp(r'[0-9].+'))
self.lineEdit.setValidator(validator)

OUTPUT:
Only integer 0-9 plus dot(.) for currency etc. Example 12345.123602, 123.79, 15.36 etc.
I doubt ism is still waiting for a solution. Your solution doesn't support scientific notation (1.234e-5). Why not use QDoubleValidator()? Not only would that accept scientific notation, but it will also uses localization (accept , as decimal place for example)