Results 1 to 17 of 17

Thread: Call extern app Adobe Acrobat to open *.pdf

  1. #1
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Call extern app Adobe Acrobat to open *.pdf

    Hi, i need help
    i do not know how i can call external view (Adobe) to open *.pdf ... from Qt.

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    OSX has the command "open". Just create a QProcess with the string "open <PATH_TO_YOUR_PDF>". OSX will use the default application to open the file. Windows and Linux have similiar commands.

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

    Lodhart (26th April 2009)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    Funny, i was searching in our sourcecode today and found the code, where we implemented the external open. Maybe it will help you a bit more than me last short answer. (System and Environment are our own classes, but I think you will get it) the env variable DESKTOP_LAUNCH points to the program that handles the file open (under linux) if that variable is not set, than the default launcher is used.

    Qt Code:
    1. const QString System::EnvironmentVariableIdentifyingDesktopLauncher("DESKTOP_LAUNCH");
    2.  
    3.  
    4. QString System::getDefaultApplicationLauncher() {
    5. if (Environment::isSet(EnvironmentVariableIdentifyingDesktopLauncher)) {
    6. QString launcher = Environment::get(EnvironmentVariableIdentifyingDesktopLauncher);
    7. return launcher + " %1";
    8. } else if (System::isWindows()) {
    9. return "cmd /c \"start %1\"";
    10. } else if (System::isMacOSX()) {
    11. return "open \"%1\"";
    12. } else if (System::runningGNOME()) {
    13. return "gnome-open \"%1\"";
    14. } else if (System::runningKDE()) {
    15. return "kfmclient exec \"%1\"";
    16. } else {
    17. // last try. Only used by XFCE, but no better Idea for default
    18. return "exo-open \"%1\"";
    19. }
    20. }
    21.  
    22.  
    23. bool System::openFileWithTheDefaultApplication(const QUrl& url) {
    24. QString launcher = getDefaultApplicationLauncher();
    25. QString url_to_execute = url.toEncoded();
    26. return QProcess::execute(launcher.arg(url_to_execute));
    27. }
    To copy to clipboard, switch view to plain text mode 

    Maybe it can be done better, but it works for us The default case (the else) is not really perfekt, but we never get this far

  5. #4
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    I onlyy know how to start an excutive application:

    Qt Code:
    1. QObject *parent = NULL;
    2. QString program = "J:\\2\\exeshan\\Debug\\11.ppt";
    3. QStringList arguments;
    4. arguments << "-style" << "motif";
    5. QProcess *myProcess = new QProcess(this);
    6. myProcess->start(program, arguments);
    To copy to clipboard, switch view to plain text mode 

    the "program" is the path of the application you want to run.

  6. #5
    Join Date
    Feb 2009
    Posts
    33
    Thanks
    6
    Thanked 7 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    I'm using QProcess::startDetached, to do the same, fwiw.

  7. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    Jep. Thats better to prevent the GUI from freezing if the started program takes a long time to start.

  8. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    why don't you just use QDesktopServices:: openUrl?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    nightghost (27th April 2009)

  10. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    Cool. I didn't know about this class. Works great.

  11. #9
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    Quote Originally Posted by spirit View Post
    why don't you just use QDesktopServices:: openUrl?
    I have tried this, but it cannot open files such as ".pdf", ".txt", and it only can run ".exe" applications. The following is my codes:
    Qt Code:
    1. if(!QDesktopServices::openUrl(QUrl(path)))
    2. {
    3. qDebug() << "Fail to run the file";
    4. }
    To copy to clipboard, switch view to plain text mode 

    the path is the path of the file to be run or open.
    thanks

  12. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    can you provide a compilable example?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #11
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    Maybe the source codes are a little complicated:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5. }
    6.  
    7. MainWindow::~MainWindow()
    8. {
    9. delete ui;
    10. }
    11.  
    12. bool MainWindow::on_addExeButton_clicked()
    13. {
    14. QString filename = QFileDialog::getOpenFileName(this, tr("choose the file or application"), qApp->applicationDirPath(), tr("Excutable(*.*)"));
    15. if(filename.isNull())
    16. return false;
    17. //User chooses a file that has been chosen before
    18. if(buttons.keys().contains(filename))
    19. {
    20. QMessageBox::critical(this, tr("Existed File"), tr("Sorry but the file you choose has been chose before.\n"
    21. "check the buttons on the interface!!"));
    22. return false;
    23. }
    24. //User chooses a new file which has not been selected before
    25. QFileInfo fileInfo(filename);
    26. QIcon icon = seekIcon.icon(fileInfo);
    27. btn->resize( 500, 500);
    28. btn->setIcon(icon);
    29. connect(btn, SIGNAL(clicked()), this, SLOT(callExe()));
    30. //click the button to run the corresponding exe or open the file
    31. buttons.insert(filename, btn);
    32. ui->verticalLayout1->addWidget(btn);
    33. ui->verticalLayout1->addStretch();
    34. qDebug() << buttons.size();
    35. return true;
    36. }
    37.  
    38. void MainWindow::callExe()
    39. {
    40. qDebug() << "Ready to execute program";
    41. QToolButton *sender = qobject_cast<QToolButton*>(this->sender());
    42. //This SLOT is called by these inner code, not the user, then return
    43. if(!sender)
    44. {
    45. return;
    46. }
    47.  
    48. qDebug() << "Sender found successfully!";
    49. QString path;
    50. QMapIterator<QString, QToolButton *> i(buttons);
    51. while(i.hasNext())
    52. {
    53. // qDebug() << "Enter the while loop successfully!";
    54. if(i.peekNext().value() == sender)
    55. {
    56. path = i.next().key();
    57. qDebug() << "Find the path!!\n";
    58. break;
    59. }
    60. else
    61. {
    62. i.next();
    63. }
    64. }
    65.  
    66. if(path.isEmpty())
    67. {
    68. qDebug() << "******File path is null!!";
    69. return;
    70. }
    71. //打开相应程序 In English------Using QProcess to run the applications
    72. //Using class QDesktopServices to run the files or applications
    73. if(!QDesktopServices::openUrl(QUrl(path)))
    74. {
    75. qDebug() << "Fail to run the file";
    76. }
    77.  
    78. qDebug() << "SLOT successfully!" << endl;
    79. }
    To copy to clipboard, switch view to plain text mode 

  14. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    maybe something wrong with path.
    try this example, works fine for me.
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. const QString file = QFileDialog::getOpenFileName();
    9.  
    10. if (!QDesktopServices::openUrl(file))
    11. qDebug() << "fail";
    12.  
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  15. #13
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    We've sometimes problems (only under Windows) if the string points to a local file and the implicit cast is used. Using QUrl::fromLocalFile() works always. In the above code change line 10:

    Qt Code:
    1. if (!QDesktopServices::openUrl(file))
    To copy to clipboard, switch view to plain text mode 

    to
    Qt Code:
    1. if (!QDesktopServices::openUrl(QUrl::fromLocalFile(file)))
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to nightghost for this useful post:

    sophister (27th April 2009)

  17. #14
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    yes, that's true.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  18. #15
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    Yea, only when I add the QUrl::from Local File() does the app works!!
    Thanks a lot!

    But I find that it cannot open files with extention ".png".
    So strange it is.

  19. #16
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    thanks, it do works.
    but, uh, it cannot open the files with extention ".png" or "jpg", a little strange.

  20. #17
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Call extern app Adobe Acrobat to open *.pdf

    in another application, it does works!!!

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.