Results 1 to 9 of 9

Thread: [Qt4.1]How to make links open browsers?

  1. #1
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [Qt4.1]How to make links open browsers?

    I have some html code in my qlabels and qtextedits. How can I make hyper links in QLabel and QTextEdit open the default web browser pointing to the given links?

    thanx in advance

    Pat

  2. #2
    Join Date
    Jan 2006
    Location
    N.B. Canada
    Posts
    47
    Thanked 8 Times in 7 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4.1]How to make links open browsers?

    There is no cross-platform way of doing this. You could try QDS - Q Desktop Services, specifically the Launcher service.
    Or something like the following code from the old forums:

    Qt Code:
    1. bool open_browser(QWidget* parent, const QString& rUrl)
    2. {
    3. bool result = false;
    4. QApplication::setOverrideCursor(Qt::BusyCursor);
    5. #ifdef Q_WS_WIN
    6. result = int(ShellExecuteW(parent->winId(),
    7. 0,
    8. rUrl.ucs2(),
    9. 0,
    10. 0,
    11. SW_SHOWNORMAL)) > 32;
    12. #else
    13. Q_UNUSED(parent);
    14. // Try a range of browsers available on UNIX, until we (hopefully)
    15. // find one that works. Start with the most popular first.
    16.  
    17. QProcess process;
    18. bool process_started = false;
    19. process.setArguments(QStringList() << "netscape" << rUrl);
    20.  
    21. process_started = process.start();
    22.  
    23. if (!process_started)
    24. {
    25. process.setArguments(QStringList() << "mozilla" << rUrl);
    26.  
    27. process_started = process.start();
    28. }
    29. if (!process_started)
    30. {
    31. process.setArguments(QStringList() << "firefox" << rUrl);
    32.  
    33. process_started = process.start();
    34. }
    35. if (!process_started)
    36. {
    37. process.setArguments(QStringList() << "konqueror" << rUrl);
    38.  
    39. process_started = process.start();
    40. }
    41. result = process_started;
    42. #endif
    43. QApplication::restoreOverrideCursor();
    44.  
    45. return result;
    46. }
    To copy to clipboard, switch view to plain text mode 

    Note that this is Qt3, so some changes may be required for Qt 4. Basically on Windows the code inside #ifdef Q_WS_WIN opens the default web browser. On Unix things are more complicated. Here, the code tries all kinds of different browsers until it succeeds.

    Sometimes the user may have the $BROWSER variable setup that holds the "default" browser.

    You can have an option for the user to set what browser they want to use. Many Linux apps do this, like QtAssistant (good source code here), etc... It has the benefit that it provides the user with control of setting that. For example, with the code above, your app will always open my mozilla browser (since it comes first), even though I may prefer firefox, or konqueror, which I also have. And there is no way for me to alter this behaviour.

    Ofcourse, you can have an option and the above code. If the option isn't set, try above, and give up if you dont find a browser. If the user has a browser option set, open the prefered browser.

    Under Linux, there really isn't such a thing as "the default browser".

    Bojan
    Last edited by Bojan; 16th January 2006 at 23:56.
    The march of progress:
    C:
    printf("%10.2f", x);
    C++:
    cout << setw(10) << setprecision(2) << showpoint << x;
    Java:
    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance();
    formatter.setMinimumFractionDigits(2);
    formatter.setMaximumFractionDigits(2);
    String s = formatter.format(x);
    for (int i = s.length(); i < 10; i++) System.out.print(' ');
    System.out.print(s);

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    52
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4.1]How to make links open browsers?

    I haven't tried this, but since you have QProcess on Windows too, you should be able to launch a cmd.com (W2k / XP) or command.com (W95/98) with no UI and then execute a :

    start http://www.yoururltoopen.com

    That will launch the default browser on Windows since http is a protocol registered with the Windows web browser.
    This at least works in my Java app's.
    You can try this yourself if you open a Windows command window (shell) and enter:

    start http://www.trolltech.com

  4. #4
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4.1]How to make links open browsers?

    I think the question, is:

    it's possible to use QTextBrowser as a very minimal local html browser. The links to local media is implemented by the trolls, but external links must be handled via external applications.

    how do i distingish between local and remote (http , ftp) links clicks?

    (lazy n00b, loot at how it's done in assistant...)

  5. #5
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4.1]How to make links open browsers?

    I haven't tried this, but since you have QProcess on Windows too, you should be able to launch a cmd.com (W2k / XP) or command.com (W95/98) with no UI and then execute a :
    I don't know in Qt4, haven't tried it in Windows yet, but in Qt3, you can't launch cmd.exe or command.com with QProcess.
    I don't know why, it doesn't raise any error, but just doesn't work.


    Cheers.
    Kandalf
    There's no place like ~

  6. #6
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4.1]How to make links open browsers?

    Quote Originally Posted by Mike
    I haven't tried this, but since you have QProcess on Windows too, you should be able to launch a cmd.com (W2k / XP) or command.com (W95/98) with no UI and then execute a :

    start http://www.yoururltoopen.com

    That will launch the default browser on Windows since http is a protocol registered with the Windows web browser.
    This at least works in my Java app's.
    You can try this yourself if you open a Windows command window (shell) and enter:

    start http://www.trolltech.com
    That's exactly what the ShellExecute() call in the code posted above does. No need to launch a command shell. The real issue is finding an elegant way of handling the problem on UNIX/Linux.

  7. #7
    Join Date
    Mar 2006
    Posts
    21
    Qt products
    Qt3
    Platforms
    Unix/X11

    Exclamation Re: [Qt4.1]How to make links open browsers?

    hi am also new to qt and i am trying to find the QTextbrowser source code ,so plese mail me the link from where i can get the QTextbrowser and its source code .email is awalesminfo@gmail.com

  8. #8
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4.1]How to make links open browsers?

    Quote Originally Posted by kandalf
    I don't know in Qt4, haven't tried it in Windows yet, but in Qt3, you can't launch cmd.exe or command.com with QProcess.
    I don't know why, it doesn't raise any error, but just doesn't work.


    Cheers.
    Qt Code:
    1. // open NEAppRoster.html
    2. QString strNARfile = this->ReportPath;
    3. strNARfile.append("NEAppRoster.html");
    4. QFile file(strNARfile );
    5. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
    6. ui.leStatus->setText("Can't open n458.txt file for output");
    7. return;
    8. }
    9. ui.leStatus->setText("Stand by ....processing...");
    10. QTextStream out(&file);
    11. out << fheader << endl
    12. out << this->HTML_Header1 ;
    13. out << "Nebraska Exemption Application Roster " << this->dbYear << endl ;
    14. out << this->HTML_Header2 << endl;
    15. // ... snip some code which streams rows of data into "out" in html form ...
    16. out << this->HTML_Footer << "\n";
    17. file.close();
    18. QStringList sRpt; // required argument for QProcess execute
    19. //prepend "file:" to filename so IE or Konqueror knows what it is
    20. sRpt << strNARfile.prepend("file:");
    21. //assume we are running on Windows. Probably will be long after I retire
    22. QString sIE = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";
    23. QProcess webbrowser;
    24. int dummy = webbrowser.startDetached(sIE, sRpt); // a simple string can't replace sRpt
    25. dummy = 0; // to avoid compiler warning about assigned but unused var
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [Qt4.1]How to make links open browsers?

    Quote Originally Posted by GreyGeek
    Qt Code:
    1. ...
    2. //assume we are running on Windows. Probably will be long after I retire
    3. QString sIE = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";
    4. QProcess webbrowser;
    5. int dummy = webbrowser.startDetached(sIE, sRpt); // a simple string can't replace sRpt
    6. dummy = 0; // to avoid compiler warning about assigned but unused var
    To copy to clipboard, switch view to plain text mode 

    This is not such a good idea in case the user has a non-default installation of Windows (my programs exist on my D: drive). Also if the users default browser is something other than IE, for example Firefox, this could get quite annoying for them.

    Again, probably the best solution (forWindows) is:

    Qt Code:
    1. // parent = main window of you application
    2. // rUrl = string containing url to open in browser
    3. #include <shellapi.h>
    4. #ifdef Q_WS_WIN
    5. bool result = int(ShellExecuteW(parent->winId(),
    6. 0,
    7. rUrl.ucs2(),
    8. 0,
    9. 0,
    10. SW_SHOWNORMAL)) > 32;
    11. #endif
    To copy to clipboard, switch view to plain text mode 

    It opens the specified URL in the default web browser.
    Save yourself some pain. Learn C++ before learning Qt.

Similar Threads

  1. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57

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.