如何使用样式表自定义 QLabels 中链接的外观? [英] How do I customise the appearance of links in QLabels using style sheets?

查看:39
本文介绍了如何使用样式表自定义 QLabels 中链接的外观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有设置深色背景的 Qt 样式表的 QLabel:

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

QLabel {
background: black;
color: white;
}

这工作正常,直到我添加带有嵌入 URL 的文本并将 Qt::TextFormat 设置为 Qt::RichText.该链接显示为默认的深蓝色,在深色背景上很难阅读.

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; }

但这没有任何影响.似乎可行的一件事是更改应用程序的 QPalette:

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?

我发现了自定义调色板的另一个问题.如果我只想修改我的小部件的调色板(在上面的示例中用 widget 代替 qApp),那么这不起作用.我不想影响应用程序中的所有其他 QLabels,那么如何限制对这个小部件的调色板更改?

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?

推荐答案

简短的回答是否定的.最近我不得不这样做.

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

  1. QLabel!visited 不起作用,因为 Qt 不跟踪 QLabel 是否被访问过.
  2. QLabel { color: ... } 不适用于链接.找不到原因,但我发现的只是在这种情况下使用 QPallete 的建议.
  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.

这篇关于如何使用样式表自定义 QLabels 中链接的外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