1

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.

Andy_K
  • 19
  • 3
  • @zixuan I have exhausted all search options and terms hence my "100+ attempts". PyQT5 has severe lack of support with almost all documentation made for C++. Should I pass my script into .setText as a function? Should I perhaps be using the .connect() signal too when doing so? – Andy_K Aug 16 '19 at 12:03
  • Well, `.connect()` is usable but that will collapse with `editingFinished`. In JS (JavaScript) with jQuery, on webpages when focused when handled it would use the `.focus()` function. There might be a similarity here. – new Q Open Wid Aug 16 '19 at 12:08
  • 1
    @Andy.K Don't try to re-invent the wheel: use a [QDoubleSpinBox](https://doc.qt.io/qt-5/qdoublespinbox.html). It does everything you want automatically, and has a much nicer user-interface. – ekhumoro Aug 16 '19 at 12:41
  • @ekhumoro QDoubleSpinBox worked well actually. However, would there be a way to reset the counter back to zero when you erase the numbers and remove focus from the box? For example, you have "4.00", you then delete that number and click outside of the box, but "4.00" magically reappears. – Andy_K Aug 16 '19 at 12:51
  • 1
    @Andy.K As a user, when I want to enter zero, I don't press the delete key - I press the 0 key. Explicit is better than implicit, and all that ... – ekhumoro Aug 16 '19 at 13:10
  • @Andy.K But if you really must, you can try this: `spinbox.setCorrectionMode(QAbstractSpinBox.CorrectToNearestValue)`. – ekhumoro Aug 16 '19 at 13:15
  • @ekhumoro Would you know of a way to make the QDoubleSpinBox blank when first clicked in as opposed to first having to delete the "0.00" before being able to enter a number? I simply want a box that acts in the same manner as a currency input box on a banking website. – Andy_K Aug 16 '19 at 21:40
  • @Andy_K Create a subclass of QDoubleSpinBox, with `def focusInEvent(self, event): if not self.value(): self.clear(); super().focusInEvent(event)`. – ekhumoro Aug 16 '19 at 23:45

0 Answers0