What I'm trying to accomplish is fetching results from an API and the results are a bunch of classes that take a bit of time to fetch.
Note that each of the results contains a link to a picture that will take time to load on the PyQt5 application.
I need to make the whole process listed above happens in another thread.
What I tried is using QThread and signals which I do not think that Im doing correctly so here is a minimal reproducable example.
| - [Main.py]
class Main(object):
def __init__(self):
self.results = MyAPISearchEngine("SearchQuery") # I can switch from normal list to a generator
def setup_ui(self):
self.vLayout = QVBoxLayout(self.scrollAreaW)
for r in self.results:
def loadData(data):
result, img = data
wrapperW = QWidget()
title = QLabel(wrapperW)
title.setText(str(result))
imgLabel = QLabel(wrapperW)
imgLabel.setPixmap(img)
self.vLayout.addWidget(wrapperW)
self.thread = Threads.MyThread(r)
self.thread.dataLoaded.connect(loadData)
self.thread.start()
| - [Threads.py]
Assuming that the picture url is inside my class's
Object.url
and the string representation ofObject
is its title.
class MyThread(QThread):
dataLoaded = pyqtSignal(object)
def __init__(self, result, *args, **kwargs):
super(MyThread, self).__init__(*args, **kwargs)
self.result = result
print("Thread is started!")
def start(self):
print("Thread is started!!")
data = urlopen(self.result.url).read()
image = QtGui.QPixmap()
image.loadFromData(data)
x = (self.result, image)
self.dataLoaded.emit(x)
atm the thread is not working it just prints out this
Thread is started!
Thread is started!
QThread: Destroyed while thread is still running
if you are wondering what is attribute project
in setup_ui all about
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
Project = QtWidgets.QTabWidget()
ui = Main()
ui.setup_ui(Project)
Project.show()
sys.exit(app.exec_())