I am trying to show the loop data in Lineedit but it is not updating. Even the print command does not print the data on the terminal till I press any key other than return in lineedit. Have a look at the program and suggest me the changes:
import sys
import time
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MyFrame(QWidget):
def __init__(self):
QWidget.__init__(self)
self.le = QLineEdit(self)
self.le.setGeometry(200,200,75,35)
i=0
self.le.setText(str(i))
self.connect(self.le, SIGNAL("textChanged(QString)"),self.updatedvalue)
def updatedvalue(self):
for i in range(1,5):
self.le.setText(str(i))
print(i)
time.sleep(1)
app=QApplication(sys.argv)
f=MyFrame()
f.show()
app.exec_()
You’ll need to call
QApplication.instance.processEvents()after updating yourQLineEdit‘s text to force an update, otherwise you won’t see anything until the final number.You also need to change your
textChanged()signal totextEdited(). UsingtextChanged()yourupdatedvalue()function will be called again on the first pass of your loop oncesetText()is called because you’re updating theQLineEdit‘s text. ThetextEdited()signal won’t be triggered if you update the text programmatically.As Bob mentioned, using a QTimer would be much better.