• QSS 选择器


    1. # 1.通配符选择器:匹配所有控件
    2. '''
    3. *{ // 用*号匹配所有控件
    4. color:green;
    5. }
    6. '''
    7. # 2.类型选择器(通过控件类型来匹配控件包含子类)
    8. '''
    9. QPushButton{
    10. font-size:30px;
    11. }
    12. Btn{
    13. font-size:30px;
    14. }
    15. '''
    16. # 3.类选择器(通过控件类型匹配 不包括子类)
    17. '''
    18. .QPushButton{
    19. font-size: 30px;
    20. }
    21. '''
    22. # 4.ID选择器setObjectName #和字母之间不能有空格
    23. '''
    24. #btn{
    25. background-color:green;
    26. }
    27. '''
    28. # 5.属性选择器setProperty
    29. '''
    30. QPushButton[name="btn"]:hover{
    31. background-color:red;
    32. }
    33. // 表示只要加上setProperty的所有name属性按钮都是红色背景,不影响其他无样式按钮的新建
    34. QPushButton[name]:hover{
    35. background-color:red;
    36. }
    37. '''
    38. # 6.后代选择器(通过父控件直接或间接作用于子控件QPushButton)
    39. '''
    40. QWidget#box QPushButton{
    41. background-color:green;
    42. }
    43. '''
    44. # 7.子选择器(直接包含的子控件)
    45. '''
    46. QWidget#box>QLabel{
    47. background-color:green;
    48. }
    49. '''
    50. # 8.子控件选择器(主要用于组合控件中的一部分组件样式修改)
    51. # 8.1.常见伪状态
    52. '''
    53. :checked # 控件被选中
    54. :unchecked # 控件被取消选中
    55. :hover # 鼠标停留
    56. :pressed # 控件被按下
    57. :focus # 获取到焦点
    58. :disable # 失效控件
    59. :enable # 有效控件
    60. :indeterminate # checkBox或radioButton被部分选中
    61. :on # 开启状态
    62. :off # 关闭状态
    63. '''
    64. # 8.2.常用组合控件子控件
    65. '''
    66. QCheckBox, QRadioButton ::indicator
    67. QComboBox ::drop-down
    68. QSpinBox, QDateEdit, QTimeEdit, QDateTimeEdit ::up-button ::down-button ::up-arrow ::down-arrow
    69. QSlider ::groove ::handle ::add-page ::sub-page
    70. QProgressBar ::chunk
    71. QScrollBar ::sub-line, ::add-line ::sub-page, ::add-page ::up-arrow, ::down-arrow ::left-arrow, ::right-arrow
    72. QGroupBox ::title ::indicator
    73. QTableView ::item
    74. QHeaderView, QTableCornerButton ::section
    75. QTreeView ::item ::branch
    76. QHeaderView ::section
    77. QTabWidget QTabWidget::pane QTabWidget::tab-bar QTabBar::tab QTabBar::close-button QTabBar::tear QTabBar::scroller QTabBar QToolButton::left-arrow QTabBar QToolButton::right-arrow
    78. '''
    79. '''
    80. QCheckBox::indicator{
    81. width: 20px;
    82. height: 20px;
    83. }
    84. QCheckBox::indicator:checked{
    85. image: url(y.png);
    86. }
    87. QCheckBox::indicator:unchecked{
    88. image: url(n.png);
    89. }
    90. '''
    91. # 9.选择器的组合使用(使用逗号隔开)
    92. '''
    93. #aaa,#bbb{
    94. color: red;
    95. }
    96. '''
    1. from PyQt5.Qt import *
    2. import sys
    3. class Btn(QPushButton) :
    4. pass
    5. class Window(QWidget) :
    6. def __init__(self) :
    7. super().__init__()
    8. self.setWindowTitle("QSS选择器使用方法总结 - PyQt5中文网")
    9. self.resize(600, 500)
    10. self.func_list()
    11. def func_list(self) :
    12. self.func()
    13. def func(self) :
    14. box = QWidget(self)
    15. box.setObjectName('box')
    16. box1 = QWidget(box)
    17. box1.setObjectName('box')
    18. box1.resize(100, 60)
    19. box1.move(250, 200)
    20. box1.setStyleSheet('border:1px solid red')
    21. label1 = QLabel('标签1', box)
    22. label1.move(100, 200)
    23. label1.resize(100, 50)
    24. label1.setObjectName('aaa')
    25. label2 = QLabel('标签2', box1)
    26. label2.move(250, 200)
    27. label2.resize(100, 50)
    28. label2.setStyleSheet('color:red;')
    29. label3 = QLabel('标签3', self)
    30. label3.move(400, 200)
    31. label3.resize(100, 50)
    32. label3.setObjectName('bbb')
    33. btn1 = QPushButton('按钮1', box)
    34. btn1.move(100, 100)
    35. btn1.resize(100, 50)
    36. btn1.setProperty('name', 'btn1')
    37. btn2 = QPushButton('按钮2', self)
    38. btn2.move(250, 100)
    39. btn2.resize(100, 50)
    40. btn3 = QPushButton('按钮3', self)
    41. btn3.setProperty('name', 'btn')
    42. btn3.move(100, 300)
    43. btn3.resize(100, 50)
    44. btn4 = Btn('按钮4', self)
    45. btn4.setObjectName('btn')
    46. btn4.move(250, 300)
    47. btn4.resize(100, 50)
    48. print(box1.children())
    49. ck = QCheckBox('选择正确答案', self)
    50. ck.move(150, 400)
    51. ck.resize(100, 40)
    52. sb = QSpinBox(self)
    53. sb.move(300, 400)
    54. sb.resize(100, 40)
    55. pass
    56. if __name__ == '__main__' :
    57. app = QApplication(sys.argv)
    58. window = Window()
    59. with open('qss11_2.qss', 'r', encoding='UTF-8') as f :
    60. qApp.setStyleSheet(f.read())
    61. window.show()
    62. sys.exit(app.exec_())
    1. /*qss 代码*/
    2. /*
    3. *{
    4. color:green;
    5. }
    6. QPushButton{
    7. font-size:30px;
    8. }
    9. Btn{
    10. font-size:30px;
    11. }
    12. .QPushButton{
    13. font-size: 30px;
    14. }
    15. #btn{
    16. background-color:green;
    17. }
    18. QPushButton[name]:hover{
    19. background-color:red;
    20. }
    21. QWidget#box QPushButton{
    22. background-color:green;
    23. }
    24. QWidget#box>QLabel{
    25. background-color:green;
    26. }
    27. QCheckBox::indicator{
    28. width: 20px;
    29. height: 20px;
    30. background-color:green;
    31. }
    32. QCheckBox::indicator:checked{
    33. image: url(y.png);
    34. }
    35. QCheckBox::indicator:unchecked{
    36. image: url(n.png);
    37. }
    38. #aaa,#bbb{
    39. color: red;
    40. }
    41. */

  • 相关阅读:
    电源硬件设计----线性调压器与LDO
    技术分享 | SpringBoot 流式输出时,正常输出后为何突然报错?
    Azure Neural TTS 持续上新,助力企业开拓小语种市场
    Netty
    Python数据分析-5
    python+nodejs+vue教材征订管理系统
    Cesium源码解析一(terrain文件的加载过程)
    【vue导入导出Excel】vue简单实现导出和导入复杂表头excel表格功能【纯前端版本和配合后端版本】
    分享在Windows操作系统中独立安装微软MS Access 2019数据库的实用方法
    【Unity3D】UI Toolkit数据动态绑定
  • 原文地址:https://blog.csdn.net/weixin_42118352/article/details/126135971