Results 1 to 5 of 5

Thread: Open PDF using Button Click

  1. #1
    Join Date
    Jul 2010
    Location
    TN
    Posts
    10
    Thanks
    2
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Android

    Question 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:

    Qt Code:
    1. void MainWindow::on_actionManual_triggered()
    2. {
    3. QDesktopServices::openUrl(QUrl::fromLocalFile(QDir::currentPath() + "/docs/" + "nameof.pdf"()));
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void MainWindow::on_actionManual_triggered()
    2. {
    3. //QDesktopServices::openUrl(QUrl("file:///docs/nameof.pdf"));
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    Neither work lol. I know I am probably missing something minor but can't put my finger on it.

    Any suggestions?

  2. #2
    Join Date
    Jul 2018
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Open PDF using Button Click

    Hi! I think your file path is wrong. Try my solution:

    Qt Code:
    1. QDesktopServices::openUrl(QUrl::fromLocalFile(qApp->applicationDirPath() + "/docs/" + "nameof.pdf"));
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Cobra91151 for this useful post:

    tndave (7th August 2018)

  4. #3
    Join Date
    Jul 2010
    Location
    TN
    Posts
    10
    Thanks
    2
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Android

    Default Re: Open PDF using Button Click

    Quote Originally Posted by Cobra91151 View Post
    Hi! I think your file path is wrong. Try my solution:

    Qt Code:
    1. QDesktopServices::openUrl(QUrl::fromLocalFile(qApp->applicationDirPath() + "/docs/" + "nameof.pdf"));
    To copy to clipboard, switch view to plain text mode 
    Cobra91151

    that worked like a charm! Thank you very much for your assistance. Gonna save this info for future use lol.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Open PDF using Button Click

    Defensive programming tip: Always create a local QString variable to hold the result of expressions like

    Qt Code:
    1. qApp->applicationDirPath() + "/docs/" + "nameof.pdf"
    To copy to clipboard, switch view to plain text mode 

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    tndave (7th August 2018)

  7. #5
    Join Date
    Jul 2010
    Location
    TN
    Posts
    10
    Thanks
    2
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows Android

    Question Re: Open PDF using Button Click

    Quote Originally Posted by d_stranz View Post
    Defensive programming tip: Always create a local QString variable to hold the result of expressions like

    Qt Code:
    1. qApp->applicationDirPath() + "/docs/" + "nameof.pdf"
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. Replies: 3
    Last Post: 15th March 2012, 16:30
  2. Replies: 2
    Last Post: 28th November 2011, 16:12
  3. Replies: 0
    Last Post: 8th November 2011, 12:16
  4. Replies: 0
    Last Post: 10th September 2010, 14:23
  5. How to open any file on button click???
    By r3aktor in forum Newbie
    Replies: 5
    Last Post: 8th July 2009, 09:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.