PDA

View Full Version : open a document



peace_comp
29th June 2008, 15:22
hi.
I would like to know if it possible to openb a document ( exp. pdf ) from a Qt application.
for example when the user click on an action (help action) a file.pdf( documentation ) should be opened..??
I tried with QProcess but I didnt get a result:


QProcess myprocess;
QString app="//path/file.pdf";
myprocess.start(app);

please if some one could help!!

jpn
29th June 2008, 15:23
Use QDesktopServices::openUrl() and QUrl::fromLocalFile().

peace_comp
29th June 2008, 19:24
thanks ..
I tried the code:



QDesktopServices desk;
desk.openUrl(QUrl::fromLocalFile("test.pdf"));


but it doesnt open anythg..

test.pdf is in the same directory as the .exe .. but it doesnt work??

I dont know where is the error

peace_comp
29th June 2008, 23:15
Oki I found it:
I used only

desc.openUrl(QUrl("test.pdf"));
it works!!
thanks for help

jpn
30th June 2008, 05:45
First of all, allocating an instance of QDesktopServices is pointless because QDesktopServices::openUrl() is a static method. QUrl::fromLocalFile("test.pdf") won't work because it would correspond to "file:///test.pdf".

patrik08
30th June 2008, 07:08
I use this way and run any time on each fullpath url



/* tr macro on no Q_OBJECT class !!!! */
static inline void OpenDesk( QUrl loc )
{
#if defined Q_WS_MAC
/* direct to open */
QProcess *m = new QProcess();
QStringList macs;
macs << loc.toString();
m->startDetached(QString("open") , macs );
return;
#endif

#if defined Q_WS_WIN
QString fullFileName = loc.toString();

if (fullFileName.startsWith("http://", Qt::CaseInsensitive) ||
fullFileName.startsWith("https://", Qt::CaseInsensitive) ||
fullFileName.startsWith("ftp://", Qt::CaseInsensitive) ||
fullFileName.startsWith("news://", Qt::CaseInsensitive) ||
fullFileName.startsWith("mailto:", Qt::CaseInsensitive) ||
fullFileName.startsWith("webdav://", Qt::CaseInsensitive) )
{

} else {
fullFileName.prepend("file:///"); /* or qurl to localfile */
}
bool wr = QDesktopServices::openUrl(QUrl(fullFileName));
if (!wr) {
QMessageBox::warning(0,QString("Error"),
QString("Window Unable to open action file or dir %1").arg(loc.toString()));
}
return;
#endif
/* linux */
bool r = QDesktopServices::openUrl(loc);
if (!r) {
QMessageBox::warning(0,QString("Error"),
QString("Linux Unable to open action file or dir %1").arg(loc.toString()));
}

}