qss样式笔记(一)

前话

        Qt的qss样式详解与Demo。

 

可设置样式的窗口部件列表

QAbstractScrollArea(滚动区域:随视口滚动背景、固定背景)

窗口部件

样式表使用方式

QAbstractScrollArea

 

支持盒模式。

所有QAbstractScrollArea的派生类(QAbstractItemView

QColumnView、QHeaderView、QListView、QTableView、QTreeView、QGraphicsView、QMdiArea、

QPlainTextEdit、QScrollArea、QTextEdit、QTextBrowser、QListWidget and QUndoView、QTableWidget、QTreeWidget):

支持滚动背景,使用background-attachment属性,设置属性值为scroll(缺省);

支持固定背景,不与视口滚动,使用background-attachment属性,设置为属性值fixed

属性background-attachment

属性值类型:Attachment

属性值:{scroll | fixed}

示例:QTextEdit使用background滚动背景与固定背景

扩展:固定平铺拉伸满视口背景


 
 
  1. QTextEdit{
  2. border-image: url( E:/1.jpg);
  3. }

 

QCheckBox

窗口部件

样式表使用方式

QCheckBox

 

支持盒模式。

拥有子控件-指示器indicator,使用::indicator选择子控件。默认情况下,指示器被放置在小部件的内容矩形的左上角。支持间隔属性,指定检查指示符和文本之间的间距,使用spacing属性。

属性spacing*

属性值类型:Length

属性值:px: pixels(default)

        pt: the size of one point (i.e., 1/72 of an inch)

        em: the em width of the font (i.e., the width of 'M')

        ex: the ex width of the font (i.e., the height of 'x')

示例1:QCheckBox::indicator定制


 
 
  1. QCheckBox {
  2. spacing: 5px;
  3. }
  4. QCheckBox::indicator {
  5. width: 13px;
  6. height: 13px;
  7. }
  8. QCheckBox::indicator:unchecked {
  9. background-color: rgb( 60, 0, 0);
  10. }
  11. QCheckBox::indicator:unchecked:hover {
  12. background-color: rgb( 120, 0, 0);
  13. }
  14. QCheckBox::indicator:unchecked:pressed {
  15. background-color: rgb( 180, 0, 0);
  16. }
  17. QCheckBox::indicator:checked {
  18. background-color: rgb( 0, 60, 0);
  19. }
  20. QCheckBox::indicator:checked:hover {
  21. background-color: rgb( 0, 120, 0);
  22. }
  23. QCheckBox::indicator:checked:pressed {
  24. background-color: rgb( 0, 180, 0);
  25. }
  26. QCheckBox::indicator:indeterminate:hover {
  27. background-color: rgb( 0, 0, 255);
  28. }
  29. QCheckBox::indicator:indeterminate:pressed {
  30. background-color: rgb( 0, 0, 255, 100);
  31. }

示例2:勾选

     


 
 
  1. /* 勾选协议 */
  2. QCheckBox {
  3. color: black;
  4. spacing: 15px;
  5. font-size: 22px;
  6. }
  7. QCheckBox::indicator {
  8. width: 30px;
  9. height: 30px;
  10. }
  11. QCheckBox::indicator:unchecked {
  12. border-image: url( :/images/unchecked.png);
  13. }
  14. QCheckBox::indicator:checked {
  15. border-image: url( :/images/checked.png);
  16. }

indeterminate为半选中状态

 

QColumnView

窗口部件

样式表使用方式

QColumnView

可被image属性控制。

拥有子控件-arrow(箭头)指示符,通过::left-arrow::right-arrow控制。

 

QComboBox

窗口部件

样式表使用方式

QComboBox

外框是frame,该frame支持盒模式。

拥有子控件-下拉按钮,可通过::drop-down控制。默认情况下拉按钮被放置在小部件的填充矩形的右上角。默认情况下,剪头放在下拉按钮内容的矩形的中心。

