[PyKDE] PyQt & threads

Kristian Nylund krnylund at msn.com
Mon Feb 25 15:23:12 GMT 2002


Hello

Can someone tell me what the problem is with the following program, as it
hangs. I'm pretty sure there's some inherent design-flaw involved, but
I'm not familiar enough with threads or GUI-programming to know what it
could be. If this is the case, maybe someone has a pointer about how this
functionality could be implemented.

I've also tried modyfing the program to use PYSIGNALs instead of passing the
caller to the thread, but the results were the same.

I'm using Python 2.2, Qt 3.0.1 and PyQt 3.0. I've also tried it with
Qt 2.3.1 and PyQt 2.4.

Thank You,

Kristian


#!/usr/bin/env python
from qt import *
from threading import *
from time import sleep

class Worker(Thread):

    def __init__(self, mw):
        Thread.__init__(self)
        # Take calling object as attribute
        self.mw = mw
        self.counter = 0

    def run(self):
        # Add 5 objects the the ListView
        while self.counter < 5:
            # Invoke method from calling object
            self.mw.addRow('val%d' % self.counter)
            self.counter = self.counter + 1
            sleep(1)
        # Remove the 5 objects from the ListView in order
        while self.counter > 0:
            # Invoke method from calling object
            self.mw.delRow('val%d' % self.counter)
            self.counter = self.counter - 1
            sleep(1)
        return

class MainWin(QMainWindow):

    def __init__(self,parent = None,name = None,fl = 0):
        QMainWindow.__init__(self,parent,name,fl)
        self.setName("Form1")
        self.resize(200,250)
        self.setCentralWidget(QWidget(self,"qt_central_widget"))

        self.ListView1 = QListView(self.centralWidget(),"ListView1")
        self.ListView1.addColumn("Column 1")
        self.ListView1.setGeometry(QRect(20,10,150,200))

        self.worker = Worker(self)
        self.worker.start()
        # If the following line is omitted, the program will
        # hang when trying to add the first object to the ListView
        QListViewItem(self.ListView1, ' ')

    def addRow(self, val):
        QListViewItem(self.ListView1, val)

    def delRow(self, val):
        # This is where it hangs
        print "Hang..."
        item = self.ListView1.findItem(val, 0)
        print "This won't be printed."
        self.ListView1.takeItem(item)
        del(item)

if __name__ == "__main__":
    a = QApplication(sys.argv)
    w = MainWin()
    a.setMainWidget(w)
    w.show()
    a.exec_loop()

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.






More information about the PyQt mailing list