[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


Hi

i'm relative new to qt-pyqt

i'm not a developper, but for my study (and passion) i need to learn  
about.

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  
port.

to do these .. googling i find a script that is neart to what i need  
to learn,
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

class GuiPart(qt.QMainWindow):

def __init__(self, queue, endcommand, *args):
    qt.QMainWindow.__init__(self, *args)
    self.queue = queue
    # We show the result of the thread in the gui, instead of the  
console
    self.editor = qt.QMultiLineEdit(self)
    self.setCentralWidget(self.editor)
    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.
    """
    self.endcommand()

def processIncoming(self):
    """
    Handle all the messages currently in the queue (if any).
    """
    while self.queue.qsize():
        try:
            msg = self.queue.get(0)
            # Check contents of message and do what it says
            # As a test, we simply print it
            self.editor.insertLine(str(msg))
        except Queue.Empty:
            pass


class ThreadedClient:
"""
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.
"""
def __init__(self):
    # Create the queue
    self.queue = Queue.Queue()

    # Set up the GUI part
    self.gui=GuiPart(self.queue, self.endApplication)
    self.gui.show()

    # A timer to periodically call periodicCall :-)
    self.timer = qt.QTimer()
    qt.QObject.connect(self.timer,
                       qt.SIGNAL("timeout()"),
                       self.periodicCall)

    # Start the timer -- this replaces the initial call to periodicCall
    self.timer.start(100)

    # 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)
    self.thread1.start()


def periodicCall(self):
    """
    Check every 100 ms if there is something new in the queue.
    """
    self.gui.processIncoming()
    if not self.running:
        root.quit()

def endApplication(self):
    self.running = 0


def workerThread1(self):
    """
    This is where we handle the asynchronous I/O. For example, it may be
    a 'select()'.
    One important thing to remember is that the thread has to yield
    control.
    """
    while self.running:
        # To simulate asynchronous I/O, we create a random number at
        # random intervals. Replace the following 2 lines with the real
        # thing.
        time.sleep(rand.random() * 0.3)
        msg = rand.random()
        self.queue.put(msg)


rand = random.Random()
root = qt.QApplication(sys.argv)

client = ThreadedClient()
root.exec_loop()

thanks for any help!

Massimo.
Chiacchiera con i tuoi amici in tempo reale! 
 http://it.yahoo.com/mail_it/foot/*http://it.messenger.yahoo.com 



More information about the PyQt mailing list