ich habe mir eine Klasse geschrieben, die einen Hochladevorgang simuliert. Damit die Gui nicht geblockt wird, habe ich einen QThread subclassed (wie sagt man das auf Deutsch?

Code: Alles auswählen
class MockUpload(QtCore.QThread):
"""
A QThread which will sequentially “upload” the files in the list.
Accepts a list of dictionaries with the file data (Generated by the
FileTableModel.file_data).
Signals:
file_upload_status(int, int)
Emitted every time a new “step” is completed. Emits the file index and
the transferred bytes for the current file.
file_upload_completed(int)
Emitted when a single file upload is completed. Emits the file index.
finished()
Emitted when the entire thread is completed.
"""
file_upload_status = QtCore.pyqtSignal(int, int) # File number, transferred bytes
file_upload_completed = QtCore.pyqtSignal(int) # File number
def __init__(self, file_data):
"""
Receives a list of dictionaries with the files to upload.
"""
QtCore.QThread.__init__(self)
self.files_data = file_data
#For mock upload:
self.steps = 10
def run(self):
for file_number, file_data in enumerate(self.files_data):
self.mock_upload(file_number)
def mock_upload(self, file_number):
transferred = 0
print("Starting mock upload of {}, taking {} steps.".format(
self.files_data[file_number]["file_path"],
self.steps))
for i in range(self.steps):
transferred += int(self.files_data[file_number]["file_size"] / self.steps)
print("Transferred {} Bytes".format(transferred))
self.file_upload_status.emit(file_number, transferred)
time.sleep(1)
self.file_upload_completed.emit(file_number)
print("Completed upload of {}.".format(self.files_data[file_number]["file_path"]))
Code: Alles auswählen
def onUpload(self):
"""
Slot for the upload button.
"""
#TODO: Button should be disabled if model has no files.
if self.files_model.has_files:
self.overallProgress.setMaximum(self.files_model.overall_size)
self.upload = MockUpload(self.files_model.file_data)
self.upload.file_upload_status.connect(self._update_progress)
self.upload.file_upload_completed.connect(self._set_completed)
self.upload.run()