SHOW:
|
|
- or go back to the newest paste.
| 1 | - | import sys, time |
| 1 | + | #!/usr/bin/env python2 |
| 2 | ||
| 3 | - | |
| 3 | + | import sys,time |
| 4 | from PyQt4 import QtCore, QtGui | |
| 5 | - | def __init__(self, parent=None): |
| 5 | + | |
| 6 | - | QtGui.QWidget.__init__(self, parent) |
| 6 | + | |
| 7 | - | |
| 7 | + | def __init__(self, parent=None): |
| 8 | - | self.setGeometry(300, 300, 280, 600) |
| 8 | + | QtGui.QWidget.__init__(self, parent) |
| 9 | - | self.setWindowTitle('threads')
|
| 9 | + | |
| 10 | - | |
| 10 | + | self.setGeometry(300, 300, 280, 600) |
| 11 | - | self.layout = QtGui.QVBoxLayout(self) |
| 11 | + | self.setWindowTitle('threads')
|
| 12 | - | |
| 12 | + | |
| 13 | - | self.testButton = QtGui.QPushButton("test")
|
| 13 | + | self.layout = QtGui.QVBoxLayout(self) |
| 14 | - | self.connect(self.testButton, QtCore.SIGNAL("released()"), self.test)
|
| 14 | + | |
| 15 | - | self.listwidget = QtGui.QListWidget(self) |
| 15 | + | self.testButton = QtGui.QPushButton("test")
|
| 16 | - | |
| 16 | + | self.connect(self.testButton, QtCore.SIGNAL("released()"), self.test)
|
| 17 | - | self.layout.addWidget(self.testButton) |
| 17 | + | self.listwidget = QtGui.QListWidget(self) |
| 18 | - | self.layout.addWidget(self.listwidget) |
| 18 | + | |
| 19 | - | |
| 19 | + | self.layout.addWidget(self.testButton) |
| 20 | - | def add(self, text): |
| 20 | + | self.layout.addWidget(self.listwidget) |
| 21 | - | """ Add item to list widget """ |
| 21 | + | |
| 22 | - | print "Add: " + text |
| 22 | + | def add(self, text): |
| 23 | - | self.listwidget.addItem(text) |
| 23 | + | """ Add item to list widget """ |
| 24 | - | self.listwidget.sortItems() |
| 24 | + | print "Add: " + text |
| 25 | - | |
| 25 | + | self.listwidget.addItem(text) |
| 26 | - | def addBatch(self, text="test", iters=6, delay=0.3): |
| 26 | + | self.listwidget.sortItems() |
| 27 | - | """ Add several items to list widget """ |
| 27 | + | |
| 28 | - | for i in range(iters): |
| 28 | + | def addBatch(self, text="test", iters=6, delay=0.3): |
| 29 | - | time.sleep(delay) # artificial time delay |
| 29 | + | """ Add several items to list widget """ |
| 30 | - | self.add(text + " " + str(i)) |
| 30 | + | for i in range(iters): |
| 31 | - | |
| 31 | + | time.sleep(delay) # artificial time delay |
| 32 | - | def threadFunc(self) : |
| 32 | + | self.add(text + " " + str(i)) |
| 33 | - | print 'in threadFunc' |
| 33 | + | |
| 34 | - | |
| 34 | + | def thread2Func(self) : |
| 35 | - | def test(self): |
| 35 | + | print 'in thread2Func' |
| 36 | - | self.listwidget.clear() |
| 36 | + | |
| 37 | - | ## adding entries just from main application: locks ui |
| 37 | + | def test(self): |
| 38 | - | #self.addBatch("_non_thread", iters=6, delay=0.3)
|
| 38 | + | self.listwidget.clear() |
| 39 | - | |
| 39 | + | |
| 40 | - | ## adding by emitting signal in different thread |
| 40 | + | self.genericThread = GenericThread(self.addBatch,"from generic thread ",delay=0.3) |
| 41 | - | #self.workThread = WorkThread() |
| 41 | + | self.genericThread.start() |
| 42 | - | #self.connect( self.workThread, QtCore.SIGNAL("update(QString)"), self.add )
|
| 42 | + | |
| 43 | - | #self.workThread.start() |
| 43 | + | #self.thread2=GenericThread(self.thread2Func) |
| 44 | - | |
| 44 | + | |
| 45 | - | self.genericThread = GenericThread(self.addBatch,"from generic thread ",delay=0.3) |
| 45 | + | |
| 46 | - | #self.genericThread = GenericThread() |
| 46 | + | def __init__(self, function, *args, **kwargs): |
| 47 | - | self.genericThread.start() |
| 47 | + | QtCore.QThread.__init__(self) |
| 48 | - | |
| 48 | + | self.function = function |
| 49 | - | self.thread2=GenericThread(self.threadFunc) |
| 49 | + | self.args = args |
| 50 | - | |
| 50 | + | self.kwargs = kwargs |
| 51 | - | #class WorkThread(QtCore.QThread): |
| 51 | + | |
| 52 | - | #def __init__(self): |
| 52 | + | def __del__(self): |
| 53 | - | #QtCore.QThread.__init__(self) |
| 53 | + | self.wait() |
| 54 | - | |
| 54 | + | |
| 55 | - | #def run(self,x): |
| 55 | + | def run(self,x): |
| 56 | - | #print x |
| 56 | + | self.function(*self.args,**self.kwargs) |
| 57 | - | #for i in range(6): |
| 57 | + | return |
| 58 | - | #time.sleep(0.3) # artificial time delay |
| 58 | + | |
| 59 | - | #self.emit( QtCore.SIGNAL('update(QString)'), "from work thread " + str(i) )
|
| 59 | + | |
| 60 | - | #return |
| 60 | + | |
| 61 | test = MyApp() | |
| 62 | test.show() | |
| 63 | app.exec_() |