Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I customise the appearance of links in QLabels using style sheets?

I have a QLabel with a Qt stylesheet that sets a dark background:

QLabel {
background: black;
color: white;
}

This works fine until I add text with an embedded URL and set the Qt::TextFormat to Qt::RichText. The link displays as the default dark blue, which is hard to read on a dark background.

I've tried customising it via a stylesheet such as:

a { color: white; }
QLabel!visited { color: white; }

but this doesn't have any effect. The one thing that does seem to work is changing the application's QPalette:

QPalette newPal(qApp->palette());
newPal.setColor(QPalette::Link, Qt::white);
newPal.setColor(QPalette::LinkVisited, Qt::white);
qApp->setPalette(newPal);

However this requires the colour to be hardcoded. Is there any way I can set the colour from a stylesheet instead?

EDIT:

I've discovered a further problem with customising the palette. If I want to just modify the palette of my widget (substituting widget for qApp in the sample above) then this doesn't work. I don't want to affect all the other QLabels in the app, so how do I limit the palette changes to this widget?

like image 974
the_mandrill Avatar asked Mar 31 '11 09:03

the_mandrill


People also ask

What is the appearance of an unvisited link?

An unvisited link is underlined and blue. A visited link is underlined and purple. An active link is underlined and red.

How to make link black in CSS?

You need to add some CSS styling using CSS pseudo-classes. Here is an example showing CSS added to the head element in a style tag that changes all links to always be black.


1 Answers

One way is to add style="color: whatever" or a class to the inner <span> of the link. I haven't figured out yet how to apply this to the whole application but it's a good start.

like image 180
cen Avatar answered Dec 28 '22 06:12

cen