Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lauch an external file from Qt?

I'd like to open a pdf file simply double-clicking on one element of a QListWidget. I created a batch file to open Acrobat Reader (reader.bat), but I want a specific pdf file:

void MainWindow::on_FileListWidget_itemDoubleClicked(QListWidgetItem *item)
{
        QFile SelectedModel(Current_Path  + "/Template/" + item->text());
        QString FileName;
        FileName = (Current_Path  + "/Template/" + item->text());
        ::system("e:\\reader.bat");
}

1) I don't know what's could be better from QFile or QString in order to identify and choose the file from the QListWidget; 2) I dont' know how to add the file to the command to open Acorbat Reader (in the shown line I'm able to open the program but not my file). Any idea?

like image 462
LittleSaints Avatar asked Apr 15 '14 09:04

LittleSaints


2 Answers

You can use Qt native methods to achieve this behaviour by calling

QDesktopServices::openUrl(QUrl::fromLocalFile("someFilePath"));

See QUrl::fromLocalFile() and QDesktopServices::openUrl()

(Refer to this post)

like image 132
Bowdzone Avatar answered Nov 06 '22 18:11

Bowdzone


Take a look at this: http://qt-project.org/doc/qt-5/qdesktopservices.html#openUrl

QDesktopServices::openUrl(QUrl("file://path/to/file"));

This not only takes care of opening the file with the default application (if the file:// scheme is used), but will also open a web URL with the default browser (if the http:// or https:// scheme is used) or will open it with the default email client in case of a mailto:// scheme. It works with other schemes like steam://, itunes:// etc. as well.

like image 40
Rohan Prabhu Avatar answered Nov 06 '22 18:11

Rohan Prabhu