示例1:QComboBox定制


 
 
  1. QComboBox {
  2. border: 1px solid gray; /* border: 宽度 线类型 颜色 */
  3. border-radius: 3px;
  4. padding: 1px 18px 1px 3px;
  5. min-width: 6em;
  6. }
  7. QComboBox:editable {
  8. background: white;
  9. }
  10. QComboBox:!editable, QComboBox::drop-down:editable {
  11. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  12. stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
  13. stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
  14. }
  15. /* QComboBox gets the "on" state when the popup is open */
  16. QComboBox:!editable:on, QComboBox::drop-down:editable:on {
  17. background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  18. stop: 0 #D3D3D3, stop: 0.4 #D8D8D8,
  19. stop: 0.5 #DDDDDD, stop: 1.0 #E1E1E1);
  20. }
  21. QComboBox:on { /* shift the text when the popup opens */
  22. padding-top: 3px;
  23. padding-left: 4px;
  24. }
  25. QComboBox::drop-down {
  26. subcontrol-origin: padding;
  27. subcontrol- position: top right;
  28. width: 15px;
  29. border-left-width: 1px;
  30. border-left-style: solid; /* just a single line */
  31. border-left-color: darkgray;
  32. border-top-right-radius: 3px; /* same radius as the QComboBox */
  33. border-bottom-right-radius: 3px;
  34. }
  35. QComboBox::down-arrow {
  36. /* image: url(E:/1.jpg);*/
  37. border-image: url( E:/1.jpg);
  38. }
  39. QComboBox::down-arrow:on { /* shift the arrow when popup is open */
  40. top: 1px;
  41. left: 1px;
  42. }

示例2:将下拉框的单项最小宽度和最小高度固定

