Results 1 to 10 of 10

Thread: QDesktopServices open url & local file & pdf or doc

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QDesktopServices open url & local file & pdf or doc

    Before QDesktopServices qt4.2 i use this function to open explorer file url & other mailto:xxx ecc....

    Qt Code:
    1. /* open file browser explorer finder pdf doc ecc... url mail ecc on other programm */
    2. void Base_Function::OpenUrl_File_Dir_Dektop( QString item )
    3. {
    4. #if defined(Q_WS_WIN)
    5. s << "url.dll,FileProtocolHandler" << item;
    6. p.startDetached(QString("rundll32.exe") , s );
    7. #endif
    8. #if defined Q_WS_MAC
    9. macs << item; /* oeffnet der default browser */
    10. m.startDetached(QString("open") , macs ); /* open file weburl .... finder e other */
    11. #endif
    12. }
    To copy to clipboard, switch view to plain text mode 

    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...


    Qt Code:
    1. #include <QDesktopServices>
    2. #define HOME_DIR_YOUR \
    3. QString( "%1/ale.png" ).arg( QDir::homePath())
    4. QUrl youhome(HOME_DIR_YOUR); /* filee path not open */
    5. QUrl youhome1("http://www.qtcentre.org/forum/"); /* running open browser*/
    6. QDesktopServices::openUrl (youhome);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDesktopServices open url & local file & pdf or doc

    Quote Originally Posted by patrik08 View Post
    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.

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDesktopServices open url & local file & pdf or doc

    Quote Originally Posted by jacek View Post
    Are you sure?
    Oh yes.... i testet on window & Mac .... Tiger qt4.2



    Qt Code:
    1. #include <QDesktopServices>
    2. #define HOME_DIR_YOUR \
    3. QString( "%1/ale.png" ).arg( QDir::homePath())
    4.  
    5. /* image exist */
    6.  
    7.  
    8. QUrl youhome(HOME_DIR_YOUR); /* file path not open */
    9. QDesktopServices::openUrl (youhome);
    10.  
    11.  
    12. /* if i use url.dll,FileProtocolHandler win or mac /usr/bin/open filexx GIMP start on win & imageprewiuv on mac */
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDesktopServices open url & local file & pdf or doc

    Quote Originally Posted by patrik08 View Post
    #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?

  5. #5
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QDesktopServices open url & local file & pdf or doc

    Quote Originally Posted by jacek View Post
    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....

    Qt Code:
    1. QString IsNetFile( QString fullFileName )
    2. {
    3. if (fullFileName.startsWith("http://", Qt::CaseInsensitive) or
    4. fullFileName.startsWith("https://", Qt::CaseInsensitive) or
    5. fullFileName.startsWith("ftp://", Qt::CaseInsensitive) or
    6. fullFileName.startsWith("news://", Qt::CaseInsensitive) or
    7. fullFileName.startsWith("mailto:", Qt::CaseInsensitive) or
    8. fullFileName.startsWith("webdav://", Qt::CaseInsensitive) )
    9. {
    10. return fullFileName;
    11. } else {
    12. return fullFileName.prepend("file:///");
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include <QDesktopServices>
    2. #define HOME_DIR_YOUR \
    3. QString( "%1/ale.png" ).arg( QDir::homePath())
    4.  
    5. qDebug() << "### HOME_DIR_YOUR " << HOME_DIR_YOUR;
    6. QUrl youhome(HOME_DIR_YOUR.prepend("file:///")); /* filee path not open */
    7. qDebug() << "### youhome.toString() " << youhome.toString();
    8. qDebug() << "### youhome.toLocalFile() " << youhome.toLocalFile();
    9. QUrl youhome1("http://www.qtcentre.org/forum/"); /* running open browser*/
    10. QDesktopServices::openUrl (youhome);
    11.  
    12.  
    13.  
    14. ### HOME_DIR_YOUR "C:/Documents and Settings/ppk-it/ale.png"
    15. ### youhome.toString() "file:///C:/Documents and Settings/ppk-it/ale.png"
    16. ### youhome.toLocalFile() "C:/Documents and Settings/ppk-it/ale.png"
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDesktopServices open url & local file & pdf or doc

    This works for me without any special magic tricks.
    Attached Files Attached Files

  7. The following user says thank you to wysota for this useful post:

    pmaktieh.sirhc (22nd March 2007)

  8. #7
    Join Date
    Feb 2007
    Posts
    1
    Thanks
    1

    Default Re: QDesktopServices open url & local file & pdf or doc

    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?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDesktopServices open url & local file & pdf or doc

    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:
    Qt Code:
    1. bool r = QDesktopServices::openUrl(QUrl(path->text()));
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. bool r = QDesktopServices::openUrl(QUrl::fromLocalFile(path->text()));
    To copy to clipboard, switch view to plain text mode 

  10. The following 4 users say thank you to wysota for this useful post:

    giowck (11th March 2011), pavanbarot (9th February 2012), RS2411 (19th February 2007), totem (27th September 2010)

  11. #9
    Join Date
    Jun 2010
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDesktopServices open url & local file & pdf or doc

    Dear all,
    thank you very much for invaluable inputs.

    I found that using
    Qt Code:
    1. QUrl::fromLocalFile(...)
    To copy to clipboard, switch view to plain text mode 
    is quite useful but on linux systems (, mine is ubuntu 11.04), it can't be used with urls like
    Qt Code:
    1. www.qtcenter.org (web addresses)
    To copy to clipboard, switch view to plain text mode 
    It does open with
    Qt Code:
    1. QDesktopServices::openUrl(...)
    To copy to clipboard, switch view to plain text mode 
    So after some search I came across
    Qt Code:
    1. QUrl::fromUserInput(...)
    To copy to clipboard, switch view to plain text mode 
    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

  12. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDesktopServices open url & local file & pdf or doc

    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?

    Quote Originally Posted by rupeshbhurke View Post
    I found that using
    Qt Code:
    1. QUrl::fromLocalFile(...)
    To copy to clipboard, switch view to plain text mode 
    is quite useful but on linux systems (, mine is ubuntu 11.04), it can't be used with urls like
    Qt Code:
    1. www.qtcenter.org (web addresses)
    To copy to clipboard, switch view to plain text mode 
    Why would you expect it to do anything useful with that? "www.qtcentre.org" is not a local file is it?
    Last edited by ChrisW67; 14th April 2012 at 04:47.

Similar Threads

  1. Replies: 3
    Last Post: 7th October 2015, 19:43
  2. Qt 4.1.4 on VS2005 error- cannot open input file 'qtmain.lib'
    By Ashish in forum Installation and Deployment
    Replies: 10
    Last Post: 11th October 2006, 16:05
  3. QProcess open all file -> url.dll,FileProtocolHandler
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2006, 17:07
  4. Opening swf file in the default browser
    By munna in forum Qt Programming
    Replies: 16
    Last Post: 5th May 2006, 09:33
  5. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.