Load in the background showing the progress via a Gauge in the statusbar.
To to this, you can initiate the loading using QThread. Your thread class can look as follows (assuming parent
will have an attribute progress
):
QtFileLoader(QtCore.QThread):
def __init__(self,parent=None, filepath=None):
QtCore.QThread.__init__(self,parent)
self.data = None
self.filepath = filepath
def run(self):
""" load data in parts and update the progess par """
chunksize = 1000
filesize = ... # TODO: get length of file
n_parts = int(filesize/chunksize) + 1
with open(self.filepath, 'rb') as f:
for i in range(n_parts):
self.data += f.read(chunksize)
self.parent.progress = i
The question whether to use QThread
or trheading.Thread
is discussed here
edit (according to hint of @Nathan):
On the parent
, a timerfunction should check, say each 100ms, the value of self.parent.progress and set the progressbar accordingly. This way, the progressbar is set from within the main thread of the GUI.