[PyQt] problem with qthread and signal/slot
Phil Thompson
phil at riverbankcomputing.co.uk
Sat Apr 14 11:14:04 BST 2007
On Saturday 14 April 2007 5:44 am, Pradnyesh Sawant wrote:
> Hello,
> i've written a small 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!
It works fine for me.
Phil
More information about the PyQt
mailing list