[PyQt] Multiprocessing and ProgressBar

Nicola Creati ncreati at inogs.it
Thu Oct 7 14:06:19 BST 2010


Hans Meine wrote:
> Am Donnerstag 07 Oktober 2010, 14:13:29 schrieb Nicola Creati:
>   
>> I'm trying to develop a simple application that should do some
>> calculation in a multiprocessing process and update a progress bar. [...]
>> Thi sis the code i'm using:
>>     
>
> There's no __main__ code, is there?  I'd be easier for others to help you if 
> there was specific code to run.
>
> Ciao,
>   Hans
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
>   
Ops, I forgot to past the main.... Sorry.

Here the complete code:

#-------------------------------------------------------------------------------
import sys, time
from multiprocessing import Process
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui


class Compute(object):
    def __init__(self, win):
        self.win = win
   
    def calculate(self):
        for i in range (100):
            self.progressBar(i)
            time.sleep(0.05)
   
    def progressBar(self, value):
        self.win.updatePb.emit(value)
       
#-------------------------------------------------------------------------------
class Form(QtGui.QDialog):
   
  updatePb = QtCore.pyqtSignal(int)
 
  def __init__(self, parent=None):
    super(Form, self).__init__(parent)
   
    self.progressBar = QtGui.QProgressBar()
    self.progressBar.setMinimum(0)
    self.progressBar.setMaximum(100)
    self.progressBar.setValue(0)
    bbox = QtGui.QDialogButtonBox()
    bbox.setOrientation(QtCore.Qt.Horizontal)
    bbox.setStandardButtons(QtGui.QDialogButtonBox.Ok)
    layout = QtGui.QVBoxLayout(self)
    layout.addWidget(self.progressBar)
    layout.addWidget(bbox)
    bbox.clicked.connect(self.calculate)
   
    self.updatePb.connect(self.progressBar.setValue)
        
  def calculate(self):
    co = Compute(self)
    p = Process(target=co.calculate)
    p.start()
    p.join()


#-------------------------------------------------------------------------------
if __name__ == "__main__":     
    app = QtGui.QApplication(sys.argv)
    form = Form()
    form.show()
    app.exec_()

Thanks,

Nicola



More information about the PyQt mailing list