PDA

View Full Version : open an external file in default app from my OS



juliano.gomes
7th December 2015, 20:19
Hello,

I'm sorry for ask something basic like this, but i searched all day and nor sure how do it...

there are any way to open an external file in default app from my OS?

eg:



QString s = qApp->applicationDirPath() + QDir::toNativeSeparators("/PDF_file.pdf");
s.open();
// or
s.execute();


many thanks!
Juliano

ChrisW67
7th December 2015, 20:43
QDesktopServices::openUrl() and QUrl::fromLocalFile()

juliano.gomes
8th December 2015, 13:30
QDesktopServices::openUrl() and QUrl::fromLocalFile()

So many thanks! Solved on linux

my code will work on windows too?



#include <QDir>
#include <QUrl>
#include <QDesktopServices>

void MainWindow::on_action_openfile_triggered()
{
QString s = qApp->applicationDirPath() + QDir::toNativeSeparators("/pdf_file.pdf");
QDesktopServices::openUrl(QUrl::fromLocalFile(s));
}

ChrisW67
8th December 2015, 20:12
It should work as long as the desktop shell has an association between the PDF extension and a reader. Not sure what happens if no association exists.