My aim is to create a PyQT5 text box for use with currency that acts like an online banking website currency box that accepts numerical input only and will always convert to two decimal places with a minimum of 1 digit before the decimal place when focus is lost. I have the following syntax into QLineEdit which will do the desired number work...
"{0:.2f}".format(num)
or f-string...
f"{num:.2f}"
However, I am struggling as to how I can implement this into QLineEdit. I have made use of QDoubleValidator() which gives the restrictions needed but want the script to run and amend the text (numbers) inside the QLineEdit box when focus is taken away. (editingFinished?).
Example:
Input: 1 / Click out of the box: 1.00
Input: .5 / Click out of the box: 0.50
Input: 1.273654 / Click out of the box: 1.27
self.tablePrice = QLineEdit(self)
self.tablePrice.setPlaceholderText("0.00")
self.tablePrice.setValidator(QDoubleValidator(0.00, 999.99, 2, notation=QDoubleValidator.StandardNotation))
price = self.tablePrice.text()
self.tablePrice.editingFinished(self.tablePrice.setText("{0:.2f}.format(price)"))
self.tablePrice.setGeometry(378, 150, 82, 20)
I have included the above code as a rough indication as to what I feel may be close but please accept that this is just one of around 100+ attempts and that will without doubt not run. QDoubleSpinBox works superb but unlike the usual online banking website text box you have to manually delete the "0.00" when first clicking to edit the box.