Skip to content

Instantly share code, notes, and snippets.

@kaotika
Last active November 12, 2022 10:10
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kaotika/e8ca5c340ec94f599fb2 to your computer and use it in GitHub Desktop.
Save kaotika/e8ca5c340ec94f599fb2 to your computer and use it in GitHub Desktop.
Simple PyQt example with QThread and QProgressBar
from PyQt4 import QtCore, QtGui
import sys, time
class mythread(QtCore.QThread):
total = QtCore.pyqtSignal(object)
update = QtCore.pyqtSignal()
def __init__(self, parent, n):
super(mythread, self).__init__(parent)
self.n = n
def run(self):
self.total.emit(self.n)
i = 0
while (i<self.n):
if (time.time() % 1==0):
i+=1
#print str(i)
self.update.emit()
# create the dialog for zoom to point
class progress(QtGui.QProgressBar):
def __init__(self, parent=None):
super(progress, self).__init__(parent)
# Set up the user interface from Designer.
self.setValue(0)
self.thread = mythread(self, 3)
self.thread.total.connect(self.setMaximum)
self.thread.update.connect(self.update)
self.thread.finished.connect(self.close)
self.n = 0
self.thread.start()
def update(self):
self.n += 1
print self.n
self.setValue(self.n)
if __name__=="__main__":
app = QtGui.QApplication([])
progressWidget = progress()
progressWidget.move(300, 300)
progressWidget.show()
sys.exit(app.exec_())
@mherrmann
Copy link

Nice. For a more recent example based on PyQt5 see here! It's a multithreaded chat client:

image

@ferdnyc
Copy link

ferdnyc commented Jul 21, 2019

mhermann's link isn't really all that directly applicable, since it doesn't just use PyQt5, it also uses his threading module. And the Python threading module. AND QThread. (That's in there, if you look deep enough.)

Cool and all, but if anyone's looking for simple examples of how to use QThread in PyQt5, this stackoverflow question has several good ones among its answers.

@mherrmann
Copy link

The way you've phrased this @ferdnyc implies something that isn't factually true. You make it sound like my examples require knowledge of some proprietary "threading module" of mine. That's not true. My link gives an examples that uses Python's threading module, without any dependence on special code of mine. Then, yes, it does give a further example that shows a more abstract (and imho useful) utility function. But it is a separate example that's again (imho useful), but not required for understanding the examples before that.

@ferdnyc
Copy link

ferdnyc commented Jul 23, 2019

@mhermann Perhaps. But the title of the original gist is "Simple PyQt example with QThread and QProgressBar".

@ati-ince
Copy link

Thank you for sharing... I was searching for a couple of days about how can I update the QThread value from the main window like the QWindow application. Your sample code helps me alot.

Unluckily, there is no good documentation for QThread applications , shared memory etc side

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment