Changing property of a QLE in python

44 views Asked by At

I'm currently having an issue with a QLE that I created. I would like the qle to take a value, turn it into a float and then increase or decrease a label value depending on if the value is positive or negative. The only problem is is that every time I type something into the qle it will start adding that value to the label before I'm done typing. For example: I type in "4" into the qle that works but once I type in "4." anything it will read it as 4 two times so the label will be changed to 8. Maybe there's a way so that when I press a button it will increase or decrease but only after I press the button? I added code for a button I created and maybe it would be easier to link that with the qle. Many thanks!

    #this creates the increase button for cam1 focus
    self.btnCam1IncreaseFocus = QtGui.QPushButton("+",self)
    self.btnCam1IncreaseFocus.clicked.connect(self.cam1IncreaseFocus)
    self.btnCam1IncreaseFocus.resize(25,25)
    self.btnCam1IncreaseFocus.move(75,100)

    #This creates a textbox or QLE for a custom tweak value for cam1 focus
    self.qleTextBoxCam1Focus = QtGui.QLineEdit(self)
    self.qleTextBoxCam1Focus.resize(25,25)
    self.qleTextBoxCam1Focus.move(40,100)
    self.qleTextBoxCam1Focus.textChanged[str].connect(self.qleCam1Focus)

    def cam1IncreaseFocus(self):
    text = self.lblCam1Focus.text()
    n = float(text)
    n = n + 1
    self.lblCam1Focus.setText(str(n))

    def qleCam1Focus(self):
    text = self.qleTextBoxCam1Focus.text()
    if text == "":
        text = "0.0"
    if str(text).isalpha() == False:
        n = float(text)
    textLabel = self.lblCam1Focus.text()
    if textLabel == "":
        textLabel = "0.0"
    y = float(textLabel)
    result = n + y
    if result <= 0.0:
        result = 0.0
    self.lblCam1Focus.setText(str(result))
1

There are 1 answers

0
ekhumoro On BEST ANSWER

Instead of textChanged, use the editingFinished signal, which will only fire when return/enter is pressed, or when the line-edit loses focus:

    self.qleTextBoxCam1Focus.editingFinished.connect(self.qleCam1Focus)