[PyQt] convert an old pyqt script to a newer PyQt4 version
Massimo Di Stefano
massimodisasha at yahoo.it
Sun May 25 16:12:50 BST 2008
i'm relative new to qt-pyqt
i'm not a developper, but for my study (and passion) i need to learn
in these days i'm tring to know how to work with asyncronous data,
ma targhet is to make a gui to display data that come from a serial
to do these .. googling i find a script that is neart to what i need
but it is wroted in an old pyqt packages.
i need to run it, but i've difficoult to make it working with the
current pyqt version
(maybe not the last, i'm running qt 4.3 on a mac osx 10.5.2)
i tried to import PyQt4 as qt ... but unluky it don't works.
the script is :
import sys, time, threading, random, Queue, qt
def __init__(self, queue, endcommand, *args):
self.queue = queue
# We show the result of the thread in the gui, instead of the
self.editor = qt.QMultiLineEdit(self)
self.endcommand = endcommand
def closeEvent(self, ev):
We just call the endcommand when the window is closed
instead of presenting a button for that purpose.
Handle all the messages currently in the queue (if any).
msg = self.queue.get(0)
# Check contents of message and do what it says
# As a test, we simply print it
Launch the main part of the GUI and the worker thread. periodicCall and
endApplication could reside in the GUI part, but putting them here
means that you have all the thread controls in a single place.
# Create the queue
self.queue = Queue.Queue()
# Set up the GUI part
# A timer to periodically call periodicCall :-)
self.timer = qt.QTimer()
# Start the timer -- this replaces the initial call to periodicCall
# Set up the thread to do asynchronous I/O
# More can be made if necessary
self.running = 1
self.thread1 = threading.Thread(target=self.workerThread1)
Check every 100 ms if there is something new in the queue.
if not self.running:
self.running = 0
This is where we handle the asynchronous I/O. For example, it may be
One important thing to remember is that the thread has to yield
# To simulate asynchronous I/O, we create a random number at
# random intervals. Replace the following 2 lines with the real
time.sleep(rand.random() * 0.3)
msg = rand.random()
rand = random.Random()
root = qt.QApplication(sys.argv)
client = ThreadedClient()
thanks for any help!
Chiacchiera con i tuoi amici in tempo reale!
More information about the PyQt