注意:增加test1 test22.. 单项的高度(ui->comboBox->setView(new QListView(); 添加图片),

原来是如下:


 
 
  1. QComboBox QAbstractItemView::item {
  2. min-height: 110px;
  3. min-width: 40px;
  4. }

示例3:定制边框和按钮


 
 
  1. * {
  2. font: 20px "黑体";
  3. }
  4. QComboBox {
  5. border: 2 px solid rgba(0,0,0,255); /* border: 宽度 线类型 颜色 */
  6. border-radius: 3px;
  7. background: rgba( 255, 255, 255, 255);
  8. }
  9. QComboBox::down-arrow {
  10. border: none;
  11. background: rgba( 0, 0, 0, 0);
  12. image: url(:/images/arrow_down.png);
  13. }

 

示例4:改变文字颜色,背景颜色,下拉框颜色


 
 
  1. QWidget {
  2. color: rgb( 255, 255, 255);
  3. background-color: rgb( 0, 0, 0);
  4. }
  5. QComboBox {
  6. border: 2px solid rgba( 0, 0, 0, 255); /* border: 宽度 线类型 颜色 */
  7. border-radius: 3px;
  8. }

示例4:定制边框,未下拉和下拉之后的箭头定制(2021年05月12日补充)


 
 
  1. /* 下拉框箭头下面的底色区域 */
  2. QComboBox::drop-down {
  3. border-top-width: 0px;
  4. border-bottom-width: 0px;
  5. border-left-width: 0px;
  6. width: 32px;
  7. background-color: rgb( 121, 188, 178);
  8. }
  9. /* 下拉框箭头下面的底色上的箭头区域 */
  10. QComboBox::down-arrow {
  11. border-top-width: 0px;
  12. border-bottom-width: 0px;
  13. border-left-width: 0px;
  14. background-color: red;
  15. width: 32px;
  16. height: 32px;
  17. border-image: url( :/settingWidget/images/arrowRight.png);
  18. background-color: rgb( 121, 188, 178);
  19. padding: 0px 0px 0px 0px;
  20. }
  21. QComboBox::down-arrow:on {
  22. border-top-width: 0px;
  23. border-bottom-width: 0px;
  24. border-left-width: 0px;
  25. background-color: red;
  26. width: 32px;
  27. height: 32px;
  28. border-image: url( :/settingWidget/images/arrowDown.png);
  29. background-color: rgb( 121, 188, 178);
  30. padding: 0px 0px 0px 0px;
  31. }

QDateEdit(参照QSpinBox)

窗口部件

样式表使用方式

QDateEdit

查看QSpinBox

 

QDialog

窗口部件

样式表使用方式

QDialog

 

只支持backgroundbackground-clipbackground-origin三个属性。

警告:确保您定义自定义部件时定义了Q_OBJECT宏。

属性:background

属性值类型:Background

属性描述:设置背景的快捷属性符,相当于指定:background-color、background-image、background-repeat、background-posision。

 

QDialogButtonBox

窗口部件

样式表使用方式

QDialogButtonBox

按钮的布局可以使用button-layout属性。

属性:button-layout

属性值类型:Number

属性值:[0 (WinLayout) | 1 (MacLayout) | 2 (KdeLayout) | 3 (GnomeLayout)]

 

QDockWidget

窗口部件

样式表使用方式

QDockWidget

支持定制标题栏和标题栏按钮样式。

支持边框定制,使用border属性。

拥有子控件-标题栏,使用::title可控制标题栏。

标题栏有用子控件-关闭按钮和浮动按钮,使用::close-button::float-button控制。

当是垂直标题栏时,会被置为:vertical状态。

依赖于QDockWidget::DockWidgetFeature特性设置,会被选取为:closable:floatable:movable三种状态。

 

QDoubleSpinBox(查看QSpinBox)

窗口部件

样式表使用方式

QDoubleSpinBox

查看QSpinBox

 

QFrame

窗口部件

样式表使用方式

QFrame

支持盒模式。

4.3版本以后,当设置一个QLabel时会自动设置QFrame::frameStyle属性为QFrame::StylePanel。

示例:QFrame定制


 
 
  1. QFrame, QLabel, QToolTip{
  2. border: 2px solid green; /* border: 宽度 线类型 颜色 */
  3. border-radius: 4px;
  4. padding: 2px; /* 边界都内部矩形的宽度 */
  5. background-image: url( E:/1.jpg);
  6. }

 

QGroupBox

 

窗口部件

样式表使用方式

QGroupBox

支持盒模式;

拥有子控件-标题栏,使用::title可控制。默认情况下,标题栏文字”GroupBox”位置取决于QGroupBox::textAlignment。

在可选择情况下,标题栏拥有子控件-指示器, 使用::indicator定制控制。

属性spaceing控制text与indicator之间的距离。

示例1:QGroupBox定制


 
 
  1. QGroupBox {
  2. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  3. stop: 0 #E0E0E0,
  4. stop: 1 #FFFFFF);
  5. border: 2px solid gray; /* 右图border: 4px solid gray; */
  6. border-radius: 5px;
  7. margin-top: 1ex; /* 右图margin-top: 4ex; */
  8. }
  9. QGroupBox::title {
  10. subcontrol-origin: margin;
  11. subcontrol- position: top center; /* position at the top center */
  12. padding: 0 3px;
  13. background-color: qlineargradient( x1: 0, y1: 0, x2: 0, y2: 1,
  14. stop: 0 #FF0ECE,
  15. stop: 1 #FFFFFF);
  16. }

当在代码或者ui中将GroupBox设置为checkable=true时,如下:


 
 
  1. QGroupBox {
  2. background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
  3. stop: 0 #E0E0E0, stop: 1 #FFFFFF);
  4. border: 4px solid gray;
  5. border-radius: 5px;
  6. margin-top: 24px; /* leave space at the top for the title */
  7. }
  8. QGroupBox::title {
  9. subcontrol-origin: margin;
  10. subcontrol- position: top center; /* position at the top center */
  11. padding: 0 3px;
  12. background-color: qlineargradient( x1: 0, y1: 0, x2: 0, y2: 1,
  13. stop: 0 #FF0ECE,
  14. stop: 1 #FFFFFF);
  15. }
  16. QGroupBox::indicator {
  17. width: 24px;
  18. height: 24px;
  19. }
  20. QGroupBox::indicator:unchecked {
  21. image: url( E:/1.jpg);
  22. }

示例2:QGroupBox修改title


 
 
  1. QGroupBox::title {
  2. subcontrol-origin: margin;
  3. subcontrol-position: left top;
  4. font-size: 224px;
  5. font-weight: bold;
  6. }
  7. QGroupBox { /* 修改标题的字体样式,本身不支持windows font */
  8. font-size: 24px;
  9. font-weight: bold;
  10. }

QHeaderView

窗口部件

样式表使用方式

QHeaderView

支持盒模式。

拥有子控件-头标题格,使用::section来控制样式,:该子控件拥有:middle:fisrt,:last:only-one:next-selected

:previous-selected:selecttedchecked状态。

拥有子控件-标题单元格箭头,使用::up-arrow::down-arrow控制。

示例:QTableWidget的QHeaderView定制


 
 
  1. QHeaderView::section {
  2. background-color: qlineargradient( x1: 0, y1: 0, x2: 0, y2: 1,
  3. stop: 0 #616161,
  4. stop: 0.5 #505050,
  5. stop: 0.6 #434343, stop: 1 #656565);
  6. color: white;
  7. padding-left: 4px;
  8. border: 1px solid #6c6c6c;
  9. }
  10. QHeaderView::section:checked
  11. {
  12. background-color: red;
  13. }
  14. /* 以下图标,只有在设置允许sortingEnable排序后,点击表头才会出现*/
  15. QHeaderView::down-arrow {
  16. width: 24px;
  17. height: 24px;
  18. image: url( E:/arrow-down.jpg);
  19. }
  20. QHeaderView::up-arrow {
  21. width: 24px;
  22. height: 24px;
  23. image: url( E:/arrow-up.jpg);
  24. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值