Qt 4.8.5 QLabel setStylesheet 忽略继承的字体 [英] Qt 4.8.5 QLabel setStylesheet ignores inherited font

查看:46
本文介绍了Qt 4.8.5 QLabel setStylesheet 忽略继承的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 setStylesheet 设置一些样式属性,例如一个边框

I want to set some style properties via setStylesheet, e.g. a border

label.setStylesheet("border: 1px solid white;");

之后我的标签有一个白色边框,但在父小部件 (QDesigner) 中设置的所有字体属性都被忽略!

After that my label has a white border but all font properties set in the parent widget (QDesigner) are ignored!

qDebug() << label->font().family();
qDebug() << label->font().rawName();

两者都打印了正确的字体系列,但在调用 setStylesheet 函数后不会应用.

both print the right font family but this is not applied after the setStylesheet function was called.

颜色也一样.如果我通过 setStylesheet() 设置了一些其他属性,则不会使用在设计器中通过 QPlatte 设置的颜色.

Same thing with colors. Colors set via QPlatte in the Designer are not used if I set some other properties via setStylesheet().

我不知道,但似乎我们不应该混合使用这两种技术,否则我在这里做错了.

I don't know but it seems that we should not mix both technologies or I am doing something wrong here.

推荐答案

不幸的是,在小部件的样式表中设置一个属性通常会导致需要设置所有样式属性,并且会破坏任何这些属性的继承.我无法在自己的环境中重现字体继承问题(您使用的是什么版本的 Qt?),但以下代码应该可以解决此问题.

Unfortunately, setting one property in the style sheet of a widget usually results in all styles properties needing to be set, as well as breaking inheritance for any of those properties. I couldn't reproduce the font inheritance issue in my own environment (what version of Qt are you using?), but the following code should get you around this issue.

//  Get the color of the label's parent (this).
QColor color = this->palette().windowText().color();
QString colorString = "rgb(" +
                      QString::number( color.red() ) + "," +
                      QString::number( color.green() ) + "," +
                      QString::number( color.blue() ) + ")";

//  Get the Font of the label's parent (this).
QFont font = this->font();

//  Set the Font and Color.
label->setFont( font );
label->setStyleSheet( "color: " + colorString + "; border: 1px solid white;" );

就我个人而言,我尝试将我的所有样式保留在针对特定表单对象样式的表单编辑器中,或者保留在顶层加载的样式表中,就像网页的 CSS 一样.

Personally, I try to keep all of my styling in either the form editor for specific form object styles, or in a style sheet that is loaded in at the top level, much like CSS for a web page.

这篇关于Qt 4.8.5 QLabel setStylesheet 忽略继承的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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