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

4

25

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?

Metsky answered 31/3, 2011 at 9:33 Comment(0)
B
5

Short answer is no. Recently I had to do this.

  1. QLabel!visited doesn't work because Qt doesn't track whether QLabel were visited or not.
  2. QLabel { color: ... } doesn't work for links. Can't find why but all I found is a suggestion to use QPallete in this case.
Brede answered 31/3, 2011 at 9:39 Comment(2)
I think I've come to the same conclusions - see my other commentMetsky
QLabel().setText('<a href="..link"><span style="color:white;">something</span>')Hilton
C
14

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.

Condiment answered 8/2, 2014 at 8:25 Comment(2)
This is the only actual answer. Note that the Qt designer seems to explicitly colour each link, which overrides any default in your global stylesheet.Lycurgus
A slight simplification here is to add the styling to the anchor tag directly: <a href="..." style="color: whatever">...</a>. Note that this answer isn't about stylesheets so it doesn't answer the original question... Useful though.Auriscope
M
7

I've had little success explicitly setting the QPalette -- it works if you set it for the entire application, but not if you set it in the widget. In the end though, the easiest thing for what I needed to do was use a QTextBrowser instead which supports a subset of HTML. I could then override the colour of links using a regular CSS stylesheet:

QTextBrowser browser;
// IMPORTANT! - set the stylesheet before the content
browser->document()->setDefaultStyleSheet("a {color: white; }");
browser->setText(html);
Metsky answered 1/4, 2011 at 8:1 Comment(0)
B
5

Short answer is no. Recently I had to do this.

  1. QLabel!visited doesn't work because Qt doesn't track whether QLabel were visited or not.
  2. QLabel { color: ... } doesn't work for links. Can't find why but all I found is a suggestion to use QPallete in this case.
Brede answered 31/3, 2011 at 9:39 Comment(2)
I think I've come to the same conclusions - see my other commentMetsky
QLabel().setText('<a href="..link"><span style="color:white;">something</span>')Hilton
E
0

You can set the color tag in the HTML to

{ color: inherit; } 
Every answered 20/6, 2020 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.