View Full Version : Html link color

17th February 2006, 18:59
I am a new user of QT. I want too give color to HTML links present in QTextBrowser in QT4.1. The color will change dynamically.


18th February 2006, 16:44
I want to change the color of the link used in HTML in QTextBrowser. The default color is blue. Some of the links in my page will have blue, and others will be gray.

<a href="one.html">one</a> -- blue color
<a href="two.html">tow</a> -- gray color

I tried to use the <font color=gray> for surrounding the link tag or the text in the link(one or two) but that doen't work.

I will be thankful if somebody provides me the solution.

Thanks in advance,


18th February 2006, 16:51
Please, don't double post.

Have you tried: <a style="text-color: gray" href="two.html">tow</a>

18th February 2006, 17:13
Thanks for the reply,
I tried your suggestion, but it did not work.


18th February 2006, 17:43
Try <font color="gray"> or even <font color="#808080">. Qt HTML support recognizes both named colors and RGB values. I really doubt Qt's HTML support extends to the full W3 specification for XHTML, but it's quite possible they at least borrowed the requirement from XHTML that attributes have to always be quoted, in which case just writing <font color=gray> won't work.

18th February 2006, 17:52
It looks like QTextBrowser tries to be smarter than the programmer:
#include <QApplication>
#include <QTextBrowser>
#include <QtDebug>

int main( int argc, char **argv )
QApplication app( argc, argv );

QTextBrowser browser;
browser.setHtml( "<html><body><a href=\"xxx.html\" style=\"text-color:red\">XXX</a></body></html>" );
qDebug() << browser.toHtml();

return app.exec();
Output:<html><head>...<a href="xxx.html"><span style=" text-decoration: underline; color:#0000ff;">XXX</span></a></p></body></html>Note the <span> tag.

18th February 2006, 19:08
That's terrible. I tried about ten different things to get it to display and the results ranged from identical to yours to Qt replacing my <a...</a> link with a "qt-paragraph-type: empty" CSS directive. I even made the HTML file external, used EXACTLY the same markup that toHTML produces except changed the color in <span> and it still changes it.

Becoming obsessed with this weirdness, I just spent the past twenty minutes searching the Qt-interest Archive. Two things struck me as funny:

1) There's about ten questions so far (but I have only reached as far back as September 2005) asking how to avoid getting your HTML mangled by TextBrowser. Not a single one of them is replied to.
2) There's a quote from 05.09.2005 which says:

In any case: Qt only supports very limited HTML, I guess it's roughly
HTML 3.2 with some style sheet support for fonts, nothing more! This is
enough to display simple help pages with tables, images, some text
layout, hyperlinks... that's it!

To sreedhar: I guess if you want to do something as vastly complex as changing the color of a link, you'll have to create a basic richedit document and parse all of the HTML yourself.

19th February 2006, 16:40
Thanks a lot for the reply. If i use <font color="gray"> in QT 4.0 it works fine and shows my links in gray, but if i shift to QT 4.1 and the links are displayed in blue.

So, it seems that there is no simple way to get around this. It seems to be a bug in QT4.1 as the parsing of HTML to change color of links works in QT4.0 and QT3.2.


28th February 2006, 19:42
The bug is removed in QT 4.1.1. the attribute in style is color and not text-color


5th September 2008, 15:36
Click <a href=\"file.pdf\" style=\"color:#33FFFF\">here</a> for PDF file.

This works for QString on QLabel, fyi. Qt 4.2.3.

"text-color" didn't work; just "color" does.