I have some issues with this:
First I create my object and move it to a thread:
FileUploader *fileUploader = new FileUploader(fileList_, start, (offset == 0 ? (fileList_.count() - start) : offset));
QThread *fileUploaderThread = new QThread;
fileUploader->moveToThread(fileUploaderThread);
fileUploaderThreads_.append(fileUploaderThread);
fileUploaders_.append(fileUploader); // contains pointers to the objects
connect(fileUploader, SIGNAL(progressChangedAt(int)), model_, SLOT(reportProgressChanged(int)), Qt::QueuedConnection);
connect(fileUploader, SIGNAL(statusChangedAt(int)), model_, SLOT(reportStatusChanged(int)), Qt::QueuedConnection);
connect(fileUploader, SIGNAL(finished()), fileUploaderThread, SLOT(quit()), Qt::QueuedConnection);
connect(fileUploaderThread, SIGNAL(finished()), this, SLOT(checkIfFinished()), Qt::QueuedConnection);
The in the slot checkIfFinished() I wanna go though all the threads and see if they quit.
qDebug() << “one thread done”;
foreach(QThread *thread, fileUploaderThreads_) { // or FileUploader* fileuploader, fileUploaders_ ?
if(thread && !thread->isFinished()) {
qDebug() << "not finished " << thread->currentThreadId();
return; // not done
}
}
When this is printed out, I only get the main thread Id, not the threads. I tried to print the threads id’s out but with no luck (after they are started ofc).
The reason why I’m doing this I because the person writing “Advanced Qt Programming – Mark S” did a similar thing with QThreads that he placed in a list and checked if they where finished. The only thing thats working now is the connection which kills the thread when the fileUploader is done.
Also, how do I store pointers for the threads? I wounder how I will be able to delete them all if they don’t seem to point to the right threads.
Edit:
I tried to store the QObjects in a list instead and do this:
QThread *senderx = qobject_cast<QThread*>(sender());
qDebug() << "one thread done" << senderx;
foreach(FileUploader *fileUploader, fileUploaders_) {
if(fileUploader && !fileUploader->thread()->isFinished()) {
qDebug() << "not finished " << fileUploader->thread();
return; // not done
}
}
//done
qDebug() << "done";
setButtonState(false);
And my outprint on the last call is :
one thread done QThread(0x43ee180)
not finished QThread(0x43ee180)
How is this possible? It is done, but the method says otherwise.
you want
QObject::thread(), notQThread::currentThreadId()– the second one returns the thread that the function was called in.Once you fix that, your pointers will work fine.