View Full Version : QDesktopServices open url & local file & pdf or doc
patrik08
26th October 2006, 11:47
Before QDesktopServices qt4.2 i use this function to open explorer file url & other mailto:xxx ecc....
/* open file browser explorer finder pdf doc ecc... url mail ecc on other programm */
void Base_Function::OpenUrl_File_Dir_Dektop( QString item )
{
#if defined(Q_WS_WIN)
QProcess p;
QStringList s;
s << "url.dll,FileProtocolHandler" << item;
p.startDetached(QString("rundll32.exe") , s );
#endif
#if defined Q_WS_MAC
QProcess m;
QStringList macs;
macs << item; /* oeffnet der default browser */
m.startDetached(QString("open") , macs ); /* open file weburl .... finder e other */
#endif
}
Now QDesktopServices open only Url http:/..... or mail to ....
& !not open file pdf or doc or other same as up write function....
wath is the location of QDesktopServices to expand this class? and Add other url different as http://
but now QDesktopServices is super to open url on linux.... method "open" exist only on mac...
#include <QDesktopServices>
#define HOME_DIR_YOUR \
QString( "%1/ale.png" ).arg( QDir::homePath())
QUrl youhome(HOME_DIR_YOUR); /* filee path not open */
QUrl youhome1("http://www.qtcentre.org/forum/"); /* running open browser*/
QDesktopServices::openUrl (youhome);
jacek
26th October 2006, 20:33
Now QDesktopServices open only Url http:/..... or mail to ....
& !not open file pdf or doc or other same as up write function....
Are you sure?
bool QDesktopServices::openUrl ( const QUrl & url ) [static]
Opens the given url in the appropriate web browser for the user's desktop environment, and returns true if successful; otherwise returns false.
If the URL is a reference to a local file (i.e. the URL scheme is "file") then it will be opened with a suitable application instead of a web browser.
If a mailto URL is specified, the user's e-mail client will be used to open a composer window containing the options specified in the URL, similar to the way mailto links are handled by a web browser.
patrik08
26th October 2006, 20:45
Are you sure?
Oh yes.... i testet on window & Mac .... Tiger qt4.2
#include <QDesktopServices>
#define HOME_DIR_YOUR \
QString( "%1/ale.png" ).arg( QDir::homePath())
/* image exist */
QUrl youhome(HOME_DIR_YOUR); /* file path not open */
QDesktopServices::openUrl (youhome);
/* if i use url.dll,FileProtocolHandler win or mac /usr/bin/open filexx GIMP start on win & imageprewiuv on mac */
jacek
26th October 2006, 20:58
#define HOME_DIR_YOUR \
QString( "%1/ale.png" ).arg( QDir::homePath())
...
QUrl youhome(HOME_DIR_YOUR);
Are you sure this creates a valid QUrl object? What does "youhome.isValid()" return? Shouldn't there be "file://" in front of the string?
patrik08
26th October 2006, 22:09
Are you sure this creates a valid QUrl object? What does "youhome.isValid()" return? Shouldn't there be "file://" in front of the string?
Now is running .....
If i write a Qurl class i append this small code..... on return value....
QString IsNetFile( QString fullFileName )
{
if (fullFileName.startsWith("http://", Qt::CaseInsensitive) or
fullFileName.startsWith("https://", Qt::CaseInsensitive) or
fullFileName.startsWith("ftp://", Qt::CaseInsensitive) or
fullFileName.startsWith("news://", Qt::CaseInsensitive) or
fullFileName.startsWith("mailto:", Qt::CaseInsensitive) or
fullFileName.startsWith("webdav://", Qt::CaseInsensitive) )
{
return fullFileName;
} else {
return fullFileName.prepend("file:///");
}
}
#include <QDesktopServices>
#define HOME_DIR_YOUR \
QString( "%1/ale.png" ).arg( QDir::homePath())
qDebug() << "### HOME_DIR_YOUR " << HOME_DIR_YOUR;
QUrl youhome(HOME_DIR_YOUR.prepend("file:///")); /* filee path not open */
qDebug() << "### youhome.toString() " << youhome.toString();
qDebug() << "### youhome.toLocalFile() " << youhome.toLocalFile();
QUrl youhome1("http://www.qtcentre.org/forum/"); /* running open browser*/
QDesktopServices::openUrl (youhome);
### HOME_DIR_YOUR "C:/Documents and Settings/ppk-it/ale.png"
### youhome.toString() "file:///C:/Documents and Settings/ppk-it/ale.png"
### youhome.toLocalFile() "C:/Documents and Settings/ppk-it/ale.png"
wysota
27th October 2006, 11:52
This works for me without any special magic tricks.
RS2411
16th February 2007, 16:50
Hi wysota,
I tried your code snipet and it works fine. The only problem I discovered is that it won't work anymore if there are spaces within the filename or path. I tried to find a workaround but I didn't find anything.
Anybody any ideas?
wysota
16th February 2007, 17:58
This has to be a valid URL and URL doesn't allow spaces. You might need to convert it from a local file path. Substitute:
bool r = QDesktopServices::openUrl(QUrl(path->text()));
with
bool r = QDesktopServices::openUrl(QUrl::fromLocalFile(path->text()));
vBulletin® v3.7.1, Copyright ©2000-2008, Jelsoft Enterprises Ltd.