スタイルシートを使用してQlabelsのリンクの外観をカスタマイズする方法
-
14-11-2019 - |
質問
暗い背景を設定するQTスタイルシートを備えたQLabel
を持っています:
QLabel {
background: black;
color: white;
}
.
埋め込みURLでテキストを追加し、Qt::TextFormat
をQt::RichText
に設定するまで機能します。リンクはデフォルトのダークブルーとして表示されます。これは暗い背景に読みにくいです。
:のようなスタイルシートを介してそれをカスタマイズしようとしました
a { color: white; }
QLabel!visited { color: white; }
.
しかし、これには効果がありません。作業しているように見えるものは、アプリケーションのQPalette
を変更しています:
QPalette newPal(qApp->palette());
newPal.setColor(QPalette::Link, Qt::white);
newPal.setColor(QPalette::LinkVisited, Qt::white);
qApp->setPalette(newPal);
.
しかしこれは色がハードコードされることを必要とする。代わりにスタイルシートから色を設定できる方法はありますか?
編集:
パレットのカスタマイズに関するさらなる問題を発見しました。私が私のウィジェットのパレットを変更したいのなら(上のサンプルのwidget
のためにqApp
を代入する)場合、これは機能しません。アプリ内の他のすべてのQLabels
に影響を与えたくないので、このウィジェットにパレットの変更を制限する方法は?
解決
Short answer is no. Recently I had to do this.
QLabel!visited
doesn't work because Qt doesn't track whether QLabel were visited or not.QLabel { color: ... }
doesn't work for links. Can't find why but all I found is a suggestion to useQPallete
in this case.
他のヒント
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);
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.