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?
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With