综合网站推广的含义,定制网站+域名+企业邮箱,台州网站建设优化案例,wordpress页面大小调节PySide6 的样式表#xff08;Qt Style Sheets#xff0c;QSS#xff09;是模仿 CSS 语法的界面美化机制#xff0c;用于统一控制 PySide6 控件的外观#xff0c;从基础的颜色、字体到复杂的控件状态、自定义控件样式都能覆盖。一、QSS 基础核心1.1 基本语法结构QSS 语法与…PySide6 的样式表Qt Style SheetsQSS是模仿 CSS 语法的界面美化机制用于统一控制 PySide6 控件的外观从基础的颜色、字体到复杂的控件状态、自定义控件样式都能覆盖。一、QSS 基础核心1.1 基本语法结构QSS 语法与 CSS 高度相似核心结构为选择器 { 属性: 值; }多个属性用分号分隔注释用/* 注释内容 */。from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout app QApplication([]) window QWidget() window.setWindowTitle(QSS基础示例) # 直接给控件设置QSS btn1 QPushButton(按钮1) btn2 QPushButton(按钮2) layout QVBoxLayout(window) layout.addWidget(btn1) layout.addWidget(btn2) btn1.setStyleSheet( QPushButton { background-color: #4CAF50; /* 背景色 */ color: white; /* 文字色 */ font-size: 14px; /* 字体大小 */ padding: 8px 16px; /* 内边距上下 左右 */ border: none; /* 无边框 */ border-radius: 4px; /* 圆角 */ } QPushButton:hover { background-color: #3e8e41; /* 鼠标悬停时背景色 */ } QPushButton:pressed { background-color: #297937; /* 鼠标按下时背景色 */ } ) window.resize(300, 200) window.show() app.exec()按钮1是设置了样式表的按钮2是没有设置样式表。1.2 核心选择器在上例中为按钮1单独设置了样式表如果有很多相同的按钮为每个按钮单独设置样式表就很繁琐所以就要用到选择器来选择样式表的设置范围。代码from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout app QApplication([]) window QWidget() window.setWindowTitle(QSS基础示例) # 直接给控件设置QSS btn1 QPushButton(按钮1) btn2 QPushButton(按钮2) layout QVBoxLayout(window) layout.addWidget(btn1) layout.addWidget(btn2) window.setStyleSheet( QMainWindow { /*设置主窗口*/ background-color: #f0f0f0; } QPushButton { /*设置窗口下的所有按钮*/ background-color: #2196F3; color: white; border-radius: 5px; padding: 10px; } QPushButton:hover { /*设置窗口下的所有按钮鼠标覆盖*/ background-color: #1860a0; } QPushButton:pressed { /*设置窗口下的所有按钮鼠标点击*/ background-color: #123654; } ) window.resize(300, 200) window.show() app.exec()在上面的代码中在窗口中通过类选择器的方法为所有按钮类定义了样式QPushButton { /*设置窗口下的所有按钮*/ background-color: #2196F3; color: white; border-radius: 5px; padding: 10px; }又通过伪状态选择器定义了按钮的悬浮和点击样式QPushButton:hover { /*设置窗口下的所有按钮鼠标覆盖*/ background-color: #1860a0; } QPushButton:pressed { /*设置窗口下的所有按钮鼠标点击*/ background-color: #123654; }除了类选择器和伪状态选择器QSS 还支持以下常用选择器优先级从高到低选择器类型示例说明类选择器QPushButton匹配所有 QPushButton 及其子类控件ID 选择器QPushButton#myBtn匹配 objectName 为 myBtn 的 QPushButton属性选择器QPushButton[flattrue]匹配 flat 属性为 true 的 QPushButton后代选择器QWidget QPushButton匹配 QWidget 下所有后代 QPushButton子选择器QWidget QPushButton匹配 QWidget 直接子级的 QPushButton伪状态选择器QPushButton:hover匹配鼠标悬浮状态的 QPushButton示例多选择器组合import sys from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout app QApplication([]) window QWidget() window.setWindowTitle(QSS基础示例) # 直接给控件设置QSS btn1 QPushButton(按钮1) btn2 QPushButton(按钮2) btn3 QPushButton(按钮3) btn2.setObjectName(btn2) # 设置对象名称 btn3.setFlat(True) # 设置flat属性 layout QVBoxLayout(window) layout.addWidget(btn1) layout.addWidget(btn2) layout.addWidget(btn3) window.setStyleSheet( QMainWindow { /*设置主窗口*/ background-color: #f0f0f0; } QPushButton { /*设置窗口下的所有按钮*/ background-color: #2196F3; color: white; border-radius: 5px; padding: 10px; } QPushButton:hover { /*设置窗口下的所有按钮鼠标覆盖*/ background-color: #1860a0; } QPushButton:pressed { /*设置窗口下的所有按钮鼠标点击*/ background-color: #123654; } QPushButton#btn2 { /*设置对象名称为btn2的按钮*/ background-color: #FF9800; } QPushButton#btn2:hover { /*设置对象名称为btn2的按钮鼠标覆盖*/ background-color: #e67e00; } QPushButton#btn2:pressed { /*设置对象名称为btn2的按钮鼠标点击*/ background-color: #b25c00; } QPushButton[flattrue] { /*设置flat属性为true的按钮*/ background-color: #ee6600; } QPushButton[flattrue]:hover { /*设置flat属性为true的按钮鼠标覆盖*/ background-color: #aa4400; } QPushButton[flattrue]:pressed { /*设置flat属性为true的按钮鼠标点击*/ background-color: #883300; } ) window.resize(300, 200) window.show() sys.exit(app.exec())1.3 常用样式属性QSS 覆盖控件的视觉属性核心分类如下类别常用属性说明背景background-color/background-image背景色 / 背景图字体font-family/font-size/font-weight字体 / 大小 / 粗细边框border/border-radius/border-color边框 / 圆角 / 边框色内边距padding控件内容与边框的间距文字color/text-align/text-decoration文字色 / 对齐 / 下划线尺寸min-width/max-height最小宽度 / 最大高度二、QSS 高级用法2.1 状态伪选择器复杂交互QSS 支持控件的各种状态如悬浮、按下、选中、禁用等可组合实现复杂交互效果常用伪状态伪状态说明适用控件:hover鼠标悬浮所有可交互控件:pressed鼠标按下未释放按钮、复选框等:checked选中状态复选框、单选框、切换按钮:disabled禁用状态所有控件:focus获取焦点输入框、按钮等:indeterminate半选状态复选框、进度条:on/:off开关状态QCheckBox、QRadioButton示例自定义的复选框样式import sys from PySide6.QtWidgets import QApplication, QWidget, QCheckBox, QVBoxLayout app QApplication([]) window QWidget() window.setStyleSheet( /* 1. 基础复选框样式控制文字与框体间距 */ QCheckBox { font-size: 20px; spacing: 10px; /* 框体和文字的间距避免重叠 */ } /* 2. 复选框框体必选设置宽/高 */ QCheckBox::indicator { width: 24px; /* 宽度 */ height: 24px; /* 高度 */ border: 2px solid #2196F3; /* 未选中边框 */ border-radius: 4px; /* 圆角可选 */ background-color: white; /* 未选中背景 */ } /* 3. 选中态样式 */ QCheckBox::indicator:checked { background-color: #2196F3; } ) checkbox QCheckBox(自定义复选框) layout QVBoxLayout(window) layout.addWidget(checkbox) window.resize(300, 100) window.show() sys.exit(app.exec())2.2 子控件选择器精细化样式对于复合控件如 QComboBox、QScrollBar、QSpinBox需通过::子控件名定位内部子元素实现精细化样式控制。常用子控件复合控件子控件名说明QComboBox::drop-down下拉箭头按钮::down-arrow下拉箭头图标QScrollBar::handle滚动条滑块::add-page/::sub-page滚动条翻页区域::up-arrow/::down-arrow上下箭头QSpinBox::up-button/::down-button上下调节按钮QSlider::handle滑块QTabWidget::tab标签页QProgressBar::chunk进度条已完成部分示例 1自定义进度条import sys from PySide6.QtWidgets import QTextEdit, QApplication, QWidget, QVBoxLayout app QApplication(sys.argv) window QWidget() from PySide6.QtWidgets import QProgressBar from PySide6.QtCore import QTimer progress QProgressBar() progress.setRange(0, 100) progress.setValue(50) progress.setStyleSheet( QProgressBar { height: 8px; border-radius: 4px; background-color: #e0e0e0; text-align: center; /* 文字居中 */ color: transparent; /* 隐藏文字 */ } /* 进度条进度块 */ QProgressBar::chunk { border-radius: 4px; /* 渐变效果 */ background: qlineargradient(x1:0, y1:0, x2:1, y2:0, stop:0 #4CAF50, stop:1 #285825); } ) # 模拟进度变化 timer QTimer() timer.timeout.connect(lambda: progress.setValue(progress.value() 1 if progress.value() 100 else 0)) timer.start(100) layout QVBoxLayout(window) layout.addWidget(progress) window.show() sys.exit(app.exec())2.3 动态样式切换运行时修改通过代码动态修改 QSS实现主题切换、状态变更等效果核心方法控件setStyleSheet()直接覆盖控件样式全局样式表QApplication.setStyleSheet()样式缓存预定义多个样式字符串按需切换示例明暗主题切换import sys from PySide6.QtWidgets import QPushButton, QHBoxLayout, QApplication, QWidget, QVBoxLayout # 预定义主题 LIGHT_THEME QWidget { background-color: #ffffff; color: #333333; } QPushButton { background-color: #2196F3; color: white; border-radius: 4px; padding: 8px; } QPushButton:hover { background-color: #1976D2; } DARK_THEME QWidget { background-color: #212121; color: #ffffff; } QPushButton { background-color: #616161; color: white; border-radius: 4px; padding: 8px; } QPushButton:hover { background-color: #757575; } # 主题切换按钮 def switch_theme(): current app.styleSheet() if current LIGHT_THEME: app.setStyleSheet(DARK_THEME) else: app.setStyleSheet(LIGHT_THEME) # 初始化浅色主题 app QApplication(sys.argv) window QWidget() layout QVBoxLayout(window) theme_btn QPushButton(切换主题) theme_btn.clicked.connect(switch_theme) layout.addWidget(theme_btn) window.show() app.setStyleSheet(LIGHT_THEME) sys.exit(app.exec())2.4 样式表继承与优先级QSS 的优先级规则高→低控件直接设置的样式widget.setStyleSheet() 父控件 / 全局样式ID 选择器 类选择器 通用选择器伪状态 / 子控件选择器 基础选择器后定义的样式覆盖先定义的同优先级样式示例优先级演示# 全局样式低优先级 app.setStyleSheet(QPushButton { background-color: gray; }) # 控件样式高优先级 btn QPushButton(优先级测试) # ID选择器更高优先级 btn.setObjectName(priorityBtn) btn.setStyleSheet( QPushButton#priorityBtn { background-color: red; } /* 最终生效 */ QPushButton { background-color: blue; } )2.5 自定义控件的 QSS 支持对于自定义控件继承 QWidget默认无法直接应用 QSS 背景等样式需重写paintEvent或设置setAttribute(Qt.WA_StyledBackground, True)。示例支持 QSS 的自定义控件import sys from PySide6.QtCore import Qt from PySide6.QtWidgets import QApplication from PySide6.QtWidgets import QWidget class CustomWidget(QWidget): def __init__(self, parentNone): super().__init__(parent) # 启用样式背景关键 self.setAttribute(Qt.WA_StyledBackground, True) # 启用样式背景必需的 app QApplication(sys.argv) window CustomWidget() window.setStyleSheet( CustomWidget { background-color: #ffeb3b; border: 2px solid #fbc02d; border-radius: 8px; padding: 16px; } CustomWidget:hover { background-color: #fff59d; } ) window.resize(300, 200) window.show() sys.exit(app.exec())2.6 QSS 变量与动态值进阶QSS 本身不支持变量但可通过字符串格式化实现 “伪变量”结合动态参数生成样式import sys from PySide6.QtWidgets import QApplication, QPushButton, QVBoxLayout from PySide6.QtWidgets import QWidget def get_button_style(bg_color, hover_color, radius): return f QPushButton {{ background-color: {bg_color}; border-radius: {radius}px; padding: 8px 16px; color: white; }} QPushButton:hover {{ background-color: {hover_color}; }} app QApplication(sys.argv) window QWidget() layout QVBoxLayout(window) # 动态生成样式 btn_red QPushButton(红色按钮) btn_red.setStyleSheet(get_button_style(#f44336, #d32f2f, 4)) btn_blue QPushButton(蓝色按钮) btn_blue.setStyleSheet(get_button_style(#2196F3, #1976D2, 8)) layout.addWidget(btn_red) layout.addWidget(btn_blue) window.resize(300, 200) window.show() sys.exit(app.exec())三、QSS 最佳实践3.1 样式分离将 QSS 写入单独的.qss文件通过读取文件加载便于维护# 加载外部QSS文件 def load_style_sheet(file_path): with open(file_path, r, encodingutf-8) as f: return f.read() # 应用样式 app.setStyleSheet(load_style_sheet(style.qss))3.2 避免过度样式化优先使用系统原生样式作为基础仅定制关键部分避免为每个控件单独写样式使用类选择器统一控制复杂交互优先用代码逻辑而非纯 QSS3.3 调试技巧使用Qt Style Sheet DebuggerQt Creator 内置调试 QSS逐步添加样式定位冲突的属性四、总结PySide6 的 QSS 是界面美化的核心工具其核心能力包括基础样式控制颜色、字体、边框状态与子控件的精细化样式运行时动态切换样式自定义控件的样式适配高级用法的关键在于掌握选择器优先级、子控件定位、状态伪选择器并结合 Python 代码实现动态样式逻辑。合理使用 QSS 可大幅提升界面美观度同时保持代码的可维护性。