Retrieving string value from label and then parsing into an integer, pyqt4

2.8k views Asked by At

I'm trying to get a string value from a label that I created and parsing, or casting it into an integer so I can add + 1 to that value of the label and then setting it back into the label. ex: having a label that says "4" press a button to increase it will then say "5". Thanks!

    self.lblCam1Focus = QtGui.QLabel("4",self)
    self.lblCam1Focus.move(50,60)



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

There are 1 answers

2
Nagaraj Tantri On

TypeError: QLabel.setText(QString): argument 1 has unexpected type 'int'

Clearly states that the argument passed is unexpected type. So next thing you should consider is, what type is important for QString

A Python string or unicode object, a QLatin1String or a QChar may be used whenever a QString is expected.

From http://pyqt.sourceforge.net/Docs/PyQt4/qstring.html

So,

def cam1IncreaseFocus(self):
    num_text = self.lblCam1Focus.text()
    num = int(num_text) + 1
    self.lblCam1Focus.setText(str(num))