posted on: 2008-10-16 06:56:45
This is a short example to show how to use a PyQt4 thread for starting non-qt processes without your application waiting.

>This is about the most simple example I could think of how to make a thread

#!/usr/bin/env python
"""This is a short program to have an external program grab data for me and get displayed without blocking the whole program"""
from PyQt4 import QtGui,QtCore
import sys
class TerminalViewer(QtGui.QWidget):
	def __init__(self,parent=None):
		QtGui.QWidget.__init__(self,parent)
		self.Label = QtGui.QLabel("Waiting for Something",self)
		self.DataCollector = TerminalX(self)
		self.connect(self.DataCollector,QtCore.SIGNAL("Activated ( QString ) "), self.Activated)
		self.DataCollector.start()
	def Activated(self,newtext):
		self.Label.setText(newtext)
	def closeEvent(self,e):
		e.accept()
		app.exit()

class TerminalX(QtCore.QThread):
	def __init__(self,parent=None):
		QtCore.QThread.__init__(self,parent)
		self.test = ''
	def run(self):
		while self.test != 'q':
			self.test = raw_input('enter data: ')
			self.emit(QtCore.SIGNAL("Activated( QString )"),self.test)

app = QtGui.QApplication(sys.argv)
qb = TerminalViewer()
qb.show()
sys.exit(app.exec_())

This starts a QLabel with some text, if you type in the terminal where you started the program then you can change the text on the widget. The "start()" method executes the "run()" method in addition to starting the thread in a separate process.

Comments

Matt
2009-01-27 08:43:08
This example is limitted example, I have an improved example here http://orangepalantir.orgindex.php?idnum=38 although this example is a different style of thread.
matt
2010-01-11 21:04:28
Another update on this example. I have created a version that connects the signals across QThreads, so that a signal is emitted then evaluated in another threads event loop.
Nate C
2010-07-13 20:25:19
Thanks. I used this as a mockup to get going using QThread.
zzz
2012-04-13 02:52:41
what a shit fucking webpage...I cant even see the full width of the code
matt
2012-04-13 08:54:50
You can highlight it to copy it, or view source.
Mario
2012-06-18 18:51:26
Simple sample... JUST what I was looking for. Thanks!
Name: