0

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_()
mrtak
  • 59
  • 1
  • 7
  • 1
    Not sure, but shouldn't you call repaint() in updatevalue() so that PyQT, repaints the contents of a widget? They also suggest using QTimer: http://stackoverflow.com/questions/1936868/pyqt-4-ui-freezes – Boris Burkov Sep 14 '12 at 10:16
  • possible duplicate of [Python PyQt: I could not display the data in lineedit from external program](http://stackoverflow.com/questions/12350925/python-pyqt-i-could-not-display-the-data-in-lineedit-from-external-program) – jdi Sep 16 '12 at 17:09

1 Answers1

0

You'll need to call QApplication.instance.processEvents() after updating your QLineEdit's text to force an update, otherwise you won't see anything until the final number.

You also need to change your textChanged() signal to textEdited(). Using textChanged() your updatedvalue() function will be called again on the first pass of your loop once setText() is called because you're updating the QLineEdit's text. The textEdited() signal won't be triggered if you update the text programmatically.

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("textEdited(QString)"),self.updatedvalue)

    def updatedvalue(self):
        for i in range(1,5):
            self.le.setText(str(i))
            QApplication.instance().processEvents()
            print(i)
            time.sleep(1)

app=QApplication(sys.argv)
f=MyFrame()
f.show()
app.exec_()

As Bob mentioned, using a QTimer would be much better.

Gary Hughes
  • 4,400
  • 1
  • 26
  • 40
  • thanks for the reply but still for displaying value, one time means first time I need to press any key while putting the cursor in the lineedit, then it displays all the values one by one. Please check this and suggest me how to overcome this issue. – mrtak Sep 15 '12 at 17:42
  • You mean you want the cycle to start when the program first runs without having to type anything into the `QLineEdit`? Add a call to `updatedvalue()` as the last line in your `QWidget`'s `__init__` method. – Gary Hughes Sep 17 '12 at 10:02
  • Thanks Bob and Gary, it works perfectly now. I have used QTimer and it is working perfectly for my said problem. @ Gary I shall also check the solution with updatedvalue() call in my __init__ method. – mrtak Sep 18 '12 at 04:54