How to load a PDF asynchronously using QtPDF (using threading)?
-
I'm trying to load a PDF in a Qt Widget application using threading. Irrespective of what I do, it fails to load the PDF. I tried creating a concurrent thread, made a new QPdfDocument, loaded a PDF into the entity and then moved it to the main thread before returning the pointer. It still doesn't work! Here is an MWE (I know that the PDF loading in this example is synchronous, but even this fails to work - forget about loading the PDF asynchronously):
QFuture<QPdfDocument*> future = QtConcurrent::run([](){ QPdfDocument* loadDoc = new QPdfDocument(); loadDoc->load(ProjectSettings::instance().pdfPath); loadDoc->moveToThread(QApplication::instance()->thread()); return loadDoc; }); m_document = future.result(); -
I'm trying to load a PDF in a Qt Widget application using threading. Irrespective of what I do, it fails to load the PDF. I tried creating a concurrent thread, made a new QPdfDocument, loaded a PDF into the entity and then moved it to the main thread before returning the pointer. It still doesn't work! Here is an MWE (I know that the PDF loading in this example is synchronous, but even this fails to work - forget about loading the PDF asynchronously):
QFuture<QPdfDocument*> future = QtConcurrent::run([](){ QPdfDocument* loadDoc = new QPdfDocument(); loadDoc->load(ProjectSettings::instance().pdfPath); loadDoc->moveToThread(QApplication::instance()->thread()); return loadDoc; }); m_document = future.result();@Bubu-Droid
Why do you first create https://forum.qt.io/post/839002, to which I have replied, and then this new topic? It is precisely because people answer in one place that we don't want you to raise duplicates.I posted there:
Obviously to start with:
- QPdfDocument::Error QPdfDocument::error() const (and statusChanged(QPdfDocument::Status status)). This is vital and first thing you should check.
- Verify
ProjectSettings::instance().pdfPathexists. - Have you tried multiple/absolute minimal PDF documents?
- Get rid of any concurrency/moveToThread.
- Is it safe/sensible to call
loadDoc->moveToThread()whileloadDoc->load()is running? Even if it is allowed, why/what is the effect of setting off a load before moving to a thread?
Please pick one or other topic and stick to it.