PDA

View Full Version : QDesktopServices open url & local file & pdf or doc



patrik08
26th October 2006, 10: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, 19: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, 19: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, 19: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, 21: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, 10:52
This works for me without any special magic tricks.

RS2411
16th February 2007, 15: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, 16: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()));

rupeshbhurke
13th April 2012, 14:40
Dear all,
thank you very much for invaluable inputs.

I found that using
QUrl::fromLocalFile(...) is quite useful but on linux systems (, mine is ubuntu 11.04), it can't be used with urls like
www.qtcenter.org (web addresses) It does open with
QDesktopServices::openUrl(...) So after some search I came across
QUrl::fromUserInput(...) I have tested it on WinXP, Win7, Ubuntu 11.04, MacOSX with success. I have tried opening following Urls with it (successfully):



/home (It's a dir path)
/usr/local (It's a dir path)
/home/rupesh/Pictures/1.jpg (it's a file)
/home/rupesh/Pictures/1 2 3 4.jpg (it's a file, please note spaces in file name)
/home/rupesh/Pictures/1.pdf (it's a file)
/home/rupesh/Pictures/1 2 3 4.pdf (it's a file, please note spaces in file name)
http://www.microsoft.com
www.microsoft.com


By the way the code I wrote was


return QDesktopServices::openUrl( QUrl::fromUserInput( theUrl ));


Please give me a feedback if you use it and come across any issues.

Thank you
Rupesh

ChrisW67
14th April 2012, 04:42
Why did you feel the need to resurrect a thread that has been dormant for five years to tell us about a documented function that did not exist until 2009 (Qt 4.6) and could not have been the solution the OP was looking for?



I found that using
QUrl::fromLocalFile(...) is quite useful but on linux systems (, mine is ubuntu 11.04), it can't be used with urls like
www.qtcenter.org (web addresses)

Why would you expect it to do anything useful with that? "www.qtcentre.org" is not a local file is it?