跳转至

样式表

QApplication::setStyleSheet() 全局设置 QWidget::setStyleSheet() 局部设置

选择器 { 声明; }

1 声明

所有声明:stylesheet-reference

2 选择器

2.1 选择器类型

选择器类型: - 通用选择器 * - 类别选择器 QPushButton - 属性选择器 [] - 类别选择器 .QPushButton 不匹配其子类的实例 - ID选择器 # - 子对象选择器 > 子控件 :: down-arrow 伪状态 : hover、!hover

2.2 冲突

设计器里测试吧。

2.3 样式级联

小部件的最终样式通过级联所有父类样式得到。自身样式 优先于 父类样式 优先于 祖父类样式...

2.4 继承

字体和颜色设置默认不会继承,需要设置 AA_UseStyleSheetPropagationInWidgetStyles 后才启用继承。

QCoreApplication::setAttribute(Qt::AA_UseStyleSheetPropagationInWidgetStyles, true);

2.5 命名空间

namespace ns {
    class MyPushButton : public QPushButton {
    }
}
qApp->setStyleSheet("ns--MyPushButton { background: yellow; }");