qtpdfqurl

qt QUrl Opening a pdf


I am trying to open a pdf file with the default application through Qt's "DesktopServices" class.

But I am coming up with a ShellExecute

'file:///C:/PMPS/PMPSv1/Instuctionsforuse.pdf' failed (error 2). problem.

Here is my code:

#include <QDesktopServices>
#include <QUrl>

}

void Dialog::guideButtonClicked()
{
QDesktopServices::openUrl(QUrl("file:///C:/PMPS/PMPSv1/Instuctionsforuse.pdf"));
}

Solution

  • If you already has handlers in your Url (like file:// etc), or if your Url is already encoded and should be used without any conversion and changes, use QUrl::fromUserInput function. When the string is not already a valid URL, a best guess is performed, making various web related assumptions.

    QUrl localfile = QUrl::fromUserInput("file:///C:/PMPS/PMPSv1/Instuctionsforus‌​e.pdf"); 
    

    You can check if it is correct with:

     qDebug() << localfile.toLocalFile();