Open PDF using Button Click
Good afternoon,
Have a little problem , I am trying to open a PDF from a sub-directory in my application folder when a button or menu item is clicked. I have tried a couple different things but neither seem to work. They are as follows:
Code:
void MainWindow::on_actionManual_triggered()
{
}
and
Code:
void MainWindow::on_actionManual_triggered()
{
//QDesktopServices::openUrl(QUrl("file:///docs/nameof.pdf"));
}
Neither work lol. I know I am probably missing something minor but can't put my finger on it.
Any suggestions?
Re: Open PDF using Button Click
Hi! I think your file path is wrong. Try my solution:
Code:
QDesktopServices::openUrl(QUrl::fromLocalFile(qApp
->applicationDirPath
() + "/docs/" + "nameof.pdf"));
Re: Open PDF using Button Click
Quote:
Originally Posted by
Cobra91151
Hi! I think your file path is wrong. Try my solution:
Code:
QDesktopServices::openUrl(QUrl::fromLocalFile(qApp
->applicationDirPath
() + "/docs/" + "nameof.pdf"));
Cobra91151
that worked like a charm! Thank you very much for your assistance. Gonna save this info for future use lol.
Re: Open PDF using Button Click
Defensive programming tip: Always create a local QString variable to hold the result of expressions like
Code:
qApp->applicationDirPath() + "/docs/" + "nameof.pdf"
so that when things don't work as expected, you can look at the contents of the QString in the debugger or a QMessageBox in release mode and verify that the string actually contains the path you expect.
The environment that gets set up when you run in the debugger vs. running standalone from the command line (or an icon click from the desktop) often leads to unexpected values for paths. If you simply build a string and pass it directly into a method, you don't really have any good way of examining that string to figure out what's wrong.
Re: Open PDF using Button Click
Quote:
Originally Posted by
d_stranz
Defensive programming tip: Always create a local QString variable to hold the result of expressions like
Code:
qApp->applicationDirPath() + "/docs/" + "nameof.pdf"
so that when things don't work as expected, you can look at the contents of the QString in the debugger or a QMessageBox in release mode and verify that the string actually contains the path you expect.
The environment that gets set up when you run in the debugger vs. running standalone from the command line (or an icon click from the desktop) often leads to unexpected values for paths. If you simply build a string and pass it directly into a method, you don't really have any good way of examining that string to figure out what's wrong.
Thank you very much for the tip d_stranz. Being somewhat new to C++ and QT any and all tips are welcome :)