前言
Qt Style Sheets (QSS) 支持许多与 CSS 类似的样式设置,用于自定义 Qt 控件的外观。
QSS样式
以下是 QSS 支持的一些主要样式设置及其属性:
1. 文本样式
color: 文本颜色font-family: 字体系列font-size: 字体大小font-style: 字体样式(如italic)font-weight: 字体粗细(如bold)text-align: 文本对齐(如left,center,right)text-decoration: 文本装饰(如underline,overline,line-through)
2. 背景样式
background-color: 背景颜色background-image: 背景图片background-repeat: 背景图片的重复方式(如repeat,repeat-x,repeat-y,no-repeat)background-position: 背景图片的位置(如top left,center center)background-clip: 背景裁剪区域(如border-box,padding-box,content-box)background-origin: 背景起始位置(如border-box,padding-box,content-box)
3. 边框样式
border: 边框(可以合并border-width,border-style,border-color)border-width: 边框宽度border-style: 边框样式(如solid,dashed,dotted)border-color: 边框颜色border-radius: 边框圆角
4. 尺寸和布局
width: 宽度height: 高度min-width: 最小宽度min-height: 最小高度max-width: 最大宽度max-height: 最大高度padding: 内边距margin: 外边距
5. 边距和填充
padding-top: 上内边距padding-right: 右内边距padding-bottom: 下内边距padding-left: 左内边距margin-top: 上外边距margin-right: 右外边距margin-bottom: 下外边距margin-left: 左外边距
6. 定位
position: 定位方式(如absolute,relative,fixed)top: 顶部定位right: 右侧定位bottom: 底部定位left: 左侧定位
7. 阴影
box-shadow: 盒子阴影
8. 透明度
opacity: 透明度(0到1之间的值)
9. 过渡和动画
transition: 过渡效果animation: 动画效果
10. 伪状态
:hover: 鼠标悬停:pressed: 控件被按下:checked: 控件被选中(如复选框或单选按钮):focus: 控件获得焦点:disabled: 控件被禁用:enabled: 控件被启用
示例
以下是一个 QSS 示例,展示了如何使用上述样式设置来自定义一个按钮:
1 | QPushButton { |
通过这些样式设置,你可以灵活地自定义 Qt 控件的外观,以满足应用程序的设计需求。
样式文件
定义样式文件 qss/common.qss
1 | /* 全局样式 */ |
引用前生成Python文件
1 | import subprocess |
代码中使用
1 | # This Python file uses the following encoding: utf-8 |