problem with qthread and signal/slot

P

Pradnyesh Sawant

Hello,
i've written a small pyqt code which increments a counter in the backend,
and prints it on the frontend. However, i'm passing a dummy string var
along with it in the signal/slot mechanism. The problem is that from
the 2nd call onwards, the string gets printed, but NOT in the 1st
call. (eg, in the foll code i get:- 1, (2, 'a'), (3, 'a'), ...)
The code is:-
**************************************************
import time
import sys
from PyQt4 import QtGui, QtCore

class Counter(QtCore.QThread):
def __init__(self):
QtCore.QThread.__init__(self)
self.cntr = 0
def run(self):
while True:
self.cntr += 1
self.emit(QtCore.SIGNAL("showCntr"), (self.cntr, "a"))
time.sleep(1)
if self.cntr == 10: break
class Gui(QtGui.QDialog):
def __init__(self, parent = None):
QtGui.QDialog.__init__(self, parent)
frameStyle = QtGui.QFrame.Sunken | QtGui.QFrame.Panel

self.lCntr = QtGui.QLabel()
self.lCntr.setFrameStyle(frameStyle)
loGrd = QtGui.QGridLayout()
loGrd.addWidget(self.lCntr, 0, 0)
self.setLayout(loGrd)
self.setWindowTitle(self.tr("Counter"))
def showCntr(self, val):
print val
self.lCntr.setText(str(val))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
dialog = Gui()
cntr = Counter()
cntr.start()
QtCore.QObject.connect(cntr, QtCore.SIGNAL("showCntr"),
dialog.showCntr, QtCore.Qt.QueuedConnection)
sys.exit(dialog.exec_())
**************************************************
Also, this is my 1st ever prog with signals/slots across threads; and
am a newbie to python and pyqt. So please be considerate and kind
enough to point out mistakes/better approaches with the above code.

Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top