I am taking photos using gphoto2 and would like to them to a list widget asynchronously as the photos are taken but for some reason it isn't working as intended. It takes the photo on a QThread but is not adding the photo to the list until all photos have been taken (like a bulk add). How would I go about this?
Here is the relevant source code (it won't compile because as there is too many dependencies to fit within the question):
class DownloadThread(QThread):
data_downloaded = Signal(object)
def __init__(self, photo_name):
QThread.__init__(self)
self.photo_name = photo_name
def run(self):
image_location = capture_image.take_photo(self.photo_name)
image = QImage(image_location)
to_pixmap = QPixmap.fromImage(image).scaled(200, 200)
to_qicon = QIcon(to_pixmap)
self.data_downloaded.emit(QListWidgetItem(to_qicon, image_location))
class MainWindow(QMainWindow, Ui_MainWindow):
def take_photo(self):
import time
for x in range(2):
photo_name = str(x) +'.jpg'
downloader = DownloadThread(photo_name)
downloader.data_downloaded.connect(self.on_photo_ready)
downloader.start()
time.sleep(5)
def on_photo_ready(self, photo):
print "WHY"
self.listWidget.addItem(photo)
I have some simple print statement in the function being called so the terminal looks like this:
Photo Photo Photo Photo Photo Photo WHY WHY WHY WHY WHY WHY
Meaning it waits to actually call emit until the for loop is complete and not on its own thread as intended. Any help would be AWESOME!