Python Forum
How to accept only float number in QLineEdit input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to accept only float number in QLineEdit input
#1
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?
Reply
#2
Go to the Documentation > https://doc.qt.io/qt-5/qlineedit.html

And look up inputMask
Reply
#3
use QValidator

self.onlyInt = QIntValidator()
self.LineEdit.setValidator(self.onlyInt)
Reply
#4
@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.
Reply
#5
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.
Reply
#6
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to create a GUI in Python which would accept a bash command max22 1 661 Nov-13-2023, 04:40 AM
Last Post: deanhystad
  [PyQt] QLineEdit Caret (Text Cursor) Transparency malonn 5 2,731 Nov-04-2022, 09:04 PM
Last Post: malonn
  Simple printing the text for a QLineEdit thewolf 16 7,658 Mar-06-2021, 11:37 PM
Last Post: deanhystad
  PyQt5: How to retrieve a QLineEdit by its name ? arbiel 4 7,746 Oct-21-2020, 02:35 PM
Last Post: arbiel
  Two QlineEdit box, which interrelated GMCobraz 1 2,371 Aug-14-2020, 07:15 PM
Last Post: deanhystad
  [PyQt] Dynamically add and remove QLineEdit's GMCobraz 3 7,090 Jun-23-2020, 07:01 PM
Last Post: Yoriz
  prompt for input in qlineedit GMCobraz 3 3,157 Jun-22-2020, 01:51 PM
Last Post: GMCobraz
  [PyQt] display content from left to right in QComboBox or QLineEdit mart79 2 2,254 May-30-2020, 04:38 PM
Last Post: Axel_Erfurt
  How to loop through all QLineEdit widgets on a form JayCee 6 6,596 Apr-03-2020, 12:15 AM
Last Post: JayCee
  [PyQt] How to clear multiple Qlineedit in a loop mart79 6 7,641 Aug-15-2019, 02:37 PM
Last Post: Denni

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020