PDA

View Full Version : [Qt4.1]How to make links open browsers?



patcito
17th January 2006, 00:17
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

Bojan
17th January 2006, 00:52
There is no cross-platform way of doing this. You could try QDS - Q Desktop Services (http://qds.berlios.de), specifically the Launcher service.
Or something like the following code from the old forums:


bool open_browser(QWidget* parent, const QString& rUrl)
{
bool result = false;
QApplication::setOverrideCursor(Qt::BusyCursor);
#ifdef Q_WS_WIN
result = int(ShellExecuteW(parent->winId(),
0,
rUrl.ucs2(),
0,
0,
SW_SHOWNORMAL)) > 32;
#else
Q_UNUSED(parent);
// Try a range of browsers available on UNIX, until we (hopefully)
// find one that works. Start with the most popular first.

QProcess process;
bool process_started = false;
process.setArguments(QStringList() << "netscape" << rUrl);

process_started = process.start();

if (!process_started)
{
process.setArguments(QStringList() << "mozilla" << rUrl);

process_started = process.start();
}
if (!process_started)
{
process.setArguments(QStringList() << "firefox" << rUrl);

process_started = process.start();
}
if (!process_started)
{
process.setArguments(QStringList() << "konqueror" << rUrl);

process_started = process.start();
}
result = process_started;
#endif
QApplication::restoreOverrideCursor();

return result;
}

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

Mike
17th January 2006, 09:14
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

elcuco
17th January 2006, 23:21
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...):mad: :mad: :mad:

kandalf
18th January 2006, 04:42
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.

Chicken Blood Machine
18th January 2006, 07:21
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.

awalesminfo
16th March 2006, 07:04
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:confused: :confused:

GreyGeek
16th March 2006, 19:16
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.


// open NEAppRoster.html
QString strNARfile = this->ReportPath;
strNARfile.append("NEAppRoster.html");
QFile file(strNARfile );
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
ui.leStatus->setText("Can't open n458.txt file for output");
return;
}
ui.leStatus->setText("Stand by ....processing...");
QTextStream out(&file);
out << fheader << endl
out << this->HTML_Header1 ;
out << "Nebraska Exemption Application Roster " << this->dbYear << endl ;
out << this->HTML_Header2 << endl;
// ... snip some code which streams rows of data into "out" in html form ...
out << this->HTML_Footer << "\n";
file.close();
QStringList sRpt; // required argument for QProcess execute
//prepend "file:" to filename so IE or Konqueror knows what it is
sRpt << strNARfile.prepend("file:");
//assume we are running on Windows. Probably will be long after I retire
QString sIE = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";
QProcess webbrowser;
int dummy = webbrowser.startDetached(sIE, sRpt); // a simple string can't replace sRpt
dummy = 0; // to avoid compiler warning about assigned but unused var

Chicken Blood Machine
16th March 2006, 19:37
...
//assume we are running on Windows. Probably will be long after I retire
QString sIE = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";
QProcess webbrowser;
int dummy = webbrowser.startDetached(sIE, sRpt); // a simple string can't replace sRpt
dummy = 0; // to avoid compiler warning about assigned but unused var



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:



// parent = main window of you application
// rUrl = string containing url to open in browser
#include <shellapi.h>
#ifdef Q_WS_WIN
bool result = int(ShellExecuteW(parent->winId(),
0,
rUrl.ucs2(),
0,
0,
SW_SHOWNORMAL)) > 32;
#endif


It opens the specified URL in the default web browser.