Opening a PDF with openURL
-
I am trying to open a PDF when a push button is pressed, I currently have the PDF added in the resources folder and I also updated the .qrc file. However when I try to print out the URL it prints out nothing.
But when I give the full path of the PDF (without the colon) it is able to open.
My question is how would you access files in the resources folder? In particular how would you open a PDF thats in the resources folder?
Thanks in advance!
-
Hi and welcome to devnet,
Your OS has no idea how to access files in Qt's resources. The usual way is to copy the file in a temporary one and open that copy.
-
Hi and welcome to devnet,
Your OS has no idea how to access files in Qt's resources. The usual way is to copy the file in a temporary one and open that copy.
-
@SGaist Thanks for the reply!
By copying the file to a temporary one do you mean copying the PDF to a local path with something like QFile::Copy?
Is there any other way because I would prefer to not play around with the user's local machine.
@David1246 said in Opening a PDF with openURL:
Is there any other way because
Other way would be to not to put the PDF into a Qt resource, but bundle it with your app as normal file.
-
If you do ultimately choose to stream it to disk and open it from the disk file, then I shared a code snippet for that on a prior thread: https://forum.qt.io/topic/115682/how-to-get-the-path-of-a-c-file-which-is-added-as-the-resource-file/11
// note the ":" at the front of the path std::string path_inside_qrc = ":/data/sim_rpc_replies_immediate_scores.pbtxt"; std::unique_ptr<QTemporaryFile> temporary_file; temporary_file.reset( QTemporaryFile::createNativeFile(QString::fromStdString(path_inside_qrc)) ); std::string file_name = temporary_file->fileName().toUtf8().constData(); // TODO: add code to use the temporary file. // Delete the temp file after using it temporary_file.reset();
-
After my prior reply here, I got to thinking...
Since the Qt method
createNativeFile
knows where to locate the bytes that it streams into the temporary on-disk file, we can go spelunking into the Qt implementation to see where those bytes are.That will take us an adventure through:
- QFileDevice
- QAbstractFileEngine
- QResourceFileEngine
- QResourceFileEngine::open
- QResourceFileEnginePrivate::uncompress
However... even then... knowing where the bytes are won't help you interact with
QDesktopServices::openUrl
.The
openUrl
function is documented as: "Opens the given url in the appropriate Web browser for the user's desktop environment"Since the file needs to be opened in a totally separate application than the Qt app that is invoking
openUrl
, it continues to be unworkable to use "qrc file bytes" that are compiled into the Qt app binary.You really do have to either create a temporary copy (like @SGaist said) or install the PDF file side-by-side with your application, but as a separate file on disk (not compiled in using qrc), as @jsulm said.
-
@David1246 said in Opening a PDF with openURL:
Is there any other way because
Other way would be to not to put the PDF into a Qt resource, but bundle it with your app as normal file.
-
After my prior reply here, I got to thinking...
Since the Qt method
createNativeFile
knows where to locate the bytes that it streams into the temporary on-disk file, we can go spelunking into the Qt implementation to see where those bytes are.That will take us an adventure through:
- QFileDevice
- QAbstractFileEngine
- QResourceFileEngine
- QResourceFileEngine::open
- QResourceFileEnginePrivate::uncompress
However... even then... knowing where the bytes are won't help you interact with
QDesktopServices::openUrl
.The
openUrl
function is documented as: "Opens the given url in the appropriate Web browser for the user's desktop environment"Since the file needs to be opened in a totally separate application than the Qt app that is invoking
openUrl
, it continues to be unworkable to use "qrc file bytes" that are compiled into the Qt app binary.You really do have to either create a temporary copy (like @SGaist said) or install the PDF file side-by-side with your application, but as a separate file on disk (not compiled in using qrc), as @jsulm said.
@KH-219Design
Thanks so much for the detailed response!! Ill try what you suggested and get back with the results -
@jsulm
I see thank you!One question I have is how would you bundle it with my app? Like in the cmake file?
@David1246 See "Installing Files" in https://doc.qt.io/qt-5/qmake-advanced-usage.html
Or do you use CMake instead of qmake?