PyQt ProgressBar Example

This is a simple PyQt performing a simple progress bar, using thread.

import time
from PyQt4 import QtCore, QtGui

class SleepProgress(QtCore.QThread):
 procDone = QtCore.pyqtSignal(bool)
 partDone = QtCore.pyqtSignal(int)
 def run(self):
 print 'proc started'
 for a in range(1, 1+35):

 self.partDone.emit(float(a)/35.0*100)
 print 'sleep', a
 time.sleep(0.13)

 self.procDone.emit(True)   
 print 'proc ended'

class AddProgresWin(QtGui.QWidget):
 def __init__(self, parent=None):
 super(AddProgresWin, self).__init__(parent)

 self.thread = SleepProgress()

 self.nameLabel = QtGui.QLabel("0.0%")
 self.nameLine = QtGui.QLineEdit()

 self.progressbar = QtGui.QProgressBar()
 self.progressbar.setMinimum(1)
 self.progressbar.setMaximum(100)

 mainLayout = QtGui.QGridLayout()
 mainLayout.addWidget(self.progressbar, 0, 0)
 mainLayout.addWidget(self.nameLabel, 0, 1)

 self.setLayout(mainLayout)
 self.setWindowTitle("Processing")

 self.thread.partDone.connect(self.updatePBar)
 self.thread.procDone.connect(self.fin)

 self.thread.start()

 def updatePBar(self, val):
 self.progressbar.setValue(val)   
 perct = "{0}%".format(val)
 self.nameLabel.setText(perct)

 def fin(self):
 sys.exit()
 ##self.hide()

if __name__ == '__main__':

 import sys
 app = QtGui.QApplication(sys.path)

 pbarwin = AddProgresWin()
 pbarwin.show()

 sys.exit(app.exec_()
Advertisements

2 Comments

Filed under Python

2 responses to “PyQt ProgressBar Example

  1. beatpanic

    thanks a lot, the QtCore.pyqtSignal ‘trick’ made my day!

  2. Worked perfectly, great example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s